Advertisement
jordno

C++ Coursework

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