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.

Recommended Answers

All 11 Replies

narrow your explanations to target the problem.
I have not yet understood what you want. Any error? Compiles?

Your definition of class Car is definitely wrong.

The Car constructor does not initialize driver or owner, but the set_driver and set_owner functions assume that driver or owner (respectively) have been initialized.

I am guessing that line 5 of Car should be

driver = p;

and line 8 should be

owner = p;

Also, the & should be removed from lines 11 and 12, and you will need to define an operator<< for class Person. Either that or change lines 11 and 12 to use the print function that you defined.

I am trying to make the main() function work from the declared classes
The class definitions were given, I was trying to implement them so that they will work for the main() function and after run the program, it will print each car in the cars vector, including the name and age of each driver.

Well, unless there's more to the Car class than you've shown us, I don't see how it can be made to work.

I change the code according to your post, and when i tried to print(), i got the model and 2 addresses for owner and driver. Is there any method to get it print out the correspond names and ages ?

No, because the pointers don't point anywhere legitimate. The Car class as you've posted it is broken. You need to fix it before it will work.

Can you tell me what is broken? I really have no idea how to fix it. Thanks.

I already told you what is broken, when I said:

The Car constructor does not initialize driver or owner, but the set_driver and set_owner functions assume that driver or owner (respectively) have been initialized.

That means that if you execute

Car c;
c.set_driver(a_person);

the effect will be undefined. And I do not see any way to solve this problem without changing the defintion of the Car class.

You responded by saying that you were not allowed to change the definition of the Car class.

Therefore, I do not see how to solve your problem. I doubt a solution exists.

So if I tried to add a default constructor for the Car class then, how can I get the driver or owner from the Car object?

You have to decide what you want the data variables "driver" and "owner" to mean. Right now you are using them as if they are pointers to objects that exist somewhere (because when you write *driver = something, that is what you are doing), but you never create those objects, nor do you make your "driver" variable point to any of them.

So before you do anything else, you have to decide how the class is supposed to work.

I already told you what is broken, when I said:

That means that if you execute

Car c;
c.set_driver(a_person);

the effect will be undefined. And I do not see any way to solve this problem without changing the defintion of the Car class.

You responded by saying that you were not allowed to change the definition of the Car class.

Therefore, I do not see how to solve your problem. I doubt a solution exists.

Thank for your responses, I actually figured how to make it work. I just have to implement
driver->print() and owner->print() inside the Car::print()

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.