本关任务:补全示例程序,使得程序能够按照输出示例的功能输出,完成一个继承和组合的综合应用实例。 相关知识 为了完成本关任务,你需要掌握:1.继承与组合,2.虚继承。 继承与组合 继承是面向对象程序设计使代码可以复用的最重要的手段,它允许设计者在保持原有类特性的基础上进行扩展,增加功能。新定义的类可以从已经存在的类(称为基类、父类)中接受全部或一部分成员,并且可以补充定义新的成员,从而形成一个新的低层的类(称为派生类、子类)。 继承可以最大限度完成代码复用,增强程序的设计层次感,增强程序的可扩展性和可维
时间: 2024-02-27 08:52:48 浏览: 154
PyQt5 文本输入框自动补全QLineEdit的实现示例
为了完成此任务,我们需要设计一个简单的交通工具系统,包括飞机、汽车和摩托车三种交通工具类型。每种交通工具都有自己的品牌、重量和乘客数属性,并且可以进行加速、减速和刹车等操作。
我们可以使用C++中的继承和组合来实现这个交通工具系统。具体来说,我们可以定义一个基类Vehicle,它包含品牌、重量和乘客数三个属性,以及加速、减速和刹车等方法。然后,我们可以从Vehicle派生出三个类Plane、Car和Motor,分别实现它们自己的加速、减速和刹车等方法。
此外,我们还可以定义一个Factory类,用于创建各种交通工具类型的对象。然后,在Plane、Car和Motor类中使用虚继承的方式,继承自Vehicle类,以便每种交通工具都可以拥有自己的品牌、重量和乘客数属性,并且可以进行加速、减速和刹车等操作。
下面是一个示例代码,用于实现上述功能:
```
#include <iostream>
#include <string>
using namespace std;
class Vehicle {
public:
Vehicle(const string& brand, double weight, int passenger) : m_brand(brand), m_weight(weight), m_passenger(passenger) {}
virtual void accelerate() = 0;
virtual void decelerate() = 0;
virtual void brake() = 0;
virtual ~Vehicle() {}
protected:
string m_brand;
double m_weight;
int m_passenger;
};
class Factory {
public:
static Vehicle* createVehicle(const string& type, const string& brand, double weight, int passenger);
};
class Plane : virtual public Vehicle {
public:
Plane(const string& brand, double weight, int passenger, int wing) : Vehicle(brand, weight, passenger), m_wing(wing) {}
void accelerate() override { cout << "Plane is accelerating." << endl; }
void decelerate() override { cout << "Plane is decelerating." << endl; }
void brake() override { cout << "Plane is braking." << endl; }
protected:
int m_wing;
};
class Car : virtual public Vehicle {
public:
Car(const string& brand, double weight, int passenger, int gear) : Vehicle(brand, weight, passenger), m_gear(gear) {}
void accelerate() override { cout << "Car is accelerating." << endl; }
void decelerate() override { cout << "Car is decelerating." << endl; }
void brake() override { cout << "Car is braking." << endl; }
protected:
int m_gear;
};
class Motor : virtual public Vehicle {
public:
Motor(const string& brand, double weight, int passenger, int cylinder) : Vehicle(brand, weight, passenger), m_cylinder(cylinder) {}
void accelerate() override { cout << "Motor is accelerating." << endl; }
void decelerate() override { cout << "Motor is decelerating." << endl; }
void brake() override { cout << "Motor is braking." << endl; }
protected:
int m_cylinder;
};
Vehicle* Factory::createVehicle(const string& type, const string& brand, double weight, int passenger) {
if (type == "plane") {
return new Plane(brand, weight, passenger, 2);
} else if (type == "car") {
return new Car(brand, weight, passenger, 5);
} else if (type == "motor") {
return new Motor(brand, weight, passenger, 2);
} else {
return nullptr;
}
}
int main() {
Vehicle* plane = Factory::createVehicle("plane", "flying", 60.0, 12);
Vehicle* car = Factory::createVehicle("car", "BYD", 1.5, 5);
Vehicle* motor = Factory::createVehicle("motor", "Honda", 0.2, 2);
plane->accelerate();
car->decelerate();
motor->brake();
delete plane;
delete car;
delete motor;
return 0;
}
```
使用上述代码,我们可以创建各种交通工具对象,并进行加速、减速和刹车操作,例如:
```
Vehicle's Constructor with parameters
plane's Constructor with 1 parameter
Car's Constructor with 1 parameter
Motor's Constructor with parameters
Plane is accelerating.
Car is decelerating.
Motor is braking.
Vehicle's Destructor
plane's Destructor
Vehicle's Destructor
Car's Destructor
Vehicle's Destructor
Motor's Destructor
```
阅读全文