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;
}
相关推荐
QYR_Jodie12 小时前
从绿色化工与新能源汽车需求驱动到稳健增长:全球合成酯2026-2032年CAGR5.6%,2032年达14.49亿美元
汽车
chipsense13 小时前
汽车电流传感器选型全指南:从霍尔到磁通门的技术对比与工程实践
汽车·新能源汽车·霍尔电流传感器·闭环霍尔电流传感器
3DVisionary14 小时前
测管即修正!Tube Qualify赋能航空与汽车管路一体化智能在线检测
阿里云·智能手机·汽车·智能制造·航空航天·tubequalify·管路检测
ws20190717 小时前
锚定华南产业高地,2026广州汽车轻量化展解码行业升级新机遇
大数据·人工智能·科技·汽车
天天爱吃肉821818 小时前
【电机双闭环控制问答:PI 输出为何不同?测功机台架选型有多关键】
功能测试·嵌入式硬件·信息可视化·汽车
汽车搬砖家18 小时前
【AP Autosar】05.关于Application和Machine Integration界面几个配置项的思考
汽车
唐丙斯城2 天前
新能源汽车热管理:电子膨胀阀的关键作用
汽车
梅尔文.古2 天前
ADCU-Ethernet-以太网在AUTOSAR与Linux架构下对比
arm开发·单片机·汽车
天天爱吃肉82183 天前
【新能源汽车集成热管理系统压力检测技术全解析——从传感器原理到VCU控制策略】
汽车
是梦终空1163 天前
汽车电子中的Service31应用与协议详解
网络·汽车