need some help about the program i don't know what is the error in this program anyone can tell me plz

#include <iostream>
#include <string>
#include <vector>

using namespace std;

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;
};


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;}




int main()
{

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);}




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);
}
return 0;
}
Please post the compiler error or describe the problem--help us, help you. Don't forget that this is your problem, not ours...
1
2
3
4
5
6
7
8
9
10
11
12
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;
}; 


----------------------------------------------------------------------------------------------------

1
2
3
4
5
6
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 


----------------------------------------------------------------------------------------------------

1
2
3
4
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 

Try:
1
2
3
4
5
void Car::print() const{
cout << model << endl;
//dereference and invoke get_name()
cout << driver->get_name() << endl;
cout << owner->get_name() << endl;}
Last edited on
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.
Last edited on
You need to invoke the print method in order to print the information.
Did you add that call to the main function?
Topic archived. No new replies allowed.