Advertisement
Guest User

Untitled

a guest
Dec 3rd, 2015
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.00 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.    
  153.     }
  154.  
  155. };
  156.  
  157. int main() {
  158.    
  159.     person p1("John", "John");
  160.    
  161.     person p2("Tom", "Smith");
  162.    
  163.     person p3("Hugo", "Armond");
  164.    
  165.     person p4("Juan", "Juan");
  166.    
  167.     person_with_telephone p5("Dick", "Smith", "+123456789");
  168.    
  169.     person_with_email p6("Harry", "Smith", "hsmith@hotmail.com");
  170.    
  171.     person_with_telephone_and_email p7("Mary", "Smith", "+123456789", "msmith@hotmail.com");
  172.    
  173.     contact agenda("Agenda");
  174.    
  175.     // person *pp = 0;
  176. //          while (read_person(cin, pp) && pp){
  177. //          cout << *pp << endl;
  178. //      }
  179.    
  180.     agenda.add(p1);
  181.     agenda.add(p2);
  182.     agenda.add(p3);
  183.     agenda.add(p4);
  184.     agenda.add(p5);
  185.     agenda.add(p6);
  186.     agenda.add(p7);
  187.    
  188.     agenda.in_order();
  189.    
  190.    
  191.     //cout << p1 << '\n';
  192.     //cout << p2 << '\n';
  193.     //cout << p3 << '\n';
  194.     //cout << p4 << '\n';
  195.    
  196.     agenda.print_persons();
  197.     //agenda.find_person("Juan");
  198.    
  199.     return 0;
  200. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement