当前位置:网站首页>声明一个抽象类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;
}
边栏推荐
- 基于.NetCore开发博客项目 StarBlog - (13) 加入友情链接功能
- 请问flink mysql cdc 全量读取mysql某个表数据,对原始的mysql数据库有影响吗
- Need your own cognition
- Development trend and market demand analysis report of China's high purity copper industry Ⓕ 2022 ~ 2028
- Blocking sockets的读写操作该怎么玩?
- Accept different views with an open mind
- 有没有大佬 遇到过flink监控postgresql数据库, 检查点无法使用的问题
- First intention is the most important
- ROS2 Foxy depthai_ ROS tutorial
- 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
猜你喜欢
mysql统计账单信息(下):数据导入及查询
redis探索之缓存击穿、缓存雪崩、缓存穿透
Vs code setting Click to open a new file window without overwriting the previous window
数据库之MHA高可用集群部署及故障切换
Use Net core access wechat official account development
王兴的无限游戏迎来“终极”一战
Nc100 converts strings to integers (ATOI)
"Analysis of 43 cases of MATLAB neural network": Chapter 40 research on prediction of dynamic neural network time series -- implementation of NARX based on MATLAB
ROS2 Foxy depthai_ ROS tutorial
Redis exploration: cache breakdown, cache avalanche, cache penetration
随机推荐
使用BurpSuite对app抓包教程
快速整明白Redis中的压缩列表到底是个啥
从数据库中更新一条数据,用cdc会同时获得op字段分别为d和c的两条数据吗?我记得之前是只有op为u
leetcode:241. Design priority for operation expression [DFS + Eval]
mysql统计账单信息(下):数据导入及查询
华为面试题: 招聘
Digital signal processing -- Design of linear phase (Ⅱ, Ⅳ) FIR filter (2)
leetcode:226. 翻转二叉树【dfs翻转】
A hole in solder paste
Development trend and market demand analysis report of China's high purity copper industry Ⓕ 2022 ~ 2028
Will it affect the original MySQL database to read the data of a MySQL table in full by flick MySQL CDC
When Sqlalchemy deletes records with foreign key constraints, the foreign key constraints do not work. What is the solution?
使用nvm管理nodejs(把高版本降级为低版本)
软件测试中功能测试流程
Wechat applet - 80 practical examples of wechat applet projects
List of QT players [easy to understand]
路由基础之OSPF LSA详细讲解
基因检测,如何帮助患者对抗疾病?
Based on the open source stream batch integrated data synchronization engine Chunjun data restore DDL parsing module actual combat sharing
腾讯安全发布《BOT管理白皮书》|解读BOT攻击,探索防护之道