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. 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. ";
    cout << bachName;
    cout << endl << "2. ";
    yourName.output(cout);
    cout << endl << endl;

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

    if (yourName<=bachName) {
        cout << "Your name before or equal to Bach!!!" << endl;
    } else {
        cout << "Bach first!!!" << 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);
    friend bool equalLast(Name n1, Name n2);
    friend bool operator==(Name n1, Name n2);
    void output(ostream& s);
    friend ostream& operator<<(ostream& s, Name n);
    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 <sstream>
#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;
}

ostream& operator<<(ostream& s, Name n) {
    s << n.first + " " + n.middle + " " + n.last;
    return s;
}

void Name::input(istream& s) {
    first = middle = last = "";
    string nameLine;
    vector<string> names;

    getline(s, nameLine);

    istringstream nameStream(nameLine);
    do {
        string name;
        nameStream >> name;
        names.push_back(name);
    } while (nameStream);

    int nc = names.size() - 1;
    switch (nc) {
        case 0:
            cout << "Blank name!" << endl;
            break;
        case 1:
            first = middle = "";
            last = names.at(0);
            break;
        case 2:
            middle = "";
            first = names.at(0);
            last = names.at(1);
            break;
        case 3:
            first = names.at(0);
            middle = names.at(1);
            last = names.at(2);
            break;
        default:
            first = names.at(0);
            last = names.at(nc - 1);
            for (int i = 1; i < nc - 1; i++) {
                middle += names.at(i) + " ";
            }
    }
}

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

bool equalLast(Name n1, Name n2) {
    return (n1.last == n2.last);
}

bool operator==(Name n1, Name n2) {
    return (n1.last == n2.last);
}

bool operator<=(Name n1, Name n2) {
    cout << n1.last << n2.last << endl;
    if (n1.last < n2.last) {
        return true;
    } else if (n1.last == n2.last) {
        if (n1.first < n2.first) {
            return true;
        } else if (n1.middle <= n2.middle) {
            return true;
        }
    }
    return false;
}
Edit | Attach | Watch | Print version | History: r2 < r1 | Backlinks | Raw View | Raw edit | More topic actions
Topic revision: r2 - 2018-04-12 - 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