当前位置:网站首页>声明一个抽象类Vehicle,它包含私有变量numOfWheels和公共函数Vehicle(int)、Horn()、setNumOfWheels(int)和getNumOfWheels()。子类Mot
声明一个抽象类Vehicle,它包含私有变量numOfWheels和公共函数Vehicle(int)、Horn()、setNumOfWheels(int)和getNumOfWheels()。子类Mot
2022-07-01 12:44:00 【laocooon】
声明一个抽象类Vehicle,它包含私有变量numOfWheels和公共函数Vehicle(int)、Horn()、setNumOfWheels(int)和getNumOfWheels()。子类Motorcycle和Truck通过公共继承从类Vehicle派生而来。
Motorcycle有一个私有成员布尔变量isElectricity,以及四个公共函数setType(bool)、getType()、ChangeType()和Horn()。ChangeType()可以为isElectricity赋一个相反的值,而Horn()可以输出一个字符串“嘟嘟嘟,嘟嘟嘟”。
Truck有一个私有成员变量load,以及四个公共成员函数构造函数Truck(float)、setLoad(float)、getLoad()和Horn()。对于类Truck, Horn()可以输出一个字符串" longlong, longlong "。
在main()函数中,分别为子类创建对象mm、tt,并通过引用Vehicle调用Horn()来演示面向对象编程的多态性。
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;
//声明一个抽象类Vehicle,
//它包含私有变量numOfWheels和公共函数Vehicle(int)、Horn()、setNumOfWheels(int)和getNumOfWheels()。
class Vehicle
{
protected:
int numOfWheels;//几轮
public:
Vehicle(int n)
{
numOfWheels = n;
}
virtual void Horn() = 0;
virtual void setNumOfWheels(int) = 0;
virtual int getNumOfWheels() = 0;
};
//子类Motorcycle和Truck通过公共继承从类Vehicle派生而来。
class Motorcycle :public Vehicle
{
private:
bool isElectricity;
public:
Motorcycle(int n = 2) :Vehicle(n) { isElectricity = false; };
void setType(bool b) { isElectricity=b; }
bool getType() { return isElectricity; }
void ChangeType() { isElectricity =! isElectricity;}
void setNumOfWheels(int n) { numOfWheels = n; }
int getNumOfWheels() { return numOfWheels; }
void Horn() { cout << "dududu, dududu" << endl; }
};
class Truck :public Vehicle
{
private:
float load;
public:
Truck(float load,int n=4):Vehicle(n) { this->load = load; }
void setLoad(bool load) { this->load = load ; }
bool getLoad() { return load; }
void setNumOfWheels(int n) { numOfWheels = n; }
int getNumOfWheels() { return numOfWheels; }
void Horn() { cout << "longlong, longlong" << endl; }
};
int main()
{
Motorcycle mm;
mm.setNumOfWheels(2);
mm.setType(true);
Truck tt(12.0);
tt.setNumOfWheels(4);
Vehicle *v;
v = &mm;
v->Horn();
v = &tt;
v->Horn();
return 0;
}
边栏推荐
- 【牛客刷题-SQL大厂面试真题】NO2.用户增长场景(某度信息流)
- 数论基础及其代码实现
- Development trend and market demand analysis report of China's high purity copper industry Ⓕ 2022 ~ 2028
- Manage nodejs with NVM (downgrade the high version to the low version)
- VS Code 设置代码自动保存
- QT 播放器之列表[通俗易懂]
- Fiori 应用通过 Adaptation Project 的增强方式分享
- Application of stack -- bracket matching problem
- 微信小程序 – 80个实用的微信小程序项目实例
- 79. Word search [DFS + backtracking visit + traversal starting point]
猜你喜欢

VS Code 设置单击打开新文件窗口,不覆盖前一个窗口

基于开源流批一体数据同步引擎 ChunJun 数据还原 —DDL 解析模块的实战分享
![leetcode:226. Flip binary tree [DFS flip]](/img/b8/6c5596ac30de59f0f347bb0bddf574.png)
leetcode:226. Flip binary tree [DFS flip]

Fiori 应用通过 Adaptation Project 的增强方式分享

Zabbix 6.0 源码安装以及 HA 配置

Chained storage of queues

Queue operation---
![[encounter Django] - (II) database configuration](/img/13/9512c1e03349092874055771c3433d.png)
[encounter Django] - (II) database configuration

be based on. NETCORE development blog project starblog - (13) add friendship link function

Fundamentals of number theory and its code implementation
随机推荐
腾讯总考epoll, 很烦
PG基础篇--逻辑结构管理(触发器)
Flinkcdc should extract Oracle in real time. What should be configured for oracle?
What are the solutions for session sharing of highly paid programmers & interview questions series 118?
Digital signal processing -- Design of linear phase (Ⅱ, Ⅳ) FIR filter (2)
Chained storage of queues
【牛客刷题-SQL大厂面试真题】NO2.用户增长场景(某度信息流)
天青色等烟雨
单点登录SSO与JWT好文整理
R language uses conf of yardstick package_ The mat function calculates the confusion matrix of the multiclass model on each fold of each cross validation (or resampling), and uses the summary to outpu
软件测试中功能测试流程
Zabbix 6.0 源码安装以及 HA 配置
高薪程序员&面试题精讲系列118之Session共享有哪些方案?
逆向调试入门-PE结构-输入表输出表05/07
华为HMS Core携手超图为三维GIS注入新动能
How can genetic testing help patients fight disease?
Wang Xing's infinite game ushers in the "ultimate" battle
leetcode:241. Design priority for operation expression [DFS + Eval]
Like the three foot platform
Operator-1初识Operator