C++ Program Implement a class Person with two attributes name and age , and a cl
ID: 3870922 • Letter: C
Question
C++ Program
Implement a class Person with two attributes name and age , and a class Car with three fields:
the model
a pointer to the owner (a Person*)
a pointer to driver (also a Person*)
The class Person must have a function called incrementAge. The function increments the age by one. Also Create a function to find a person with the given name in an array of Person* pointer.
/**
@param a the array
@param n the name to look for
@return the first matching Person* or NULL if there is no match (I mainly need help with this working. I asked this question before and making it null is not working, so please help me with this one mainly)
*/
Person* find(vector<Person*> a, string n)
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 cars and print out the car model, owner's name and , and driver's name and age.
Use vector to store the objects Car and objects Person.
Output sample
Enter name, q to quit: John Nguyen
Enter age: 30
Enter name, q to quit: Ralph Bravaco
Enter age: 50
Enter name, q to quit: q
Enter model, q to quit: Tesla model S
Enter owner name: John Nguyen
Enter driver name: Sahi Simonson
Enter model, q to quit: Tesla model X
Enter owner name: Timonthy Budd
Enter driver name: John Nguyen
Enter model, q to quit: q
Car model: Tesla model S,owner=John Nguyen,age=31,driver=NULL.
Car model: Tesla model X,owner=NULL,driver=John Nguyen,age=31.
Problem 2 (Seperate Program)
Create a function to copy a portion of a string to another.
@param t a pointer to the start of the target string
@param s a pointer to the start of the source string
@param n the maximum number of characters to copy
Write a main function to test the string.
Problem 3 (Seperate Program)
Create a function that reverses the values in an array passing to the function. Use pointers to implement the functions.
@param a the array
@param size the number of elements in the array
Explanation / Answer
#include <iostream>
#include <cstdlib>
#include <vector>
using namespace std;
class Person // Define person class
{
private:
string name;
int age;
public:
Person(string n, int a);
string getName()const;
int getAge()const;
void incrementAge();
void print()const;
};
Person::Person(string n, int a)
{
name = n;
age = a;
}
string Person::getName() const
{
return name;
}
void Person::incrementAge() //Function to Increment age
{
age += 1;
}
void Person::print() const{
cout << name << ", Age = " << age;
}
class Car // Define car class
{
private:
string model;
Person *owner;
Person *driver;
public:
Car(string m);
void set_owner(Person* p); //Pointer to owner
void set_driver(Person* p); //Pointer to driver
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 << endl;
cout << "Car model : ";
cout << model << ", ";
cout << "Owner = "; owner->print();
cout << ", ";
cout << "Driver = "; driver->print();
cout << endl;
}
Person* find(vector<Person*> a, string n)
{
for(int i = 0; i < a.size(); i++)
{
if(a.at(i)->getName() == n)
{
return a.at(i);
}
}
return NULL;
}
int main() //Main Method
{
vector <Person> person;
string name;
int age;
cout<<"Enter name, q to quit: ";
cin>>name;
while(name != "q")
{
cout<<"enter the age of "<<name<<": ";
cin>>age;
Person *a = new Person(name, age);
person.push_back(*a);
cout<<"Enter name, q to quit: ";
cin>>name;
}
vector<Car> car;
string model;
cout<<"Enter model, q to quit: ";
cin>>model;
while(model != "q")
{
Car *c = new Car(model);
string name;
cout<<"Enter owner name: ";
cin>>name;
for(int i = 0; i < person.size(); i++)
if(person.at(i).getName() == name)
c->set_owner(&person.at(i));
cout<<"Enter driver name: ";
cin>>name;
for(int i = 0; i < person.size(); i++)
if(person.at(i).getName() == name)
c->set_driver(&person.at(i));
car.push_back(*c);
cout<<"Enter model, q to quit: ";
cin>>model;
}
for( int i = 0; i < person.size(); i++ )
{
(person[i]).incrementAge();
}
for( int i = 0; i < car.size(); i++ )
{
(car[i]).print();
}
system("PAUSE");
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.