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;
}
相关推荐
图特摩斯科技19 小时前
本体智能应用案例实践分享:汽车零部件库存优化与召回应急保障
人工智能·汽车·palantir·ontology·ontoflow·ontoos
Godspeed Zhao1 天前
跨越天际:从智能汽车到 eVTOL 的适航与系统级开发55——附录 B 典型 eVTOL 适航全生命周期文档核查清单(SOI-1 到 SOI-4 审计指南)
汽车·evtol
ykdcdc2 天前
DC-DC隔离和非隔离怎么选?工控/监控/医疗分别用哪种
数据结构·汽车·推荐算法
天天爱吃肉82182 天前
AI Agent时代你的经验值多少钱?
人工智能·python·功能测试·学习·汽车
码农学院2 天前
Elasticsearch构建汽车零配件智能搜索与匹配系统
大数据·elasticsearch·汽车
l0001092 天前
2026纯电汽车专用润滑油选购指南:从赛事技术到全兼容方案的全面对比
汽车·汽车养护·润滑油·汽车润滑油
lisw053 天前
汽车电池回收的挑战与趋势
汽车·制造
天天爱吃肉82184 天前
【电源拆解:跟着问答逐一认识开关适配器PCB元器件】
大数据·人工智能·python·功能测试·嵌入式硬件·汽车
song150265372984 天前
液位传感器在新能源汽车中的应用(纯电/插混/氢能)
汽车
dandan734624 天前
膜结构汽车棚厂家哪家品牌知名?
汽车·膜结构汽车棚厂家