Advertisement
Elygian

Frustrating problems!

Dec 3rd, 2015
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.00 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <sstream>
  4.  
  5. //Allows use of basic C++ functions
  6. using namespace std;
  7.  
  8. //Base class for all classes
  9. class Person
  10. {
  11.  
  12. protected:
  13.     string m_Name, m_Surname, m_Telephone, m_Email;
  14.  
  15. public:
  16.     Person() { m_Name = "NoName", m_Surname = "NoSurname", m_Telephone = "", m_Email = ""; }
  17.     Person(string &s, string &f)
  18.     {
  19.         set_surname(s);
  20.         set_name(f);
  21.     }
  22.  
  23.     void set_name(string &f)
  24.     {
  25.         m_Name = f;
  26.     }
  27.  
  28.     void set_surname(string &s)
  29.     {
  30.         m_Surname = s;
  31.     }
  32.  
  33.     virtual string get_name()
  34.     {
  35.         return m_Name;
  36.     }
  37.  
  38.     virtual string get_surname()
  39.     {
  40.         return m_Surname;
  41.     }
  42.  
  43.     virtual string get_telephone()
  44.     {
  45.         return m_Telephone;
  46.     }
  47.  
  48.     virtual string get_email()
  49.     {
  50.         return m_Email;
  51.     }
  52.  
  53.    
  54.     virtual bool has_telephone_p()
  55.     {
  56.  
  57.         if (m_Telephone.empty())
  58.         {
  59.             //cout << "NoNumber" << endl;
  60.             return false;
  61.         }
  62.  
  63.         else
  64.         {
  65.             //cout << "Your phone number is now registered"<< endl;
  66.             return true;
  67.         }
  68.  
  69.     }
  70.  
  71.     virtual bool has_email_p()
  72.     {
  73.  
  74.         if (m_Email.empty())
  75.         {
  76.             //cout << "NoEmail" << endl;
  77.             return false;
  78.         }
  79.  
  80.         else
  81.         {
  82.             //cout << "Your email is now registered" << endl;
  83.             return true;
  84.         }
  85.  
  86.     }
  87.  
  88.     //Allows the ostream class to access the variables in class Person
  89.     friend ostream &operator<<(ostream &output, Person &p);
  90.  
  91. };
  92.  
  93. //Inherits from class Person
  94. class Person_with_telephone : public virtual Person
  95. {
  96.  
  97. protected:
  98.  
  99. public:
  100.     Person_with_telephone() {}
  101.     Person_with_telephone(string &s, string &f, string &telephone)
  102.     {
  103.         set_surname(s);
  104.         set_name(f);
  105.         set_telephone(telephone);
  106.     }
  107.  
  108.     void set_telephone(string &telephone)
  109.     {
  110.         m_Telephone = telephone;
  111.     }
  112.  
  113.  
  114. };
  115.  
  116. //Inherits from class Person
  117. class Person_with_email : public virtual Person
  118. {
  119. protected:
  120.  
  121. public:
  122.     Person_with_email() {}
  123.     Person_with_email(string &s, string &f ,string &email)
  124.     {
  125.         set_surname(s);
  126.         set_name(f);
  127.         set_email(email);
  128.     }
  129.  
  130.     void set_email(string &email)
  131.     {
  132.         m_Email = email;
  133.     }
  134.  
  135. };
  136.  
  137. //Inherits from classes Person_with_telephone and Person_and_email
  138. class Person_with_telephone_and_email : public virtual Person_with_telephone, public virtual Person_with_email
  139. {
  140. protected:
  141.  
  142. public:
  143.     Person_with_telephone_and_email() {}
  144.     Person_with_telephone_and_email(string &s, string &f, string &telephone, string &email)
  145.     {
  146.         set_surname(s);
  147.         set_name(f);
  148.         set_telephone(telephone);
  149.         set_email(email);
  150.     }
  151. };
  152.  
  153. ostream &operator << (ostream &output, Person &p)
  154. {
  155.     //Checks that both 'telephone' and 'email' are initialized to NoNumber and NoEmail respectively. If true, it prints name and surname
  156.     if (p.has_telephone_p() == false && p.has_email_p() == false)
  157.     {
  158.         output << "<person" << " S " << p.get_surname() << " N " << p.get_name() <<" >" <<  endl;
  159.         cout << endl;
  160.         return output;
  161.     }
  162.     //Checks that 'email' is initialized to NoEmail. If true, it prints name, surname, and telephone
  163.     else if (p.has_email_p() == false)
  164.     {
  165.         output << "<person" << " S " << p.get_surname() << " N " << p.get_name() << " T " << p.get_telephone() << " >" << endl;
  166.         cout << endl;
  167.         return output;
  168.     }
  169.     //Checks that 'telephone' is initialized to NoNumber. If true, it prints name, surname, and email
  170.     else if (p.has_telephone_p() == false)
  171.     {
  172.         output << "<person" << " S " << p.get_surname() << " N " << p.get_name() << " E " << p.get_email() << " >" << endl;
  173.         cout << endl;
  174.         return output;
  175.     }
  176.     //Checks to see that all 4 variables are initialized to non default values. If true, it prints all 4 values
  177.     else
  178.     {
  179.         output << "<person" << " S " << p.get_surname() << " N " << p.get_name() << " T " << p.get_telephone() << " E " << p.get_email() << " >" << endl;
  180.         cout << endl;
  181.         return output;
  182.     }
  183. }
  184.  
  185. //This is called at runtime after object Person is constructed from it's class
  186. istream &operator >> (istream &input, Person &p)
  187. {
  188.     string s, f;
  189.     cout << "Enter Surname: ";
  190.     input >> s;
  191.     cout << "Enter Name: ";
  192.     input >> f;
  193.     p.set_surname(s);
  194.     p.set_name(f);
  195.     return input;
  196. }
  197.  
  198. //This is called at runtime after object Person_with_telephone is constructed from it's class
  199. istream &operator >> (istream &input, Person_with_telephone &pwt)
  200. {
  201.     string s, f, t;
  202.     cout << "Enter Surname: ";
  203.     input >> s;
  204.     cout << "Enter Name: ";
  205.     input >> f;
  206.     cout << "Enter Telephone: ";
  207.     input >> t;
  208.     pwt.set_surname(s);
  209.     pwt.set_name(f);
  210.     pwt.set_telephone(t);
  211.     return input;
  212. }
  213.  
  214. //This is called at runtime after object Person_with_email is constructed from it's class
  215. istream &operator >> (istream &input, Person_with_email &pwe)
  216. {
  217.     string s, f, e;
  218.     cout << "Enter Surname: ";
  219.     input >> s;
  220.     cout << "Enter Name: ";
  221.     input >> f;
  222.     cout << "Enter Email: ";
  223.     input >> e;
  224.     pwe.set_surname(s);
  225.     pwe.set_name(f);
  226.     pwe.set_email(e);
  227.     return input;
  228. }
  229.  
  230. //This is called at runtime after object Person_with_telephone_and_email is constructed from it's class
  231. istream &operator >> (istream &input, Person_with_telephone_and_email &pwte)
  232. {
  233.     string s, f, t, e;
  234.     cout << "Enter Surname: ";
  235.     input >> s;
  236.     cout << "Enter Name: ";
  237.     input >> f;
  238.     cout << "Enter Telephone: ";
  239.     input >> t;
  240.     cout << "Enter Email: ";
  241.     input >> e;
  242.     pwte.set_surname(s);
  243.     pwte.set_name(f);
  244.     pwte.set_telephone(t);
  245.     pwte.set_email(e);
  246.     return input;
  247. }
  248.  
  249. int main()
  250. {
  251.     //This creates object Person and takes input to initialize the variables
  252.     cout << "Person"<< endl << endl;
  253.     Person p;
  254.     cin >> p;
  255.     cout << p;
  256.  
  257.     //This creates object Person_with_telephone and takes input to initialize the variables
  258.     cout << "Person with telephone" <<endl << endl;
  259.     Person_with_telephone pwt;
  260.     cin >> pwt;
  261.     cout << pwt;
  262.  
  263.     //This creates object Person_with_email and takes input to initialize the variables
  264.     cout << "Person with email" << endl << endl;
  265.     Person_with_email pwe;
  266.     cin >> pwe;
  267.     cout << pwe;
  268.  
  269.     //This creates object Person_with_telephone_and_email and takes input to initialize the variables
  270.     cout << "Person with telephone and email" << endl << endl;
  271.     Person_with_telephone_and_email pwte;
  272.     cin >> pwte;
  273.     cout << pwte;
  274.  
  275.  
  276.     return 0;
  277. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement