Difference: InClassActivityFriend (1 vs. 2)

Revision 22016-12-01 - JimSkon

Line: 1 to 1
 
META TOPICPARENT name="WebHome"

In Class Activity

Classes, Friends, Operators
Line: 42 to 42
  %ENDCODE%
Changed:
<
<

Name.cpp

>
>

Name.h

  %CODE{"c++"}% #ifndef NAME_H
Line: 68 to 68
 #endif /* NAME_H */ %ENDCODE%
Changed:
<
<

main.cpp

>
>

Name.cpp

  %CODE{"c++"}% #include

Revision 12016-12-01 - JimSkon

Line: 1 to 1
Added:
>
>
META TOPICPARENT name="WebHome"

In Class Activity

Classes, Friends, Operators

We will be writing code to use Friends and Operators

  1. Finish the name input routine
  2. Add a version of equalLast with 2 Name parameters.
  3. Get == to work on last name
  4. get <= to work on whole name (last name first)

main.cpp

<-- SyntaxHighlightingPlugin -->
#include <iostream>
#include <vector>
#include <fstream>
#include <string>
#include "Name.h"

using namespace std;

int main() {
    Name yourName, bachName("Johann", "Sebastian", "Bach");

    cout << "Enter a name (2 or 3 words)";
    yourName.input(cin);

    cout << "The names: " << endl << "1. ";
    bachName.output(cout);
    cout << endl << "2. ";
    yourName.output(cout);
    cout << endl << endl;

    if (yourName.equalLast(bachName)) {
        cout << "Same Last Names!!!" << endl;
    } else {
        cout << "Not the same Last Names!!!" << endl;
    }

}

<-- end SyntaxHighlightingPlugin -->

Name.cpp

<-- SyntaxHighlightingPlugin -->
#ifndef NAME_H
#define NAME_H

using namespace std;

class Name {
public:
    Name();
    Name(string f, string m, string l);
    void input(istream& s);
    bool equalLast(Name n);
    void output(ostream& s);
    string getLast();
    
private:
    string first;
    string middle;
    string last;    
};

#endif /* NAME_H */
<-- end SyntaxHighlightingPlugin -->

main.cpp

<-- SyntaxHighlightingPlugin -->
#include <iostream>
#include <sstream>
#include <vector>
#include <fstream>
#include <string>
#include "Name.h"

using namespace std;

Name::Name() {
}

Name::Name(string f, string m, string l) {
    first = f;
    middle = m;
    last = l;
}

void Name::output(ostream& s) {
    s << first + " " + middle + " " + last;
}

void Name::input(istream& s) {
    first = middle = last = "";
    string nameLine;
 // ...
}

string Name::getLast() {
    return last;
}

bool Name::equalLast(Name n) {
    return (last == n.getLast());
}
<-- end SyntaxHighlightingPlugin -->
 
This site is powered by the TWiki collaboration platform Powered by PerlCopyright © 2008-2019 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback