Advertisement
Guest User

Untitled

a guest
Sep 11th, 2013
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.23 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <algorithm>
  5.  
  6. using namespace std;
  7.  
  8. // Start Person Class
  9. class Person
  10. {
  11. public:
  12.  
  13.     Person(string n, int a);
  14.     string getName();
  15.     int getAge();
  16.     void incrementAge();
  17.     void print();
  18.  
  19. private:
  20.     string name;
  21.     int age;
  22.  
  23. };
  24.  
  25. Person::Person(string n, int a)
  26. {
  27.     name = n;
  28.     age = a;
  29. }
  30.  
  31. string Person::getName()
  32. {
  33.     return name;
  34. }
  35.  
  36. int Person::getAge()
  37. {
  38.     return age;
  39. }
  40.  
  41.  
  42. void Person::incrementAge(){
  43.     age += 1;
  44. }
  45.  
  46. void Person::print()
  47. {
  48.     cout << name << endl;
  49.     cout << age << endl;
  50. }
  51. // Person Class END
  52.  
  53. // Car Class Begin
  54. class Car
  55. {
  56. public:
  57.  
  58.     Car() : model(), owner(), driver(){}
  59.     Car(string s, Person*o, Person*d) : model(s), owner(o), driver(d){}
  60.     void print();
  61.  
  62.  
  63. private:
  64.  
  65.     string model;
  66.     Person *owner;
  67.     Person *driver;
  68. };
  69.  
  70.  
  71. void Car::print()
  72. {
  73.     cout << model << endl;
  74.     cout << "Driver: "; driver->print();
  75.     cout << "Owner: "; owner->print();
  76. }
  77. //End of Car Class
  78.  
  79. Person*find(string n, vector<Person>&people)
  80. {
  81.     for (int i = 0; i < people.size(); i++)
  82.     {
  83.         if (n == people[i].getName())
  84.             return &(people[i]);
  85.         else
  86.             return 0;
  87.     }
  88. };
  89.  
  90.  
  91. // Begin Main Function
  92. int main()
  93. {
  94.     vector<Person*>people;
  95.     vector<Car*> cars;
  96.     string name = "";
  97.     Car * d;
  98.     Car * o;
  99.     string person_owner;
  100.     string person_driver;
  101.     int age = 0;
  102.     string model;
  103.     int testValue = 0;
  104.  
  105.     while (testValue != -1)
  106.     {
  107.         //Start Input for People
  108.         cout << "Enter Driver Name: ";
  109.         cin >> name;
  110.         cout << "Enter Age: ";
  111.         cin >> age;
  112.         people.push_back(new Person(name, age));
  113.  
  114.         // Start Input for Cars
  115.         cout << "Enter car model, owner name, driver name: ";
  116.         cin >> model >> person_owner >> person_driver;
  117.        
  118.         d = find(person_owner, people); //find doesn't work
  119.         o = find(person_driver, people); //same here
  120.  
  121.         cars.push_back(new Car(model, o, d));
  122.         cin >> model >> person_owner >> person_driver;
  123.  
  124.         cout << "Press -1 to stop, or 1 to enter info for others: ";
  125.         cin >> testValue;
  126.     }
  127.  
  128.     //Output info  
  129.  
  130.     for (int i = 0; i < people.size(); i++)
  131.     {
  132.         people[i]->incrementAge();
  133.         cout << "Name: " << (*people[i]).getName() << endl;
  134.         cout << "Age: " << (*people[i]).getAge() << endl;
  135.     }
  136.    
  137.  
  138.     system("PAUSE");
  139.     return 0;
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement