#include "stdafx.h"
#include <iostream>
#include <vector>
#include <string>
using namespace std;
/*
Person class with members name and age
*/
class Person
{
public:
/*
default constructor
*/
Person();
/*
creates Person object with parameters
@param input_name the name of the person
@param input_age the age of the person
*/
Person(string input_name, int input_age); // constructor with inputs
/*
method to read person data
@param input_name the name of the person
@param input_age the age of the person
*/
void read(string input_name, int input_age);
/*
method that returns the age of the person
@return returns the age of the person
*/
int get_age() const;
/*
method that returns the name of the person
@return returns the name of the person
*/
string get_name() const;
/*
increments the age of the person by 1 year
*/
void update_age();
private:
string name;
int age;
};
Person::Person()
{
name = " ";
age = 0;
}
Person::Person(string input_name, int input_age)
{
name = input_name;
age = input_age;
}
void Person::read(string input_name, int input_age)
{
name = input_name;
age = input_age;
}
int Person::get_age() const
{
return age;
}
string Person::get_name() const
{
return name;
}
void Person::update_age()
{
age = age + 1;
}
class Car
{
public:
/*
default constructor
*/
Car();
/*
creates an object of Car with parameters
@param input_model the model of the car
@param p_input_owner the owner of the car, type Person
@param p_input_driver the driver of the car, type Person
*/
Car(string input_model, Person* p_input_owner, Person* p_input_driver);
/*
method to read the car data
@param input_model the model of the car
@param p_input_owner the owner of the car, type Person
@param p_input_driver the driver of the car, type Person
*/
void read(string input_model, Person* p_input_owner, Person* p_input_driver);
/*
method that returns the model of the car
@return the model of the car
*/
string get_model() const;
private:
string model;
Person* p_owner;
Person* p_driver;
};
Car::Car()
{
model = " ";
p_owner = NULL;
p_driver = NULL;
}
Car::Car(string input_model, Person* p_input_owner, Person* p_input_driver)
{
model = input_model;
p_owner = p_input_owner;
p_driver = p_input_driver;
}
void Car::read(string input_model, Person* p_input_owner, Person* p_input_driver)
{
model = input_model;
p_owner = p_input_owner;
p_driver = p_input_driver;
}
string Car::get_model() const
{
return model;
}
int main()
{
int const NUMBER_OF_CARS = 3; // using a constant for the number of cars, can take this as input if desired
vector<Person*> people(NUMBER_OF_CARS * 2); // 2 people for each car, vector is 2 * NUMBER_OF_CARS
vector<Car*> cars(NUMBER_OF_CARS); // Sets the size of cars to the constant NUMBER_OF_CARS value
// declare variables
string input_name_owner;
string input_name_driver;
int input_age_owner = 0;
int input_age_driver = 0;
string remainder;
string input_model;
Person* temp_owner;
Person* temp_driver;
for(int i = 0; i < cars.size(); i++)
{
/*
use 3 variables to increment the vectors to accurately to input the car, owner and driver
Example of the math:
a = 0 1 2 3 The cars vector elements
a+b = 0 2 4 6 The people vector owner elements
a+b+c = 1 3 5 7 The people vector driver elements
no matter the size of the vectors, the input will populate the
vectors properly
*/
int a = i;
int b = i;
int c = 1;
// initialize all elements of the vectors
cars[a] = new Car();
people[a+b] = new Person();
people[a+b+c] = new Person();
// get user input
cout << "Enter the model of the car: ";
getline(cin, input_model);
cout << "Enter the owner's name: ";
getline(cin, input_name_owner);
cout << "Enter the owner's age: ";
cin >> input_age_owner;
getline(cin, remainder); // empty the remainder of the input string
cout << "\nEnter the driver's name: ";
getline(cin, input_name_driver);
cout << "Enter the driver's age: ";
cin >> input_age_driver;
getline(cin, remainder); // empty the remainder of the input string
cout << endl;
people[a+b]->read(input_name_owner, input_age_owner);
people[a+b+c]->read(input_name_driver, input_age_driver);
temp_owner = people[a+b];
temp_driver = people[a+b+c];
cars[a]->read(input_model, temp_owner, temp_driver);
}
for(int i = 0; i < cars.size(); i++)
{
/*
same theory for the output of the vector elements
*/
int a = i;
int b = i;
int c = 1;
cout << "Model: " << cars[a]->get_model() << endl;
cout << "Owner's name: " << people[a+b]->get_name() << endl;
cout << "Owner's age: " << people[a+b]->get_age() << endl;
cout << "Driver's name: " << people[a+b+c]->get_name() << endl;
cout << "Driver's age: " << people[a+b+c]->get_age() << endl;
cout << endl;
}
// increment ages of owners and drivers by 1
for (int i = 0; i < people.size(); i++)
{
people[i]->update_age();
}
// output the model, owner and driver with updated age
for(int i = 0; i < cars.size(); i++)
{
int a = i;
int b = i;
int c = 1;
cout << "Model: " << cars[a]->get_model() << endl;
cout << "Owner's name: " << people[a+b]->get_name() << endl;
cout << "Owner's age: " << people[a+b]->get_age() << endl;
cout << "Driver's name: " << people[a+b+c]->get_name() << endl;
cout << "Driver's age: " << people[a+b+c]->get_age() << endl;
cout << endl;
}
// delete unused pointers
for (int i = 0; i < cars.size(); i++)
{
delete cars[i];
}
for (int i = 0; i < people.size(); i++)
{
delete people[i];
}
// pause for user input
system("pause");
return 0;
}
好的,谢谢大家的帮助,这里是“完成”的代码。它有效,但可能并不漂亮。任务是创建一个 Car 类,它有 3 个成员,汽车的型号、车主、具有姓名和年龄的 Person* 类型的对象以及汽车的司机,还有一个具有姓名和年龄的 Person*。从用户那里获取输入并将它们存储在 Car* 和 Person* 的向量中(为什么我不知道指针),将司机的年龄增加一年并输出汽车和司机。
再次感谢您让我朝着正确的方向前进。
希望获得有关如何使代码更高效并且仍然符合练习标准的反馈。
我把它按原样交了。