当前位置:网站首页>声明一个抽象类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;
}
边栏推荐
- Mobile note application
- First intention is the most important
- PG基础篇--逻辑结构管理(触发器)
- Accept different views with an open mind
- 微信模拟地理位置_伪装微信地理位置
- Run PowerShell script prompt "because running script is prohibited on this system" solution
- 请问flink mysql cdc 全量读取mysql某个表数据,对原始的mysql数据库有影响吗
- oracle cdc 数据传输时,clob类型字段,在update时值会丢失,update前有值,但
- [encounter Django] - (II) database configuration
- 下半年还有很多事要做
猜你喜欢
随机推荐
从数据库中更新一条数据,用cdc会同时获得op字段分别为d和c的两条数据吗?我记得之前是只有op为u
leetcode:329. 矩阵中的最长递增路径【dfs + cache + 无需回溯 + 优雅】
硬件开发笔记(九): 硬件开发基本流程,制作一个USB转RS232的模块(八):创建asm1117-3.3V封装库并关联原理图元器件
QT 播放器之列表[通俗易懂]
用.Net Core接入微信公众号开发
Eurake partition understanding
leetcode:241. Design priority for operation expression [DFS + Eval]
Tencent Li Wei: deeply cultivate "regulatory technology" to escort the steady and long-term development of the digital economy
高薪程序员&面试题精讲系列118之Session共享有哪些方案?
When Sqlalchemy deletes records with foreign key constraints, the foreign key constraints do not work. What is the solution?
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
Topic 1004: the story of cows (recursion)
有人碰到过这种情况吗,oracle logminer 同步的时候,clob字段的值丢失
PG基础篇--逻辑结构管理(触发器)
Will it affect the original MySQL database to read the data of a MySQL table in full by flick MySQL CDC
6.30 simulation summary
oracle cdc 数据传输时,clob类型字段,在update时值会丢失,update前有值,但
网络socket的状态要怎么统计?
VM虚拟机配置动态ip和静态ip访问
Operator-1 first acquaintance with operator









