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

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

using namespace std;

int main() {
    Name yourName, bachName("Johann", "Sebastian", "Bach");
    do {
    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)) {
//    if (equalLast(yourName,bachName)) {
    if (yourName==bachName) {
        cout << "Same Last Names!!!" << endl;
    } else {
        cout << "Not the same Last Names!!!" << endl;
    }
    }
    while (1);

}

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();
    friend bool operator==(Name n1, Name n2);
    
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;
    getline(s,nameLine);
    cout << nameLine << endl;
    
    size_t pos1 = nameLine.find(' ');
    if (pos1 == string::npos) {
        if (nameLine.length()==0) {
            cout << "Nothing entered!!!!" << endl;
        } else {
            last = nameLine;
        }
        return;
    }
    // more than one name
    first = nameLine.substr(0,pos1);
    
    size_t pos2 = nameLine.find(' ',pos1+1);
    if (pos2 == string::npos) {
        // this is the last name (no middle name)
        last = nameLine.substr(pos1+1);
        return;
    }
    // There is a middle and last name
    middle = nameLine.substr(pos1+1,pos2-pos1-1);
    last = nameLine.substr(pos2+1);
            

 // ...
}

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

bool Name::equalLast(Name n) {
    return (last == n.getLast());
}

bool operator==(Name n1, Name n2) {
   //return ( n1.getLast()==n2.getLast());
    return (n1.last==n2.last);
}
Edit | Attach | Watch | Print version | History: r2 < r1 | Backlinks | Raw View | Raw edit | More topic actions...
Topic revision: r1 - 2017-04-27 - JimSkon
 
  • Edit
  • Attach
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