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;
}
相关推荐
雨大王51211 小时前
汽车AI智能体矩阵:驱动行业智能化变革的新范式
人工智能·汽车
无忧智库13 小时前
汽车零部件行业破局之道:工业4.0智能工厂解决方案全景解析(PPT)
汽车
雨大王51213 小时前
汽车零部件制造中质量缺陷识别的智能化解决方案
汽车·制造
汽车仪器仪表相关领域16 小时前
AI赋能智能检测,引领灯光检测新高度——NHD-6109智能全自动远近光检测仪项目实战分享
大数据·人工智能·功能测试·机器学习·汽车·可用性测试·安全性测试
紧固件研究社17 小时前
汽车紧固件,检测标准到底严在哪里?
汽车·制造·紧固件
深圳元器猫18 小时前
板对板连接器解决方案:覆盖消费电子、汽车、工业全领域
汽车
F1331689295718 小时前
WD5030K,耐高压输出电流10A,WD5030A输出电流12A
网络·单片机·嵌入式硬件·物联网·汽车
高工智能汽车19 小时前
CES2026丨中科创达发布滴水OS 2.0 Pre 以AI原生重构智能汽车交互体验
重构·汽车·ai-native
汽车仪器仪表相关领域1 天前
工况模拟精准检测,合规减排赋能行业 ——NHASM-1 型稳态工况法汽车排气检测系统项目实战经验分享
数据库·算法·单元测试·汽车·压力测试·可用性测试
线束线缆组件品替网2 天前
Amphenol LTW 防水线缆 IP67/IP68 结构解析
运维·网络·人工智能·汽车·硬件工程·材料工程