Advertisement
Guest User

Untitled

a guest
Dec 3rd, 2015
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.31 KB | None | 0 0
  1. #include <iostream>
  2. #include <list>
  3. #include <string>
  4. #include <sstream>
  5. #include <algorithm>
  6.  
  7. using namespace std;
  8.  
  9. class person{ // Question 1
  10.     protected:
  11.         string pname;
  12.         string psurname;
  13.  
  14.     public:
  15.         person(string name, string surname) : pname(name), psurname(surname){}
  16.     
  17.         string get_name() const{return pname;}
  18.         
  19.         string get_surname() const{return psurname;}
  20.         
  21.         virtual bool has_telephone_p(){return false;}
  22.         
  23.         virtual bool has_email_p(){return false;}
  24.         
  25.         virtual string print() const{return "<person S " + psurname + " N " + pname + " >";}
  26.         
  27.         void set_name(string name){pname = name;}
  28.         
  29.         void set_surname(string surname){psurname = surname;}
  30.         
  31. };
  32.  
  33. class person_with_telephone : public virtual person{ // Question 2
  34.     protected:
  35.         string ptelephone;
  36.  
  37.     public:
  38.         person_with_telephone(string name, string surname, string phone) : person(name, surname), ptelephone(phone){}
  39.  
  40.     string get_telephone() const{return ptelephone;}
  41.         
  42.     void set_telephone(string telephone){ptelephone = telephone;}
  43.     
  44.     virtual bool has_telephone_p(){return true;}
  45.     
  46.     virtual string print() const{return "<person S " + psurname + " N " + pname + " T " + ptelephone + " >";}
  47.     
  48. };
  49.  
  50. class person_with_email : public virtual person{ // Question 3
  51.     protected:
  52.         string pemail;
  53.  
  54.     public:
  55.         person_with_email(string name, string surname, string email) : person(name, surname), pemail(email){}
  56.  
  57.     string get_email(){return pemail;}
  58.         
  59.     void set_email(string email){pemail = email;}
  60.     
  61.     virtual string print() const{return "<person S " + psurname + " N " + pname + " E " + pemail + " >";}
  62. };
  63.  
  64.  
  65. class person_with_telephone_and_email : public person_with_telephone, public person_with_email{ // Question 4
  66.     public:
  67.         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){}
  68.     
  69.         virtual string print() const{return "<person S " + psurname + " N " + pname + " T " + ptelephone + " E " + pemail + " >";}
  70.     
  71. };
  72.  
  73.     bool operator== (const person &p1, const person &p2) {
  74.         return p1.get_surname() == p2.get_surname() && p1.get_name() == p2.get_name();
  75.     }
  76.     
  77.     ostream & operator<<(ostream & os, const person &p){ // Question 5
  78.         return os << p.print();
  79.     }
  80.  
  81.     istream & read_person(istream & in, person* &p){ // Question 6
  82.         string per, name, surname, details, email;
  83.         char s, n, c1, c2;
  84.         //cout << "Enter name, surname and details:\n";
  85.         
  86.         if ((in >> per) && (per == "<person")){
  87.             if ((in >> s >> surname >> n >> name >> c1) && (s == 'S' && n == 'N' && c1 == '>')){
  88.                 //cout << "person" << '\n';
  89.                 p = new person(name, surname);
  90.             } else if ((in >> details >> c2) && (c1 == 'T' && c2 == '>')){
  91.                 //cout << "person with phone" << '\n';
  92.                 p = new person_with_telephone(name, surname, details);
  93.             } else if ((in) && (c1 == 'E' && c2 == '>')){
  94.                 //cout << "person with email" << '\n';
  95.                 p = new person_with_email(name, surname, details);
  96.             } else if ((in >> email >> c2) && (c2 == '>')){
  97.                 //cout << "person with phone and email" << '\n';
  98.                 p = new person_with_telephone_and_email(name, surname, details, email);
  99.             } else if ((in) && (s != 'S')){
  100.                 in.setstate(ios::badbit);
  101.                 //cout << "read failed" << '\n';
  102.             }
  103.         }
  104.         
  105.         return in;
  106.     }
  107.  
  108.  
  109. class contact{
  110.     
  111.     private:
  112.         list<person *> persons;
  113.         string contact_name;
  114.     
  115.     public:
  116.     
  117.     contact(string name) : contact_name(name){}
  118.     
  119.     void add(person &new_person) {
  120.         //persons.push_back(&new_person);
  121.         bool per = false;
  122.         for(auto i = begin(persons); i != end(persons); ++i){
  123.             if(**i == new_person){
  124.                 *i = &new_person;
  125.                 per = true;
  126.             }
  127.         }
  128.         if(per == false){
  129.             persons.push_back(&new_person);
  130.         }
  131.     }
  132.     
  133.     void print_persons(){
  134.         for (auto i = begin(persons); i != end(persons); ++i){
  135.             cout << (*i)->print() << '\n';
  136.         }
  137.     }
  138.     
  139.     void find_person(string name){
  140.         for (auto i = begin(persons); i != end(persons); ++i){
  141.             if(name == (*i)->get_name() || name == (*i)->get_surname()){
  142.                 cout << (*i)->print() << '\n';
  143.             }
  144.         }
  145.     }
  146.     
  147.     void in_order(){
  148.         persons.sort();
  149.     }
  150.     
  151.     void with_telephone(){
  152. in_order();
  153.  
  154.     for (auto i = begin(persons); i != end(persons); ++i){
  155.            if((*i)->has_telephone_p()){
  156. cout << (*i)->print() << endl;
  157. }
  158.         }
  159.  
  160.     }
  161.  
  162. };
  163.  
  164. int main() {
  165.     
  166.     person p1("John", "John");
  167.  
  168. person_with_telephone p8("Zick", "Smith", "+123456789");
  169.     
  170.     person p2("Tom", "Smith");
  171.     
  172.     person p3("Juan", "Juan");
  173.     
  174.     person p4("Juan", "Juan");
  175.     
  176.     person_with_telephone p5("Dick", "Smith", "+123456789");
  177.     
  178.     person_with_email p6("Juan", "Juan", "hsmith@hotmail.com");
  179.     
  180.     person_with_telephone_and_email p7("Mary", "Smith", "+123456789", "msmith@hotmail.com");
  181.     
  182.     contact agenda("Agenda");
  183.     
  184.     // person *pp = 0;
  185. //          while (read_person(cin, pp) && pp){
  186. //          cout << *pp << endl;
  187. //      }
  188.     
  189. agenda.add(p8);
  190.     agenda.add(p1);
  191.     agenda.add(p2);
  192.     agenda.add(p3);
  193.     agenda.add(p4);
  194.     agenda.add(p5);
  195.     agenda.add(p6);
  196.     agenda.add(p7);
  197.       
  198.       agenda.in_order();
  199.     
  200.     
  201.     //cout << p1 << '\n';
  202.     //cout << p2 << '\n';
  203.     //cout << p3 << '\n';
  204.     //cout << p4 << '\n';
  205.     
  206.     agenda.print_persons();
  207.     //agenda.find_person("Juan");
  208.  
  209. agenda.with_telephone();
  210.     
  211.     return 0;
  212. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement