Tags:
create new tag
view all tags

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. Make a version of << that works with names.
  5. get <= to work on whole name (last name first)

main.cpp

#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;
    }

}

Name.h

#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 */

Name.cpp

#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());
}
Edit | Attach | Watch | Print version | History: r3 < r2 < r1 | Backlinks | Raw View | Raw edit | More topic actions
Topic revision: r3 - 2018-04-10 - JimSkon
 
This site is powered by the TWiki collaboration platform Powered by PerlCopyright © 2008-2018 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback