#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;
}
わかりました、すべての助けに感謝します。これが「完成した」コードです。動作しますが、おそらくきれいではありません。割り当ては、車のモデル、所有者、名前と年齢を持つ Person* 型のオブジェクト、車のドライバー、名前と年齢を持つ Person* の 3 つのメンバーを持つ Car クラスを作成することでした。ユーザーから入力を受け取り、それらを Car* と Person* のベクトルに格納し (なぜポインタがわからないのか)、ドライバーの年齢を 1 年ずつ増やして、車とドライバーを出力します。
繰り返しますが、私を正しい方向に向けてくれてありがとう。
コードをより効率的にし、演習の基準を維持する方法についてのフィードバックをお待ちしています。
そのまま入れましたが。