【发布时间】:2019-09-25 16:34:01
【问题描述】:
我得到了 main 并被告知创建一个程序来响应 main 使用 3 个类。游乐园班、骑手班和骑行班。 我想将骑手添加到游乐设施向量中,并将该向量存储在游乐园向量中。我究竟做错了什么?我该如何解决这个问题?
#include <iostream>
#include <string>
using namespace std;
class Rider
{
string name;
int height;
public:
Rider(string name, int height)
{
this->name=name;
this->height=height;
}
Rider(int height)
{
this->height=height;
}
};
class Ride
{
public:
vector <Rider> all_riders;
};
class Amusement_park
{
vector <Ride> all_rides;
public:
Amusement_park(int numRides)
{
all_rides[numRides];
}
vector <Rider> get_ride(int whichRide)
{
vector <Ride> the_ride= all_rides[whichRide];
return the_ride;
}
void add_line(class Rider)
{
the_ride.pushback(Rider);
}
};
int main()
{
Rider r1("Yaris",45); //name, height in inches
Rider r2(49); //height in inches
Amusement_park a1(3); //3 is the number of rides in the park
a1.get_ride(1).add_line(r1); //add a rider to the line of a ride
Amusement_park a2(2); //2 is the number of rides in the park
a2.get_ride(1).add_line(r2); //add a rider to the line of a ride
return 0;
}
【问题讨论】:
-
您可能想要为
Ride类添加一个名称。游乐园里的大多数游乐设施都有名字。
标签: c++ class oop object vector