Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <list>
- #include <string>
- #include <sstream>
- #include <algorithm>
- using namespace std;
- class person{ // Question 1
- protected:
- string pname;
- string psurname;
- public:
- person(string name, string surname) : pname(name), psurname(surname){}
- string get_name() const{return pname;}
- string get_surname() const{return psurname;}
- virtual bool has_telephone_p(){return false;}
- virtual bool has_email_p(){return false;}
- virtual string print() const{return "<person S " + psurname + " N " + pname + " >";}
- void set_name(string name){pname = name;}
- void set_surname(string surname){psurname = surname;}
- };
- class person_with_telephone : public virtual person{ // Question 2
- protected:
- string ptelephone;
- public:
- person_with_telephone(string name, string surname, string phone) : person(name, surname), ptelephone(phone){}
- string get_telephone() const{return ptelephone;}
- void set_telephone(string telephone){ptelephone = telephone;}
- virtual bool has_telephone_p(){return true;}
- virtual string print() const{return "<person S " + psurname + " N " + pname + " T " + ptelephone + " >";}
- };
- class person_with_email : public virtual person{ // Question 3
- protected:
- string pemail;
- public:
- person_with_email(string name, string surname, string email) : person(name, surname), pemail(email){}
- string get_email(){return pemail;}
- void set_email(string email){pemail = email;}
- virtual string print() const{return "<person S " + psurname + " N " + pname + " E " + pemail + " >";}
- };
- class person_with_telephone_and_email : public person_with_telephone, public person_with_email{ // Question 4
- public:
- person_with_telephone_and_email(string name, string surname, string phone, string email) : person(name, surname), person_with_telephone(name, surname, phone), person_with_email(name, surname,email){}
- virtual string print() const{return "<person S " + psurname + " N " + pname + " T " + ptelephone + " E " + pemail + " >";}
- };
- bool operator== (const person &p1, const person &p2) {
- return p1.get_surname() == p2.get_surname() && p1.get_name() == p2.get_name();
- }
- ostream & operator<<(ostream & os, const person &p){ // Question 5
- return os << p.print();
- }
- istream & read_person(istream & in, person* &p){ // Question 6
- string per, name, surname, details, email;
- char s, n, c1, c2;
- //cout << "Enter name, surname and details:\n";
- if ((in >> per) && (per == "<person")){
- if ((in >> s >> surname >> n >> name >> c1) && (s == 'S' && n == 'N' && c1 == '>')){
- //cout << "person" << '\n';
- p = new person(name, surname);
- } else if ((in >> details >> c2) && (c1 == 'T' && c2 == '>')){
- //cout << "person with phone" << '\n';
- p = new person_with_telephone(name, surname, details);
- } else if ((in) && (c1 == 'E' && c2 == '>')){
- //cout << "person with email" << '\n';
- p = new person_with_email(name, surname, details);
- } else if ((in >> email >> c2) && (c2 == '>')){
- //cout << "person with phone and email" << '\n';
- p = new person_with_telephone_and_email(name, surname, details, email);
- } else if ((in) && (s != 'S')){
- in.setstate(ios::badbit);
- //cout << "read failed" << '\n';
- }
- }
- return in;
- }
- class contact{
- private:
- list<person *> persons;
- string contact_name;
- public:
- contact(string name) : contact_name(name){}
- void add(person &new_person) {
- //persons.push_back(&new_person);
- bool per = false;
- for(auto i = begin(persons); i != end(persons); ++i){
- if(**i == new_person){
- *i = &new_person;
- per = true;
- }
- }
- if(per == false){
- persons.push_back(&new_person);
- }
- }
- void print_persons(){
- for (auto i = begin(persons); i != end(persons); ++i){
- cout << (*i)->print() << '\n';
- }
- }
- void find_person(string name){
- for (auto i = begin(persons); i != end(persons); ++i){
- if(name == (*i)->get_name() || name == (*i)->get_surname()){
- cout << (*i)->print() << '\n';
- }
- }
- }
- void in_order(){
- persons.sort();
- }
- void with_telephone(){
- }
- };
- int main() {
- person p1("John", "John");
- person p2("Tom", "Smith");
- person p3("Hugo", "Armond");
- person p4("Juan", "Juan");
- person_with_telephone p5("Dick", "Smith", "+123456789");
- person_with_email p6("Harry", "Smith", "hsmith@hotmail.com");
- person_with_telephone_and_email p7("Mary", "Smith", "+123456789", "msmith@hotmail.com");
- contact agenda("Agenda");
- // person *pp = 0;
- // while (read_person(cin, pp) && pp){
- // cout << *pp << endl;
- // }
- agenda.add(p1);
- agenda.add(p2);
- agenda.add(p3);
- agenda.add(p4);
- agenda.add(p5);
- agenda.add(p6);
- agenda.add(p7);
- agenda.in_order();
- //cout << p1 << '\n';
- //cout << p2 << '\n';
- //cout << p3 << '\n';
- //cout << p4 << '\n';
- agenda.print_persons();
- //agenda.find_person("Juan");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement