Advertisement
jordno

Coursework update

Dec 4th, 2015
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.66 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <sstream>
  4. #include <vector>
  5.  
  6. using namespace std;
  7.  
  8. class Person
  9. {
  10. protected:
  11.         string forename, surname, email;
  12.  
  13. public:
  14.         Person(){}
  15.         Person(string s, string f):surname(s), forename(f){}
  16.         string get_surname() const//get the persons surname
  17.         {
  18.                 return surname;
  19.         }
  20.  
  21.         string get_forename() const//get the persons first name
  22.         {
  23.                 return forename;
  24.         }
  25.  
  26.         virtual bool has_telephone() const{ return false; } //returns true if person has a telephone number
  27.  
  28.         virtual bool has_email() const { return false; }//returns true if the person has a email
  29.  
  30.         virtual string get_email() const { return ""; };//gets the persons email
  31.         virtual string get_telephone() const { return ""; };//gets the persons telephone number
  32.  
  33.         friend bool operator==(Person &person1, Person &person2);
  34.  
  35.         virtual void type() const //persons class type for later functions
  36.         {
  37.                 cout << "person" << endl;
  38.         };
  39. };
  40. //----------------------------------------------------------------------------------------------------
  41. //Override the == comparator to compare person objects
  42. //bool operator== (Person &person1, Person &person2)
  43. //{
  44. //      cout << person1.get_surname() << " " << person2.get_surname();
  45. //      return ((person1.forename == person2.forename) && (person1.surname == person1.surname));
  46. //}
  47.  
  48. //person with telephone class with class person inheritance
  49. class Person_with_telephone : public virtual Person
  50. {
  51. protected:
  52.         string telephone_number;
  53.  
  54. public:
  55.         Person_with_telephone(){}
  56.         Person_with_telephone(string s, string f, string t) : Person(s, f), telephone_number(t){}
  57.  
  58.         bool has_telephone() const {
  59.         return !telephone_number.empty();
  60.         }
  61.  
  62.         virtual string get_telephone() const//get persons telephone number
  63.         {
  64.                 return telephone_number;
  65.         }
  66.  
  67.         void set_telephone(string &t)//set classes telephone number
  68.         {
  69.                 telephone_number = t;
  70.         }
  71.  
  72.         virtual void type() const //class type shown as t for telephone
  73.         {
  74.                 cout << "person w/ t" << endl;
  75.         };
  76. };
  77. //----------------------------------------------------------------------------------------------------
  78. class Person_with_email : public virtual Person
  79. {
  80. protected:
  81.         string email;
  82.  
  83. public:
  84.         Person_with_email(){}
  85.         Person_with_email(string s, string f, string e) :Person(s, f), email(e){}
  86.  
  87.         bool has_email() const
  88.         {
  89.                 return !email.empty();
  90.         }
  91.  
  92.         virtual string get_email() const
  93.         {
  94.                 return email;
  95.         }
  96.  
  97.         void set_email(string &e)
  98.         {
  99.                 email = e;
  100.         }
  101.  
  102.         virtual void type() const
  103.         {
  104.                 cout << "person w/ e" << endl;
  105.         };
  106. };
  107. //----------------------------------------------------------------------------------------------------
  108. class Person_with_telephone_email : public Person_with_telephone, public Person_with_email{
  109. public:
  110.         Person_with_telephone_email(){}
  111.         Person_with_telephone_email(string s, string f, string t, string e) : Person(s, f), Person_with_telephone(s, f, t), Person_with_email(s, f, e) {}
  112.  
  113.         virtual void type() const
  114.         {
  115.                 cout << "person w/ t + e" << endl;
  116.         };
  117.  
  118. };
  119. //----------------------------------------------------------------------------------------------------
  120. ostream& operator<<(ostream &os, Person &p)
  121. {
  122.         if (p.has_email() && p.has_telephone())
  123.     {
  124.                 cout << "<person" << " S " << p.get_surname() << " F " << p.get_forename() << " E " << p.get_email() << " T " << p.get_telephone() << " >";
  125.         }
  126.         else if (p.has_email())
  127.         {
  128.                 cout << "<person" << " S " << p.get_surname() << " F " << p.get_forename() << " E " << p.get_email() << " >";
  129.         }
  130.         else if (p.has_telephone())
  131.         {
  132.                 cout << "<person" << " S " << p.get_surname() << " F " << p.get_forename() << " T " << p.get_telephone() << " >";
  133.         }
  134.         else
  135.         {
  136.                 cout << "<person" << " S " << p.get_surname() << " F " << p.get_forename() << " >";
  137.         }
  138.         return os;
  139. }
  140. //----------------------------------------------------------------------------------------------------
  141. istream& operator>>(istream &is, Person &p){
  142.         string opening, s, surname, f, forename, closing;
  143.         if (
  144.                 (is >> opening >> s >> surname >> f >> forename >> closing)
  145.                 &&
  146.                 ((opening == "<person") && (s == "S") && (f == "F") && (closing == ">"))
  147.                 )
  148.         {
  149.                 p = Person(surname, forename);
  150.         }
  151.         return is;
  152. }
  153.  
  154. istream& operator>>(istream &is, Person_with_email &p){
  155.         string opening, s, surname, f, forename, e, email, closing;
  156.         if (
  157.                 (is >> opening >> s >> surname >> f >> forename >> e >> email >> closing)
  158.         &&
  159.                 ((opening == "<person") && (s == "S") && (f == "F") && (e == "E") && (closing == ">"))){
  160.                 p = Person_with_email(surname, forename, email);
  161.         }
  162.         return is;
  163. }
  164.  
  165. istream& operator>>(istream &is, Person_with_telephone &p){
  166.         string opening, s, surname, f, forename, t, telephone, closing;
  167.         if (
  168.                 (is >> opening >> s >> surname >> f >> forename >> t >> telephone >> closing) &&
  169.                 ((opening == "<person") &&
  170.                 (s == "S") &&
  171.                 (f == "F") &&
  172.                 (t == "T") &&
  173.                 (closing == ">"))
  174.                 )
  175.         {
  176.                 p = Person_with_telephone(surname, forename, telephone);
  177.         }
  178.         return is;
  179. }
  180.  
  181. istream& operator>>(istream &is, Person_with_telephone_email &p){
  182.         string opening, s, surname, f, forename, t, telephone, e, email, closing;
  183.         if (
  184.                 (is >> opening >> s >> surname >> f >> forename >> t >> telephone >> e >> email >> closing) &&
  185.                 ((opening == "<person") &&
  186.                 (s == "S") &&
  187.                 (f == "F") &&
  188.                 (t == "T") &&
  189.                 (e == "E") &&
  190.                 (closing == ">"))
  191.                 )
  192.         {
  193.                 p = Person_with_telephone_email(surname, forename, telephone, email);
  194.         }
  195.         return is;
  196. }
  197.  
  198. Person* make_person(istream &stream){
  199.         Person *person_pointer = new Person;
  200.         stream >> *person_pointer;
  201.         return person_pointer;
  202. }
  203.  
  204. Person_with_email* make_person_email(istream &stream){
  205.         Person_with_email *person_pointer = new Person_with_email;
  206.         stream >> *person_pointer;
  207.         return person_pointer;
  208. }
  209.  
  210. Person_with_telephone* make_person_telephone(istream &stream){
  211.         Person_with_telephone *person_pointer = new Person_with_telephone;
  212.         stream >> *person_pointer;
  213.         return person_pointer;
  214. }
  215.  
  216. Person_with_telephone_email* make_person_telephone_email(istream &stream){
  217.         Person_with_telephone_email *person_pointer = new Person_with_telephone_email;
  218.         stream >> *person_pointer;
  219.         return person_pointer;
  220. }
  221.  
  222. istream & read_person(istream &in, Person * & p){
  223.         string input;
  224.         getline(in, input);
  225.         stringstream stream(input);
  226.         string e = " E ";
  227.         string t = " T ";
  228.         bool has_e = false;
  229.         bool has_t = false;
  230.  
  231.         if (input.find(e) != std::string::npos){ has_e = true; }
  232.         if (input.find(t) != string::npos){ has_t = true; }
  233.  
  234.         if (has_e && has_t) {
  235.                 cout << "e + t\n";
  236.                 Person_with_telephone_email *person_pointer = make_person_telephone_email(stream);
  237.                 p = person_pointer;
  238.         }
  239.         else if (has_e){
  240.                 cout << "e\n";
  241.                 Person_with_email *person_pointer = make_person_email(stream);
  242.                 p = person_pointer;
  243.         }
  244.         else if (has_t){
  245.                 cout << "t \n";
  246.                 Person_with_telephone *person_pointer = make_person_telephone(stream);
  247.                 p = person_pointer;
  248.         }
  249.         else{
  250.                 cout << "is person \n";
  251.                 Person *person_pointer = make_person(stream);
  252.                 p = person_pointer;
  253.         }
  254.         return in;
  255. }
  256. //----------------------------------------------------------------------------------------------------
  257. /**
  258.         Container class for storing and handling objects of class Person
  259. */
  260. template <typename Contact>
  261. class Contacts {
  262.  
  263. private:
  264.         vector<Person*> data;
  265.         int data_size;
  266.  
  267. public:
  268.         Contacts() {
  269.                 data_size = 0;
  270.         }
  271.  
  272.         void add(Person p ) {
  273.                 data.push_back(&p);
  274.                 data_size++;
  275.         }
  276.  
  277.         void remove(int i) {
  278.                 data.erase(data.begin() + (i - 1));
  279.                 data_size--;
  280.         }
  281.  
  282.  
  283.  
  284.         Contact get(int i) {
  285.                 return data[i];
  286.         }
  287.  
  288.         int get_size() {
  289.                 return data.size();
  290.         }
  291.  
  292.         ~Contacts() {
  293.  
  294.                 data.clear();
  295.         }
  296. };
  297. //----------------------------------------------------------------------------------------------------
  298. //this is main
  299. int main(){
  300. //Tests the == compare
  301. //      Person *p = new Person("Dave", "Harry");
  302. //      Person *s = new Person("Dave", "Harry");
  303. //      Person *d = new Person("Dave", "Johns");
  304.         // if (p == s)
  305.         // {
  306.         //      cout << "They're the same!\n";
  307.         // }+
  308. //Tests the read_person istream
  309. //    Person *p = 0;
  310. //      string ss = "<person S Smith N Tom >\n<person S Smith N Dick T +49.921.1434 >\n<person S Smith N Harry E hsmith@gmail.com >\n<person S Smith N Mary T +39.921.1434 E msmith@gmail.com >\n<person S John N John T + 33.921.1434 E jjohn@gmail.com >";
  311. //      stringstream iss(ss);
  312. //      while (read_person(iss, p) && p)
  313. //              cout << *p << endl;
  314.  
  315. //Tests the output and input of the vector
  316.     Contacts <Person> c;
  317.  
  318.     cout << c.get_size() << endl;
  319.         Person john1("John","Smith");
  320.         c.add(john1);
  321.         Person_with_telephone john2("John", "Tim","+02958.5645764.75");
  322.         c.add(john2);
  323.         Person_with_email john3("John", "Jones","email@email.com");
  324.         c.add(john3);
  325.         Person_with_telephone_email john4 ("John", "Adams", "+33.02.0101.0202", "email@email.com");
  326.         c.add(john4);
  327.  
  328.         //c.get causing errors
  329.         cout << c.get(1) << endl << c.get(2) << endl << c.get(3) << endl;
  330.         cout << c.get_size() << endl;
  331. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement