当前位置:网站首页>声明一个抽象类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;
}
边栏推荐
- Tencent security and KPMG released a regulatory technology white paper to analyze the "3+3" hot application scenarios
- 使用nvm管理nodejs(把高版本降级为低版本)
- codeforces -- 4B. Before an Exam
- Localtime can't re-enter. It's a pit
- 华为面试题: 招聘
- 系统测试UI测试总结与问题(面试)
- flinkcdc要实时抽取oracle,对oracle要配置什么东西?
- Redis explores cache consistency
- Use Net core access wechat official account development
- 用.Net Core接入微信公众号开发
猜你喜欢

基因检测,如何帮助患者对抗疾病?

Based on the open source stream batch integrated data synchronization engine Chunjun data restore DDL parsing module actual combat sharing

Feign & Eureka & Zuul & Hystrix 流程

Ikvm of toolbox Net project new progress

Logstash error: cannot reload pipeline, because the existing pipeline is not reloadable
![leetcode:226. Flip binary tree [DFS flip]](/img/b8/6c5596ac30de59f0f347bb0bddf574.png)
leetcode:226. Flip binary tree [DFS flip]

Use Net core access wechat official account development

How can genetic testing help patients fight disease?

游戏公会在去中心化游戏中的未来

The popular major I chose became "Tiankeng" four years later
随机推荐
I spent tens of thousands of dollars to learn and bring goods: I earned 3 yuan in three days, and the transaction depends on the bill
redis探索之缓存一致性
单点登录SSO与JWT好文整理
[20220605] Literature Translation -- visualization in virtual reality: a systematic review
SSO and JWT good article sorting
A hole in solder paste
微信模拟地理位置_伪装微信地理位置
R language builds a binary classification model based on H2O package: using H2O GBM build gradient hoist model GBM, use H2O AUC value of AUC calculation model
项目部署,一点也不难!
List of QT players [easy to understand]
Wang Xing's infinite game ushers in the "ultimate" battle
强大、好用、适合程序员/软件开发者的专业编辑器/笔记软件综合评测和全面推荐
手把手教你完成图像分类实战——基于卷积神经网络的图像识别
Operator-1 first acquaintance with operator
下半年还有很多事要做
VS Code 设置代码自动保存
redis探索之缓存击穿、缓存雪崩、缓存穿透
快速整明白Redis中的压缩列表到底是个啥
Shell script imports stored procedures into the database
codeforces -- 4B. Before an Exam