Guest

Untitled

By: a guest on Jan 28th, 2012  |  syntax: None  |  size: 3.41 KB  |  hits: 11  |  expires: Never
download  |  raw  |  embed  |  report abuse
Copied
  1. ======PointerTester.h==============
  2. #pragma once
  3. #include <string>
  4. #include <iostream>
  5. #include <vector>
  6. #include "Person.h"
  7. using namespace std;
  8.  
  9. class PointerTester
  10. {
  11. public:
  12.         PointerTester(void);
  13.         int findPerson(vector<Person*>, string);
  14. };
  15.  
  16.  
  17.  
  18.  
  19.  
  20.  
  21. ======PointerTester.cpp==============
  22. #include "Person.cpp"
  23. #include "Car.cpp"
  24. #include <string>
  25. #include <iostream>
  26. #include <iomanip>
  27. #include <vector>
  28. using namespace std;
  29. using std::cin;
  30. using std::cout;
  31.  
  32.  
  33. int main (){
  34.         string name;
  35.         string owner_name;
  36.         string driver_name;
  37.         Person *driver;
  38.         Person *owner;
  39.         string model;
  40.         int age;
  41.         int max;
  42.         vector <Car *> carList;
  43.         vector <Person *> personList;
  44.  
  45.         //Create people
  46.          cout << "Enter the driver's name and age:  (enter -1 for age to exit)";
  47.          cin >> name >> age;
  48.  
  49.          while (age!= -1){
  50.                 personList.push_back(new Person(name, age));
  51.                 cout << "Enter the driver's name and age:  (enter -1 for age to exit)";
  52.                 cin >> name >> age;
  53.          }//end while
  54.  
  55.          //Create cars
  56.                 cout << "Enter the model of the car:  " ;
  57.                 cin >> model;
  58.                 cout << "Enter the name of the owner:  ";
  59.                 cin >> owner_name;
  60.                 cout << "Enter the name of the driver:  (enter -1 for model to exit)  ";
  61.                 cin >>driver_name;
  62.  
  63.         while (driver_name != "-1"){
  64.                 int ownerPosition = findPerson(personList, owner_name);
  65.                 int driverPosition = findPerson(personList, driver_name);
  66.                 carList.push_back(new Car(model, personList[ownerPosition], personList[driverPosition]));
  67.  
  68.                 cout << "Enter the model of the car:  " ;
  69.                 cin >> model;
  70.                 cout << "Enter the name of the owner:  ";
  71.                 cin >> owner_name;
  72.                 cout << "Enter the name of the driver:  (enter -1 for model to exit)  ";
  73.                 cin >>driver_name;
  74.         }//end while
  75.  
  76.         //increment age of all Person objects
  77.  
  78.  
  79.         //Print information
  80.         int size = personList.size();
  81.         for(int k = 0; k < size; k++){
  82.                 carList[k]->toString();
  83.                 }//end displayfor
  84.         system("pause");
  85.  
  86.  
  87. //error 'findPerson' identifier not found
  88. //error personList undeclared
  89.  
  90. int PointerTester::findPerson(vector<Person *> people, string searchName) {
  91.         int size = personList.size();
  92.         for (int i = 0; i < size;i++){
  93.                 if (searchName == people[i]->getName()){
  94.                         return i;
  95.                 }//end if
  96.         }//end for
  97. }//end findPerson
  98.         return 0;
  99. }//end PersonTester
  100.  
  101.  
  102.  
  103. =====Person.h and .cpp========
  104. #include <string>
  105. #include <iostream>
  106. using namespace std;
  107.  
  108. class Person
  109. {
  110. public:
  111.         Person(string, int);
  112.         string getName();
  113.         int getAge();
  114.         string name;
  115.         int age;
  116. };
  117.  
  118.  
  119. #include "Person.h"
  120. #include <string>
  121. #include <iostream>
  122. using namespace std;
  123.  
  124.  
  125. Person::Person(string n, int a){
  126.         name = n;
  127.         age = age;
  128. }
  129.  
  130. string Person::getName(){
  131.         return name;
  132. }
  133.  
  134. int Person::getAge(){
  135.         return age;
  136. }
  137.  
  138.  
  139. =========Car.h and .cpp ==========
  140.  
  141. #pragma once
  142. #include <string>
  143. #include <iostream>
  144. #include "Person.h"
  145. using namespace std;
  146.  
  147. class Car
  148. {
  149. public:
  150.         Car(string, Person*, Person*);
  151.         void toString();
  152.  
  153.         string model;
  154.         Person *owner;
  155.         Person *driver;
  156.  
  157.  
  158. };
  159.  
  160. #include "Car.h"
  161. #include "Person.h"
  162. #include <string>
  163. #include <iostream>
  164. using namespace std;
  165.  
  166.  
  167. Car::Car(string type, Person* ownerPtr, Person* driverPtr){
  168.         model = type;
  169.         owner = ownerPtr;
  170.         driver = driverPtr;
  171. }
  172.  
  173. void Car::toString(){
  174.         cout << "Car Model: " + model;
  175.         cout << "Owner: " + owner->getName();
  176.         cout << "Owner's Age: " + owner->getAge();
  177.         cout << "Driver: " + driver->getName();
  178.         cout << "Driver's Age " + driver->getAge();
  179.         }