6-55.汽车类的继承

根据给定的汽车类vehicle(包含的数据成员有车轮个数wheels和车重weight)声明,完成其中成员函数的定义,之后再定义其派生类并完成测试。

小车类car是它的派生类,其中包含载人数passenger_load。每个类都有相关数据的输出方法。

输出样例:

在这里给出相应的输出。例如:

Type:Vehicle

Wheel:4

Weight:1000kg

Type:Car

Type:Vehicle

Wheel:4

Weight:2000kg

Load:5 persons

它的派生类,其中包含载人数passenger_load。每个类都有相关数据的输出方法。

代码实现:

cpp 复制代码
#include<iostream>
using  namespace  std;  
class  Vehicle  
{  
        protected:  
                int  wheels;  
                float  weight;  
        public:  
                Vehicle(int  wheels,float  weight);  
                int  get_wheels();  
                float  get_weight();  
                float  wheel_load();  
                void  show();  
};  

/*  请在这里填写答案  */
class Car : public Vehicle{
	private:
		int passenger_load;
	public:
		Car(int a,float b,int c):Vehicle(a,b){
			passenger_load=c;
		}
		void show(){
			cout<<"Type:Car"<<endl;
			Vehicle::show();
			cout<<"Load:"<<passenger_load<<" persons"<<endl;
		}
		
};

Vehicle::Vehicle(int wheels,float weight):wheels(wheels),weight(weight){
}
void Vehicle::show(){
	cout<<"Type:Vehicle"<<endl;
	cout<<"Wheel:"<<wheels<<endl;
	cout<<"Weight:"<<weight<<"kg"<<endl;
}

int  main  ()  
{  
        Vehicle  v(4,1000);
        v.show();  
        Car  car1(4,2000,5);    
        car1.show  ();  
        return  0;
}
相关推荐
大白IT2 天前
第二部分:感知篇——汽车的“眼睛”与“耳朵”(第5章:环境感知与理解——从“看见”到“看懂”)
人工智能·目标跟踪·自动驾驶·汽车
JIngJaneIL3 天前
汽车租赁|汽车管理|基于Java+vue的汽车租赁系统(源码+数据库+文档)
java·vue.js·spring boot·汽车·论文·毕设·汽车租赁
唐丙斯城3 天前
新能源汽车动力电池全解析:性能与未来
汽车
MM_MS3 天前
C#小案例-->汽车租聘系统计价功能
c#·汽车·简单工厂模式·抽象工厂模式·visual studio
Rainmicro3 天前
TC387——MPU简介
汽车
电子科技圈4 天前
IAR与Quintauris携手推进RISC-V汽车实时应用的功能安全软件开发
嵌入式硬件·安全·设计模式·编辑器·汽车·risc-v
2301_764441334 天前
新能源汽车电磁辐射高级预测
python·算法·数学建模·汽车
MOS管-冠华伟业4 天前
微硕WINSOK高性能MOS管WST2N7002B,赋能汽车多区氛围灯精准控制
汽车
图扑可视化4 天前
图扑 HT 智慧汽车展示平台全自研技术方案
汽车·数据可视化·组态监控·汽车展示
九河云4 天前
汽车轻量化部件智造:碳纤维成型 AI 调控与强度性能数字孪生验证实践
人工智能·汽车·数字化转型