- ======PointerTester.h==============
- #pragma once
- #include <string>
- #include <iostream>
- #include <vector>
- #include "Person.h"
- using namespace std;
- class PointerTester
- {
- public:
- PointerTester(void);
- int findPerson(vector<Person*>, string);
- };
- ======PointerTester.cpp==============
- #include "Person.cpp"
- #include "Car.cpp"
- #include <string>
- #include <iostream>
- #include <iomanip>
- #include <vector>
- using namespace std;
- using std::cin;
- using std::cout;
- int main (){
- string name;
- string owner_name;
- string driver_name;
- Person *driver;
- Person *owner;
- string model;
- int age;
- int max;
- vector <Car *> carList;
- vector <Person *> personList;
- //Create people
- cout << "Enter the driver's name and age: (enter -1 for age to exit)";
- cin >> name >> age;
- while (age!= -1){
- personList.push_back(new Person(name, age));
- cout << "Enter the driver's name and age: (enter -1 for age to exit)";
- cin >> name >> age;
- }//end while
- //Create cars
- cout << "Enter the model of the car: " ;
- cin >> model;
- cout << "Enter the name of the owner: ";
- cin >> owner_name;
- cout << "Enter the name of the driver: (enter -1 for model to exit) ";
- cin >>driver_name;
- while (driver_name != "-1"){
- int ownerPosition = findPerson(personList, owner_name);
- int driverPosition = findPerson(personList, driver_name);
- carList.push_back(new Car(model, personList[ownerPosition], personList[driverPosition]));
- cout << "Enter the model of the car: " ;
- cin >> model;
- cout << "Enter the name of the owner: ";
- cin >> owner_name;
- cout << "Enter the name of the driver: (enter -1 for model to exit) ";
- cin >>driver_name;
- }//end while
- //increment age of all Person objects
- //Print information
- int size = personList.size();
- for(int k = 0; k < size; k++){
- carList[k]->toString();
- }//end displayfor
- system("pause");
- //error 'findPerson' identifier not found
- //error personList undeclared
- int PointerTester::findPerson(vector<Person *> people, string searchName) {
- int size = personList.size();
- for (int i = 0; i < size;i++){
- if (searchName == people[i]->getName()){
- return i;
- }//end if
- }//end for
- }//end findPerson
- return 0;
- }//end PersonTester
- =====Person.h and .cpp========
- #include <string>
- #include <iostream>
- using namespace std;
- class Person
- {
- public:
- Person(string, int);
- string getName();
- int getAge();
- string name;
- int age;
- };
- #include "Person.h"
- #include <string>
- #include <iostream>
- using namespace std;
- Person::Person(string n, int a){
- name = n;
- age = age;
- }
- string Person::getName(){
- return name;
- }
- int Person::getAge(){
- return age;
- }
- =========Car.h and .cpp ==========
- #pragma once
- #include <string>
- #include <iostream>
- #include "Person.h"
- using namespace std;
- class Car
- {
- public:
- Car(string, Person*, Person*);
- void toString();
- string model;
- Person *owner;
- Person *driver;
- };
- #include "Car.h"
- #include "Person.h"
- #include <string>
- #include <iostream>
- using namespace std;
- Car::Car(string type, Person* ownerPtr, Person* driverPtr){
- model = type;
- owner = ownerPtr;
- driver = driverPtr;
- }
- void Car::toString(){
- cout << "Car Model: " + model;
- cout << "Owner: " + owner->getName();
- cout << "Owner's Age: " + owner->getAge();
- cout << "Driver: " + driver->getName();
- cout << "Driver's Age " + driver->getAge();
- }