当前位置:网站首页>声明一个抽象类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;
}
边栏推荐
- 类的初始化与实例化
- Interpretation of hard threshold function [easy to understand]
- 游戏公会在去中心化游戏中的未来
- R语言基于h2o包构建二分类模型:使用h2o.gbm构建梯度提升机模型GBM、使用h2o.auc计算模型的AUC值
- How to play with the reading and writing operations of blocking sockets?
- Redis exploration: cache breakdown, cache avalanche, cache penetration
- 从数据库中更新一条数据,用cdc会同时获得op字段分别为d和c的两条数据吗?我记得之前是只有op为u
- 强大、好用、适合程序员/软件开发者的专业编辑器/笔记软件综合评测和全面推荐
- 【历史上的今天】7 月 1 日:分时系统之父诞生;支付宝推出条码支付;世界上第一支电视广告
- 天青色等烟雨
猜你喜欢
随机推荐
oracle cdc 数据传输时,clob类型字段,在update时值会丢失,update前有值,但
软件测试中功能测试流程
强大、好用、适合程序员/软件开发者的专业编辑器/笔记软件综合评测和全面推荐
Double linked list related operations
Ikvm of toolbox Net project new progress
《MATLAB 神经网络43个案例分析》:第40章 动态神经网络时间序列预测研究——基于MATLAB的NARX实现
Eurake分区理解
题目 2612: 蓝桥杯2021年第十二届省赛真题-最少砝码(枚举找规律+递推)
ROS2 Foxy depthai_ros教程
高薪程序员&面试题精讲系列118之Session共享有哪些方案?
VM虚拟机配置动态ip和静态ip访问
ustime写出了bug
Wang Xing's infinite game ushers in the "ultimate" battle
leetcode:241. Design priority for operation expression [DFS + Eval]
Huawei interview question: Recruitment
游戏公会在去中心化游戏中的未来
redis探索之缓存一致性
运行Powershell脚本提示“因为在此系统上禁止运行脚本”解决办法
【牛客刷题-SQL大厂面试真题】NO2.用户增长场景(某度信息流)
Fundamentals of number theory and its code implementation