Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <sstream>
- #include <vector>
- using namespace std;
- class Person
- {
- protected:
- string forename, surname, email;
- public:
- Person(){}
- Person(string s, string f):surname(s), forename(f){}
- string get_surname() const//get the persons surname
- {
- return surname;
- }
- string get_forename() const//get the persons first name
- {
- return forename;
- }
- virtual bool has_telephone() const{ return false; } //returns true if person has a telephone number
- virtual bool has_email() const { return false; }//returns true if the person has a email
- virtual string get_email() const { return ""; };//gets the persons email
- virtual string get_telephone() const { return ""; };//gets the persons telephone number
- friend bool operator==(Person &person1, Person &person2);
- virtual void type() const //persons class type for later functions
- {
- cout << "person" << endl;
- };
- };
- //----------------------------------------------------------------------------------------------------
- //Override the == comparator to compare person objects
- //bool operator== (Person &person1, Person &person2)
- //{
- // cout << person1.get_surname() << " " << person2.get_surname();
- // return ((person1.forename == person2.forename) && (person1.surname == person1.surname));
- //}
- //person with telephone class with class person inheritance
- class Person_with_telephone : public virtual Person
- {
- protected:
- string telephone_number;
- public:
- Person_with_telephone(){}
- Person_with_telephone(string s, string f, string t) : Person(s, f), telephone_number(t){}
- virtual string get_telephone() const//get persons telephone number
- {
- return telephone_number;
- }
- void set_telephone(string &t)//set classes telephone number
- {
- telephone_number = t;
- }
- virtual void type() const //class type shown as t for telephone
- {
- cout << "person w/ t" << endl;
- };
- };
- //----------------------------------------------------------------------------------------------------
- class Person_with_email : public virtual Person
- {
- protected:
- string email;
- public:
- Person_with_email(){}
- Person_with_email(string s, string f, string e) :Person(s, f), email(e){}
- bool has_email() const
- {
- return !email.empty();
- }
- virtual string get_email() const
- {
- return email;
- }
- void set_email(string &e)
- {
- email = e;
- }
- virtual void type() const
- {
- cout << "person w/ e" << endl;
- };
- };
- //----------------------------------------------------------------------------------------------------
- class Person_with_telephone_email : public Person_with_telephone, public Person_with_email{
- public:
- Person_with_telephone_email(){}
- Person_with_telephone_email(string s, string f, string t, string e) : Person(s, f), Person_with_telephone(s, f, t), Person_with_email(s, f, e) {}
- virtual void type() const
- {
- cout << "person w/ t + e" << endl;
- };
- };
- //----------------------------------------------------------------------------------------------------
- ostream& operator<<(ostream &os, Person &p)
- {
- if (p.has_email() && p.has_telephone())
- {
- cout << "<person" << " S " << p.get_surname() << " F " << p.get_forename() << " E " << p.get_email() << " T " << p.get_telephone() << " >";
- }
- else if (p.has_email())
- {
- cout << "<person" << " S " << p.get_surname() << " F " << p.get_forename() << " E " << p.get_email() << " >";
- }
- else if (p.has_telephone())
- {
- cout << "<person" << " S " << p.get_surname() << " F " << p.get_forename() << " T " << p.get_telephone() << " >";
- }
- else
- {
- cout << "<person" << " S " << p.get_surname() << " F " << p.get_forename() << " >";
- }
- return os;
- }
- //----------------------------------------------------------------------------------------------------
- istream& operator>>(istream &is, Person &p){
- string opening, s, surname, f, forename, closing;
- if (
- (is >> opening >> s >> surname >> f >> forename >> closing)
- &&
- ((opening == "<person") && (s == "S") && (f == "F") && (closing == ">"))
- )
- {
- p = Person(surname, forename);
- }
- return is;
- }
- istream& operator>>(istream &is, Person_with_email &p){
- string opening, s, surname, f, forename, e, email, closing;
- if (
- (is >> opening >> s >> surname >> f >> forename >> e >> email >> closing)
- &&
- ((opening == "<person") && (s == "S") && (f == "F") && (e == "E") && (closing == ">"))){
- p = Person_with_email(surname, forename, email);
- }
- return is;
- }
- istream& operator>>(istream &is, Person_with_telephone &p){
- string opening, s, surname, f, forename, t, telephone, closing;
- if (
- (is >> opening >> s >> surname >> f >> forename >> t >> telephone >> closing) &&
- ((opening == "<person") &&
- (s == "S") &&
- (f == "F") &&
- (t == "T") &&
- (closing == ">"))
- )
- {
- p = Person_with_telephone(surname, forename, telephone);
- }
- return is;
- }
- istream& operator>>(istream &is, Person_with_telephone_email &p){
- string opening, s, surname, f, forename, t, telephone, e, email, closing;
- if (
- (is >> opening >> s >> surname >> f >> forename >> t >> telephone >> e >> email >> closing) &&
- ((opening == "<person") &&
- (s == "S") &&
- (f == "F") &&
- (t == "T") &&
- (e == "E") &&
- (closing == ">"))
- )
- {
- p = Person_with_telephone_email(surname, forename, telephone, email);
- }
- return is;
- }
- Person* make_person(istream &stream){
- Person *person_pointer = new Person;
- stream >> *person_pointer;
- return person_pointer;
- }
- Person_with_email* make_person_email(istream &stream){
- Person_with_email *person_pointer = new Person_with_email;
- stream >> *person_pointer;
- return person_pointer;
- }
- Person_with_telephone* make_person_telephone(istream &stream){
- Person_with_telephone *person_pointer = new Person_with_telephone;
- stream >> *person_pointer;
- return person_pointer;
- }
- Person_with_telephone_email* make_person_telephone_email(istream &stream){
- Person_with_telephone_email *person_pointer = new Person_with_telephone_email;
- stream >> *person_pointer;
- return person_pointer;
- }
- istream & read_person(istream &in, Person * & p){
- string input;
- getline(in, input);
- stringstream stream(input);
- string e = " E ";
- string t = " T ";
- bool has_e = false;
- bool has_t = false;
- if (input.find(e) != std::string::npos){ has_e = true; }
- if (input.find(t) != string::npos){ has_t = true; }
- if (has_e && has_t) {
- cout << "e + t\n";
- Person_with_telephone_email *person_pointer = make_person_telephone_email(stream);
- p = person_pointer;
- }
- else if (has_e){
- cout << "e\n";
- Person_with_email *person_pointer = make_person_email(stream);
- p = person_pointer;
- }
- else if (has_t){
- cout << "t \n";
- Person_with_telephone *person_pointer = make_person_telephone(stream);
- p = person_pointer;
- }
- else{
- cout << "is person \n";
- Person *person_pointer = make_person(stream);
- p = person_pointer;
- }
- return in;
- }
- //----------------------------------------------------------------------------------------------------
- /**
- Container class for storing and handling objects of class Person
- */
- template <typename Contact>
- class Contacts {
- private:
- vector<Person*> data;
- int data_size;
- public:
- Contacts() {
- data_size = 0;
- }
- void add(Person p ) {
- data.push_back(&p);
- data_size++;
- }
- void remove(int i) {
- data.erase(data.begin() + (i - 1));
- data_size--;
- }
- Contact get(int i) {
- return data[i];
- }
- int get_size() {
- return data.size();
- }
- ~Contacts() {
- data.clear();
- }
- };
- //----------------------------------------------------------------------------------------------------
- //this is main
- int main(){
- //Tests the == compare
- // Person *p = new Person("Dave", "Harry");
- // Person *s = new Person("Dave", "Harry");
- // Person *d = new Person("Dave", "Johns");
- // if (p == s)
- // {
- // cout << "They're the same!\n";
- // }+
- //Tests the read_person istream
- // Person *p = 0;
- // string ss = "<person S Smith N Tom >\n<person S Smith N Dick T +49.921.1434 >\n<person S Smith N Harry E hsmith@gmail.com >\n<person S Smith N Mary T +39.921.1434 E msmith@gmail.com >\n<person S John N John T + 33.921.1434 E jjohn@gmail.com >";
- // stringstream iss(ss);
- // while (read_person(iss, p) && p)
- // cout << *p << endl;
- //Tests the output and input of the vector
- Contacts <Person> c;
- cout << c.get_size() << endl;
- Person john1("John","Smith");
- c.add(john1);
- Person_with_telephone john2("John", "Tim","+02958.5645764.75");
- c.add(john2);
- Person_with_email john3("John", "Jones","email@email.com");
- c.add(john3);
- Person_with_telephone_email john4 ("John", "Adams", "+33.02.0101.0202", "email@email.com");
- c.add(john4);
- //phone numbers don't seem to output???
- cout << john1 << endl;
- cout << john2 << endl;
- cout << john3 << endl;
- cout << john4 << endl;
- //c.get causing errors
- // << c.get(1) << endl << c.get(2) << endl << c.get(3) << endl;
- cout << c.get_size() << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement