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;
}
相关推荐
Godspeed Zhao9 小时前
现代智能汽车系统——网络
网络·汽车
ASD123asfadxv10 小时前
汽车保险丝盒元件识别与分类:基于Faster R-CNN与ResNet101的检测方案详解
分类·r语言·汽车
Godspeed Zhao1 天前
现代智能汽车中的无线技术55——UWB(2)
汽车
jkyy20141 天前
健康座舱:健康有益赋能新能源汽车开启移动健康新场景
人工智能·物联网·汽车·健康医疗
ws2019071 天前
从“面子”到“里子”的变革——AUTO TECH China 2026 广州展,引领汽车内外饰新纪元
人工智能·科技·汽车
自动化控制仿真经验汇总2 天前
3.7 kW 无线电能传输系统-EXP-汽车-无线充电
自动化·汽车
Godspeed Zhao2 天前
现代智能汽车系统——总线技术1
网络·汽车·php
摩尔元数2 天前
2026年汽车仪表工厂选MES,厂商推荐
低代码·汽车·制造·mes
汽车仪器仪表相关领域2 天前
MTX-A 模拟废气温度(EGT)计 核心特性与车载实操指南
网络·人工智能·功能测试·单元测试·汽车·可用性测试
深圳博众测控2 天前
博众测控 | ISO 16750-2:2023汽车电气测试新标准解读:关键变化与测试设备选型
人工智能·测试工具·汽车