当前位置:网站首页>Declare an abstract class vehicle, which contains the private variable numofwheel and the public functions vehicle (int), horn (), setnumofwheel (int) and getnumofwheel (). Subclass mot
Declare an abstract class vehicle, which contains the private variable numofwheel and the public functions vehicle (int), horn (), setnumofwheel (int) and getnumofwheel (). Subclass mot
2022-07-01 13:06:00 【laocooon】
Declare an abstract class Vehicle, It contains private variables numOfWheels And common functions Vehicle(int)、Horn()、setNumOfWheels(int) and getNumOfWheels(). Subclass Motorcycle and Truck From classes through public inheritance Vehicle Derived from .
Motorcycle There is a private member boolean variable isElectricity, And four common functions setType(bool)、getType()、ChangeType() and Horn().ChangeType() It can be for isElectricity Assign an opposite value , and Horn() You can output a string “ Toot toot , Toot toot ”.
Truck There is a private member variable load, And four public member function constructors Truck(float)、setLoad(float)、getLoad() and Horn(). For classes Truck, Horn() You can output a string " longlong, longlong ".
stay main() Function , Create objects for subclasses respectively mm、tt, And by quoting Vehicle call Horn() To demonstrate the polymorphism of object-oriented programming .
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;
// Declare an abstract class Vehicle,
// It contains private variables numOfWheels And common functions Vehicle(int)、Horn()、setNumOfWheels(int) and getNumOfWheels().
class Vehicle
{
protected:
int numOfWheels;// Several rounds
public:
Vehicle(int n)
{
numOfWheels = n;
}
virtual void Horn() = 0;
virtual void setNumOfWheels(int) = 0;
virtual int getNumOfWheels() = 0;
};
// Subclass Motorcycle and Truck From classes through public inheritance Vehicle Derived from .
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;
}
边栏推荐
- Vs code setting Click to open a new file window without overwriting the previous window
- 79. Word search [DFS + backtracking visit + traversal starting point]
- Class initialization and instantiation
- 有人碰到过这种情况吗,oracle logminer 同步的时候,clob字段的值丢失
- 使用nvm管理nodejs(把高版本降级为低版本)
- Feign & Eureka & zuul & hystrix process
- 阿霍的三个阶段
- Svg diamond style code
- Simple Fibonacci (recursive)
- [encounter Django] - (II) database configuration
猜你喜欢

ROS2 Foxy depthai_ ROS tutorial

mysql统计账单信息(下):数据导入及查询

Huawei HMS core joins hands with hypergraph to inject new momentum into 3D GIS

Wang Xing's infinite game ushers in the "ultimate" battle

Logstash error: cannot reload pipeline, because the existing pipeline is not reloadable

Vs code set code auto save

Feign & Eureka & Zuul & Hystrix 流程

JS变色的乐高积木

Ikvm of toolbox Net project new progress

logstash报错:Cannot reload pipeline, because the existing pipeline is not reloadable
随机推荐
题目 2612: 蓝桥杯2021年第十二届省赛真题-最少砝码(枚举找规律+递推)
Simple two ball loading
波浪动画彩色五角星loader加载js特效
SSO and JWT good article sorting
网络socket的状态要怎么统计?
mysql统计账单信息(下):数据导入及查询
工具箱之 IKVM.NET 项目新进展
Will it affect the original MySQL database to read the data of a MySQL table in full by flick MySQL CDC
How can genetic testing help patients fight disease?
硬件开发笔记(九): 硬件开发基本流程,制作一个USB转RS232的模块(八):创建asm1117-3.3V封装库并关联原理图元器件
Svg diamond style code
Function test process in software testing
Topic 2612: the real topic of the 12th provincial competition of the Blue Bridge Cup in 2021 - the least weight (enumerating and finding rules + recursion)
logstash报错:Cannot reload pipeline, because the existing pipeline is not reloadable
The future of game guild in decentralized games
1553B环境搭建
Perl 5.10.0 installation package download
nexus搭建npm依赖私库
不同的测试技术区分
题目 1004: 母牛的故事(递推)