当前位置:网站首页>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;
}
边栏推荐
- MySQL报错1040Too many connections的原因以及解决方案
- leetcode:241. Design priority for operation expression [DFS + Eval]
- 硬件开发笔记(九): 硬件开发基本流程,制作一个USB转RS232的模块(八):创建asm1117-3.3V封装库并关联原理图元器件
- ROS2 Foxy depthai_ ROS tutorial
- 科学创业三问:关于时机、痛点与重要决策
- Fiori applications are shared through the enhancement of adaptation project
- Fiori 应用通过 Adaptation Project 的增强方式分享
- 香港科技大学李泽湘教授:我错了,为什么工程意识比上最好的大学都重要?
- 不同的测试技术区分
- PG basics -- Logical Structure Management (trigger)
猜你喜欢
随机推荐
简单斐波那契(递推)
There are risks in trading
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)
请问flink mysql cdc 全量读取mysql某个表数据,对原始的mysql数据库有影响吗
工具箱之 IKVM.NET 项目新进展
哪个券商公司开户佣金低又安全又可靠
Idea of [developing killer]
买卖其实也有风险
Wang Xing's infinite game ushers in the "ultimate" battle
使用nvm管理nodejs(把高版本降级为低版本)
不同的测试技术区分
PG basics -- Logical Structure Management (trigger)
下半年还有很多事要做
波浪动画彩色五角星loader加载js特效
运行Powershell脚本提示“因为在此系统上禁止运行脚本”解决办法
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
shell脚本导入存储过程到数据库
启动solr报错The stack size specified is too small,Specify at least 328k
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
leetcode:226. Flip binary tree [DFS flip]









![79. Word search [DFS + backtracking visit + traversal starting point]](/img/d6/a7693b2af435b7cf4562161ca4bd3f.png)