[英]Understanding an array of pointers to an object in c++
There are 3 types of planes.有3种类型的飞机。
I want to index through the list and create an Airplane object and store the respective value.我想对列表进行索引并创建一个 Airplane object 并存储相应的值。 My for loop will create a new Airplane object that is set to 10. How can I dynamically set the respective plane types to the corresponding max capacity?
我的 for 循环将创建一个新的 Airplane object 并设置为 10。我如何动态地将各个飞机类型设置为相应的最大容量? The code currently outputs the address, for example:
代码目前输出地址,例如:
Airplane 1 maximum load 0xffe飞机 1 最大载荷 0xffe
I am trying to print the following:我正在尝试打印以下内容:
Airplane 1 maximum load 10
Airplane 2 maximum load 10
Airplane 3 maximum load 10
Airplane 4 maximum load 10
Airplane 5 maximum load 32
Airplane 6 maximum load 32
Airplane 7 maximum load 40
My code我的代码
#include <iostream>
using namespace std;
class Airplane
{
public:
Airplane(int n); // the maximum capacity of the airplane
int maxLoad(void) const;
int currentLoad(void) const;
bool addContainers(int n);
private:
const int maxContainers;
int numContainers;
};
Airplane::Airplane(int n):maxContainers(n){
n = maxContainers;
}
int Airplane::maxLoad(void) const{
return maxContainers;
}
int Airplane::currentLoad(void) const{
return numContainers;
}
class Airline
{
public:
Airline(int nA321, int nB777, int nB787);
~Airline(void);
void addShipment(int size);
void printSummary(void) const; // prints a list of airplanes with their current and maximum load.
private:
const int nAirplanes; // total # of airplanes used by airline
Airplane** airplaneList; // array of pointers to Airplane objects
};
Airline::Airline(int nA321, int nB777, int nB787):nAirplanes(nA321 + nB777 + nB787){
airplaneList = new Airplane*[nAirplanes];
for ( int i = 0; i < this->nAirplanes; i++ ){
airplaneList[i] = new Airplane(10);
airplaneList[i] -> maxLoad();
cout << "Airline " << i+1 << " maximum load " << airplaneList[i] << endl;
}
}
Airline::~Airline(void){}
int main(void)
{
// create an Airline with 4 A321, 2 B777 and 1 B787
Airline airline(4,2,1);
cout << "Assignment complete" << endl;
}
You can use runtime polymorphism:您可以使用运行时多态性:
Airplane
class virtual pure, so you cannot really instantiate an Airplane
.Airplane
class 纯虚拟,这样你就不能真正实例化一个Airplane
。 virtual int maxLoad() const = 0;
Airplane
class, one for each type of plane.Airplane
class 添加 3 个子类,每种类型的飞机一个。 These subclasses will define the maximum number of containers, for example, using an static inline const
member.static inline const
成员。 static inline const int maxContainers{10};
They will also implement the maxLoad
virtual method to return maxContainers
.他们还将实现
maxLoad
虚拟方法以返回maxContainers
。
virtual int maxLoad() const override { return maxContainers; }
airplaneList
as an array of Airplane*
, but then assign a specific plane instance to each of those pointers:airplaneList
保留为Airplane*
的数组,然后为每个指针分配一个特定的飞机实例: airplaneList = new Airplane*[nAirplanes];
int size = 0;
for (int i = 0; i < nA321; i++, size++) { airplaneList[size] = new A321(); }
for (int i = 0; i < nB777; i++, size++) { airplaneList[size] = new B777(); }
for (int i = 0; i < nB787; i++, size++) { airplaneList[size] = new B787(); }
Airplane
destructor virtual.Airplane
析构函数虚拟化。airplaneList
is an array of Airplane
pointers created through new
, at some point we should do a delete
of each of those pointers, and that is going to invoke the Airplane
destructor;airplaneList
是通过new
创建的Airplane
指针数组,在某些时候我们应该delete
每个指针,这将调用Airplane
析构函数; however, since we actually created instances of subclasses of Airplane
(eg with new A321()
), we need the destructor of the subclass to be executed.Airplane
子类的实例(例如使用new A321()
),我们需要执行子类的析构函数。Airplane
destructor is not virtual.Airplane
析构函数不是虚拟的,那将是不可能的。 virtual ~Airplane() {};
Airline
constructor is doing a new[]
for the airplane list, and a new
for each airplane. Airline
constructor 正在为飞机列表做一个new
new[]
,并为每架飞机做一个 new。 Airline
destructor should do the correspondings delete[]
and delete
. Airline
析构函数应该执行相应的delete[]
和delete
。Airline::~Airline()
{
for (int i = 0; i < nAirplanes; i++) { delete airplaneList[i]; }
delete[] airplaneList;
}
// Outputs:
//
// Airplane 1 (A321): maximum load = 10
// Airplane 2 (A321): maximum load = 10
// Airplane 3 (A321): maximum load = 10
// Airplane 4 (A321): maximum load = 10
// Airplane 5 (B777): maximum load = 32
// Airplane 6 (B777): maximum load = 32
// Airplane 7 (B787): maximum load = 40
// Assignment complete
As an aside note, using a std::vector
for the airplaneList
, and smart pointers (eg std::unique_ptr
) to the airplane instances would simplify the code a lot:附带说明一下,使用
std::vector
作为airplaneList
和指向飞机实例的智能指针(例如std::unique_ptr
)会大大简化代码:
class Airline
{
public:
Airline(int nA321, int nB777, int nB787);
~Airline() {}
void addShipment(int size);
void printSummary(void) const; // prints a list of airplanes with their current and maximum load.
private:
int nAirplanes{}; // total # of airplanes used by airline
std::vector<std::unique_ptr<Airplane>> airplaneList{}; // array of pointers to Airplane objects
};
Airline::Airline(int nA321, int nB777, int nB787)
: nAirplanes(nA321 + nB777 + nB787)
{
for (auto i = 0; i < nA321; i++) { airplaneList.emplace_back(std::make_unique<A321>()); }
for (auto i = 0; i < nB777; i++) { airplaneList.emplace_back(std::make_unique<B777>()); }
for (auto i = 0; i < nB787; i++) { airplaneList.emplace_back(std::make_unique<B787>()); }
for (size_t i = 0; i < airplaneList.size(); i++)
{
std::cout
<< "Airplane " << i+1
<< " (" << airplaneList[i]->getName() << ")"
<< ": maximum load = " << airplaneList[i]->maxLoad() << "\n";
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.