使用指针将两个对象(每个对象与一个类)相关联

时间:2022-11-05 11:45:02

The goal of my program is to allow the user to enter up to 100 names for people and 100 names for cars. Then the user can "register" a car to as many people as s/he wishes using pointers. I know I need a person class and a car class and I need to use two arrays of size 100 for each. But I am completely lost on how to set anything else up. I have done a lot of researching to try and figure something out. I would be extremely appreciative if anyone could give me some basic example code for how something like this could be done.

我的程序的目标是允许用户输入最多100个人名和100个汽车名。然后,用户可以使用指针将汽车“注册”给他/她希望的人。我知道我需要一个人类和一个汽车类,我需要使用两个大小为100的数组。但我完全迷失了如何设置其他任何东西。我做了很多研究,试图找出一些东西。如果有人能给我一些基本的示例代码来完成这样的事情,我将非常感激。

I don't have much code:

我的代码不多:

class Person{public:    person();    Car* in_car;};class Car{public:    Car();};int main(){    Car cars[101];    Person people[101];}

1 个解决方案

#1


0  

You could add a vector of car pointers to the People class.

您可以向People类添加一个汽车指针向量。

class People{...private:vector<Car*> _pointer;};

#1


0  

You could add a vector of car pointers to the People class.

您可以向People类添加一个汽车指针向量。

class People{...private:vector<Car*> _pointer;};