class Person
{
private:
string name;
int age;
public:
Person(string n, int a);
string get_name()const;
int get_age()const;
void Person::increment_age();
void print()const;
};
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;}
class Car
{
private:
string model;
Person *owner;
Person *driver;
public:
Car::Car(string m);
void Car::set_driver(Person* p);
void Car::set_owner(Person* p);
void print()const;
};
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);
}
return 0;
}
class Person
{
private:
string name;
int age;
public:
Person(string n, int a);
string get_name()const;
int get_age()const;
void Person::increment_age(); //is inside the class body, you don't need the prefixed Person::void print()const;
};
1 2 3 4 5 6 7 8 9 10 11 12
class Car
{
private:
string model;
Person *owner;
Person *driver;
public:
Car::Car(string m); //Again, same thing.
void Car::set_driver(Person* p); //and here
void Car::set_owner(Person* p); //and also here
void print()const;
};
void Car::set_driver(Person* p){
*driver = *p;} //This doesn't make sense! The L-value (left side of the = operator) could just be a variable.
// (Unless you actually wanted to copy the data in p, but I don't think that's what you want here)
void Car::set_owner(Person* p){
*owner = *p;} //same thing here
Try:
1 2 3 4 5 6
void Car::set_driver(Person* p){
driver = p;}
void Car::set_owner(Person* p){
owner = p;}
//this is fine since "dirver" and "owner" are of type Person* also
void Car::print() const{
cout << model << endl;
cout << &driver << endl; //You are printing the address of the pointer, you want to dereference it instead.
cout << &owner << endl;} //here too
Mathhead200 thanks for your help but the program is compiling successfully but it isn't giving me any output.
i post the problem in bottom that i'm trying to solve plz help me. bcoz i don't now more than this what i done in above code.
Implement a class Person with two fields name and age, and a class Car with three fields:
(1). the model, (2). a pointer to the owner (aPerson*), (3). a pointer to the driver (also a Person)
Write a program that prompts the user to specify people and cars. Store them in a vector<Person*> and a vector<Car*>. Traverse the vector of Person objects and increment their ages by one year. Finally, traverse the vector of cars and print out the car model, owner’s name and age, and driver’s name and age.