Advertisement
Elygian

Untitled

Dec 5th, 2015
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.97 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.  
  250.  
  251. int main()
  252. {
  253.     //This creates object Person and takes input to initialize the variables
  254.     cout << "Person"<< endl << endl;
  255.     Person p;
  256.     cin >> p;
  257.     cout << p;
  258.  
  259.     //This creates object Person_with_telephone and takes input to initialize the variables
  260.     cout << "Person with telephone" <<endl << endl;
  261.     Person_with_telephone pwt;
  262.     cin >> pwt;
  263.     cout << pwt;
  264.  
  265.     //This creates object Person_with_email and takes input to initialize the variables
  266.     cout << "Person with email" << endl << endl;
  267.     Person_with_email pwe;
  268.     cin >> pwe;
  269.     cout << pwe;
  270.  
  271.     //This creates object Person_with_telephone_and_email and takes input to initialize the variables
  272.     cout << "Person with telephone and email" << endl << endl;
  273.     Person_with_telephone_and_email pwte;
  274.     cin >> pwte;
  275.     cout << pwte;
  276.  
  277.  
  278.     return 0;
  279. }
  280.  
  281. //istream & read_person(istream &in, Person * & p)
  282. //{
  283. //  string input;
  284. //  getline(in, input);
  285. //  stringstream stream(input);
  286. //  string e = " E ";
  287. //  string t = " T ";
  288. //  bool has_e = false;
  289. //  bool has_t = false;
  290. //
  291. //  if (input.find(e) != std::string::npos) { has_e = true; }
  292. //  if (input.find(t) != string::npos) { has_t = true; }
  293. //
  294. //  if (has_e && has_t)
  295. //  {
  296. //      cout << "e + t\n";
  297. //      Person_with_telephone_and_email *person_pointer = make_person_telephone_email(stream);
  298. //      p = person_pointer;
  299. //  }
  300. //  else if (has_e)
  301. //  {
  302. //      cout << "e\n";
  303. //      Person_with_email *person_pointer = make_person_email(stream);
  304. //      p = person_pointer;
  305. //  }
  306. //  else if (has_t)
  307. //  {
  308. //      cout << "t \n";
  309. //      Person_with_telephone *person_pointer = make_person_telephone(stream);
  310. //      p = person_pointer;
  311. //  }
  312. //  else
  313. //  {
  314. //      cout << "is person \n";
  315. //      Person *person_pointer = make_person(stream);
  316. //      p = person_pointer;
  317. //  }
  318. //  return in;
  319. //}
  320. //istream& operator>>(istream &is, Person_with_email &p) {
  321. //  string opening, s, surname, f, forename, e, email, closing;
  322. //
  323. //  if (
  324. //      (is >> opening >> s >> surname >> f >> forename >> e >> email >> closing) &&
  325. //      ((opening == "<person") && (s == "S") && (f == "F") && (e == "E") && (closing == ">"))) {
  326. //      p = Person_with_email(surname, forename, email);
  327. //  }
  328. //  return is;
  329. //}
  330.  
  331. //istream& operator>>(istream &is, Person_with_telephone &p) {
  332. //  string opening, s, surname, f, forename, t, telephone, closing;
  333. //  if (
  334. //      (is >> opening >> s >> surname >> f >> forename >> t >> telephone >> closing) &&
  335. //      ((opening == "<person") &&
  336. //          (s == "S") &&
  337. //          (f == "F") &&
  338. //          (t == "T") &&
  339. //          (closing == ">"))
  340. //      )
  341. //  {
  342. //      p = Person_with_telephone(surname, forename, telephone);
  343. //  }
  344. //  return is;
  345. //}
  346. //
  347. //istream& operator>>(istream &is, Person_with_telephone_and_email &p) {
  348. //  string opening, s, surname, f, forename, t, telephone, e, email, closing;
  349. //
  350. //  if (
  351. //      (is >> opening >> s >> surname >> f >> forename >> t >> telephone >> e >> email >> closing) &&
  352. //      ((opening == "<person") &&
  353. //          (s == "S") &&
  354. //          (f == "F") &&
  355. //          (t == "T") &&
  356. //          (e == "E") &&
  357. //          (closing == ">"))
  358. //      )
  359. //  {
  360. //      p = Person_with_telephone_and_email(surname, forename, telephone, email);
  361. //  }
  362. //  return is;
  363. //}
  364.  
  365. //Person* make_person(istream &stream) {
  366. //  Person *person_pointer = new Person;
  367. //  stream >> *person_pointer;
  368. //  return person_pointer;
  369. //}
  370. //
  371. //Person_with_email* make_person_email(istream &stream) {
  372. //  Person_with_email *person_pointer = new Person_with_email;
  373. //  stream >> *person_pointer;
  374. //  return person_pointer;
  375. //}
  376. //
  377. //Person_with_telephone* make_person_telephone(istream &stream) {
  378. //  Person_with_telephone *person_pointer = new Person_with_telephone;
  379. //  stream >> *person_pointer;
  380. //  return person_pointer;
  381. //}
  382. //
  383. //Person_with_telephone_and_email* make_person_telephone_email(istream &stream) {
  384. //  Person_with_telephone_email *person_pointer = new Person_with_telephone_email;
  385. //  stream >> *person_pointer;
  386. //  return person_pointer;
  387. //}
  388. //
  389. //istream & read_person(istream &in, Person * & p) {
  390. //  string input;
  391. //  getline(in, input);
  392. //  stringstream stream(input);
  393. //  string e = " E ";
  394. //  string t = " T ";
  395. //  bool has_e = false;
  396. //  bool has_t = false;
  397. //
  398. //  if (input.find(e) != std::string::npos) { has_e = true; }
  399. //  if (input.find(t) != string::npos) { has_t = true; }
  400. //
  401. //  if (has_e && has_t) {
  402. //      cout << "e + t\n";
  403. //      Person_with_telephone_email *person_pointer = make_person_telephone_email(stream);
  404. //      p = person_pointer;
  405. //  }
  406. //  else if (has_e) {
  407. //      cout << "e\n";
  408. //      Person_with_email *person_pointer = make_person_email(stream);
  409. //      p = person_pointer;
  410. //  }
  411. //  else if (has_t) {
  412. //      cout << "t \n";
  413. //      Person_with_telephone *person_pointer = make_person_telephone(stream);
  414. //      p = person_pointer;
  415. //  }
  416. //  else {
  417. //      cout << "is person \n";
  418. //      Person *person_pointer = make_person(stream);
  419. //      p = person_pointer;
  420. //  }
  421. //  return in;
  422. //}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement