I had created a Person class with 2 fields ages and name, its member functions are
Person::Person(string n, int a){
name = n;
age = a;}
string Person::get_name() const{
return name;}
void Person::increment_age(){
age += 1;}
void Person::print() const{
cout << name << endl;
cout << age << endl;}
And also a Cars class with fields: But I think my print() function is wrong.
private:
string model;
Person* owner;
Person* driver;
Cars' class member function:
Car::Car(string m){
model = m;}
void Car::set_driver(Person* p){
*driver = *p;}
void Car::set_owner(Person* p){
*owner = *p;}
void Car::print() const{
cout << model << endl;
cout << &driver << endl;
cout << &owner << endl;}
I want to fill a vector of Person pointers using names and ages from arrays
vector<Person*> people;
const int PERSON_SZ = 4;
char * names[] = {"Jim", "Fred", "Harry", "Linda"};
int ages[] = { 23, 35, 52, 59 };
for (int i = 0; i < PERSON_SZ; i++){
Person *a = new Person(names[i], ages[i]);
people.push_back(a);}
And I also want to fill a vector of Cars pointers using the model from an array, then set driver and owner from the people vector. Is this right?
vector<Car*> cars;
const int CAR_SZ = 3;
char * models[] = { "Festiva", "Ferrarri", "Prius" };
for (int i = 0; i < CAR_SZ; i++)
{
Car *c = new Car(models[i]);
c->set_driver(people[rand()% (people.size())]);
c->set_owner(people[rand()% (people.size())]);
cars.push_back(c);
}
And finally I need to Print out each car in the cars vector, including the name and age of each driver, but i think my print() function is not correct, therefore I wouldn't run my code. Please help.