编写一个程序,其中有一个汽车类Vehicle,它具有一个需要传递参数的构造函数,类中的数据成员:车轮个数wheels和车重weight为保护属性;小车类Car是它的私有派生类,其中包含载人数passa

题目要求:

编写一个程序,其中有一个汽车类Vehicle,它具有一个需要传递参数的构造函数,类中的数据成员:车轮个数wheels和车重weight为保护属性;小车类Car是它的私有派生类,其中包含载人数passager_load;卡车类Truck是Vehicle的私有派生类,其中包含载人数passager_load和载重量payload。每个类都有相关数据的输出方法。

代码描述:

1.创建Vehicle类

创建Vehicle.h文件,用于声明Vehicle类

#pragma once
#include<iostream>
using namespace std;
//定义汽车类
class Vehicle
{
protected:
	int wheels;		//车轮个数
	double weight;	//车重量
public:
	//构造函数
	Vehicle(int wh,double we);
	int GetWheels();	//获取车轮个数
	double GetWeight(); //获取车的重量
	void Display();		//显示输出车辆信息
};

2.实现Vehicle类

创建Vehicle.cpp文件,用于实现Vehicle类

//导入声明Vehicle类的头文件
#include "Vehicle.h"
//汽车类的构造函数初始化对象
Vehicle::Vehicle(int wh, double we):wheels(wh),weight(we)
{
}
//获取车轮个数
int Vehicle::GetWheels()
{
	return wheels;
}
//获取车重量
double Vehicle::GetWeight()
{
	return weight;
}
//显示输出车辆信息
void Vehicle::Display()
{
	cout << "车轮个数:" << this->GetWheels() << endl;
	cout << "车重量:" << this->GetWeight() << endl;
}

3.创建Car类

创建Car.h文件,用于声明Car类

#pragma once
//导入Vehicle(汽车类)
#include"Vehicle.h"
//定义Car(小车)类 使用私有的方式继承 Vehicle(汽车)类
class Car:private Vehicle
{
private:
	int passager_load;	//车载人数

public:
	//构造函数
	Car(int pa, int wh, double we);
	int GetPassager_load();	//获取车载人数的方法
	int GetWheels();		//获取车轮个数
	double GetWeight();		//获取车的重量
	void Display();			//显示Car类的信息
};

4.实现Car类

创建Car.cpp文件,用于实现Car类

//导入Car类的声明文件
#include "Car.h"
//实现Car类的构造方法,初始化Car类
Car::Car(int pa, int wh, double we):passager_load(pa),Vehicle(wh,we)
{
}
//实现获取车载人数的方法
int Car::GetPassager_load()
{
	return passager_load;
}
//实现获取车的重量
double Car::GetWeight()
{

	return Vehicle::GetWeight();
}
//实现获取车轮个数
int Car::GetWheels()
{
	return Vehicle::GetWheels();
}
//实现显示Car类的信息的方法
void Car::Display()
{
	Vehicle::Display();
	cout << "车载人数:" << this->GetPassager_load() << endl;
}

5.创建Truck类

创建Truck.h文件,用于声明Truck类

#pragma once
//导入Vehicle(汽车类)
#include"Vehicle.h"
//定义Truck(卡车)类 使用私有的方式继承 Vehicle(汽车)类
class Truck:private Vehicle
{
private:
	int passager_load;	//车载人数
	double payload;		//车载重量
public:
	//构造函数
	Truck(int pa,double pl, int wh, double we);
	int GetPassager_load(); //获取车载人数的方法
	double GetPayload();	//获取车载重量的方法
	int GetWheels();		//获取车轮个数
	double GetWeight();		//获取车的重量
	void Display();			//显示Truck类的信息
};

6.实现Truck类

创建Truck.cpp文件,用于实现Truck类

//导入Truck类的声明文件
#include "Truck.h"
//实现Truck类的构造方法,初始化Truck类
Truck::Truck(int pa, double pl, int wh, double we):passager_load(pa), payload(pl), Vehicle(wh,we)
{
}
//实现获取车载人数的方法
int Truck::GetPassager_load() {
	return passager_load;
}
//实现获取车载重量的方法
double Truck::GetPayload() {
	return payload;
}
//实现获取车轮个数
int Truck::GetWheels() {
	return Vehicle::GetWheels();
}
//实现获取车的重量
double Truck::GetWeight() {
	return Vehicle::GetWeight();
}
//显示Truck类的信息
void Truck::Display()
{
	Vehicle::Display();
	cout <<"车载人数:" << this->GetPassager_load() << endl;
	cout << "车载重量:" << this->GetPayload() << endl;
	
}

7.Main函数实现

创建main.cpp实现各个类的功能

#include"Vehicle.h"
#include"Car.h"
#include"Truck.h"
int main() {
	//汽车类
	Vehicle v = Vehicle(10, 5.7);
	v.Display();
	cout << endl;
	//小车类
	Car c = Car(8, 5, 6.7);
	c.Display();
	cout << endl;
	//卡车类
	Truck t = Truck(8, 5.5, 10, 6.7);
	t.Display();
	return 0;
}

样例输出:

在这里插入图片描述

  • 5
    点赞
  • 38
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

孑然R

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值