Class Person

Feb 10, 2014 at 7:10pm
Implement a class Person with two fields name and age, and a class Car with three fields:

The model
A pointer to the owner (a Person*)
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. Traverse the vector of cars and print out the car model, owner’s name and age, and driver’s name and age.

Here is what I have so far I know this wrong, Im going through my textbook and working on this even after I post it. I was wondering if someone could take a look and least tell me if im heading in the right direction, Thank you for any help you can give.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <iostream>
#include <vector>
#include <conio.h>
#include <string>

using namespace std;

class Person
{
      string name;
      int age;
      };
class Car
{
      char16_t model;
      Owner* name;
      Driver* name;
      }
int main()
{
    vector<Person*>
    cout<<"Enter a Driver name and age: \n";
    cin>>Person*[i];
    cout<<"Enter a Owner name and age:\n";
    cin>>Person*[i];
    age = age+1;
    
    vector<Car*>
    cout<<"Enter a model number \n";
    cin>>Car*[i];
    
    cout<<Person*[i], Car*[i];
    
    system ("pause")
    return 0;
}
Feb 10, 2014 at 7:56pm
Members of a class object are private by default so you cannot access them outside the class unless through a public method that allows you to do so.
vector is a type therefore it needs a name when you declare one (line 21 and 28)
where is i defined? (lines 23, 25, 30, 32)
where is age defined? (line 26) Don't say inside Person class
Feb 10, 2014 at 8:00pm
Also why do you put *[i]?

Are you trying to dereference? If so it would be *variable[i]
Last edited on Feb 10, 2014 at 8:01pm
Feb 10, 2014 at 8:08pm
1
2
3
4
5
6
class Car
{
      char16_t model;
      Person *owner;
      Person *driver;
};
Feb 14, 2014 at 3:27pm
I have made all the necessary changes but it still wont compile

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>
#include <vector>
#include <conio.h>
#include <string>

using namespace std;

class Person
{
      string name;
      
      }
class Car
{
      char model;
      Person *owner;
      Person *driver;
      }
int age;
int main()
{
    vector<string>Person*
    cout<<"Enter a Driver name and age: \n";
    cin>>Person[i];
    cout<<"Enter a Owner name and age:\n";
    cin>>Person[i];
    
age = age+1;
    
    vector<string>Car*
    cout<<"Enter a model number \n";
    cin>>Car[i];
    
    cout<<Person[i], Car[i];
    
    system ("pause");

    return 0;
}
Feb 14, 2014 at 3:53pm
To access your class members - need to make them public...

1
2
3
4
5
6
class Person
{
  public:
    string name;
    int age;
};


Additionally, this might help...

Defining a vector
 
vector<Person> aPerson;


Adding a new "person" to your vector
1
2
3
4
Person tmpPerson;
tmpPerson.name = "Tom";
tmpPerson.age = 24;
aPerson.push_back(tmpPerson);


Retrieving your "person"
 
cout << aPerson[0].name;
Last edited on Feb 14, 2014 at 4:06pm
Feb 14, 2014 at 4:02pm
Pierreseoul wrote:
I have made all the necessary changes but it still wont compile


What necessary changes? And what compiler errors are you getting?
Feb 14, 2014 at 8:22pm
Also for POD(Plain-old-data) types most people use structs.
Topic archived. No new replies allowed.