有一个汽车类Vehicle,它的数据成员有车轮数(wheels)、汽车重量(weight),均为受保护的访问权限;小汽车类Car是Vehicle的公有继承类,载客数(passengers_load)是Car类的私有数据成员;卡车类Truck也是Vehicle的公有继承类,载重量(weight_load)是Truck类的私有数据成员。每一个类都有自己的数据输出方法display()。画出类图,编写程序,反映这种继承关系。
类图如下:
代码如下
头文件(caar.h)如下
#pragma once
class Vehicle { //汽车类型,基类
protected:
char model[50];//型号
char engine[20];//发动机
char manufacturer[20];//出厂商
double price; //价钱
double maxvelocity;//最高车速
double warranty_period;//保修时间
int wheels;//车轮数
double weight;//汽车重量
public:
Vehicle(const char* mo, const char* en, const char* ma, double pri,
double maxv, double w_p, int ws, double wt);
~Vehicle();
void display();
};
class Car :public Vehicle { //小汽车类,子类
private:
int pessengers_load;//载客数
char color[20];//颜色
public:
Car(const char* mo, const char* en, const char* ma, double pri,
double maxv, double w_p, int ws, double wt,int p, const char*c);
~Car();
void display();
};
class Truck :protected Vehicle { //卡车类,子类
private:
double weight_load;//载重量
double maxhigh;//限高
public:
Truck(const char* mo, const char* en, const char* ma, double pri,
double maxv, double w_p, int ws, double wt, double w, double mh);
~Truck();
void display();
};
main文件如下:
#include<iostream>
using namespace std;
#include"car.h"
#include<string>
Vehicle::Vehicle(const char *mo, const char* en, const char* ma,double pri,
double maxv,double w_p,int ws, double wt) {
strcpy_s(model, mo);
strcpy_s(engine, en);
strcpy_s(manufacturer, ma);
price = pri;
maxvelocity = maxv;
warranty_period = w_p;
wheels = ws;
weight = wt;
}
void Vehicle::display() {
cout << "型号:" << model << endl;
cout << "发动机:" << engine << endl;
cout << "出厂商:" << manufacturer << endl;
cout << "价格:" << price <<"万元"<< endl;
cout << "最高车速:" << maxvelocity<<"KM/H" << endl;
cout << "保修时间:" << warranty_period << "年" << endl;
cout << "车轮数:" << wheels <<"个"<< endl;
cout << "汽车重量:" << weight <<"T"<< endl;
}
Vehicle::~Vehicle() {
cout << "汽车类退出"<<endl;
}
Car::Car(const char* mo, const char* en, const char* ma, double pri,
double maxv, double w_p, int ws, double wt,
int p, const char *c):Vehicle(mo, en, ma, pri, maxv, w_p, ws, wt) {
pessengers_load = p;
strcpy_s(color, c);
}
void Car::display()
{
cout << endl<<"小汽车类"<<endl;
Vehicle::display();
cout << "载客数:"<<pessengers_load<< "人"<<endl;
cout << "颜色:" <<color<< endl;
}
Car::~Car() {
cout << "小汽车类退出" << endl;
}
Truck::Truck(const char* mo, const char* en, const char* ma, double pri,
double maxv, double w_p, int ws, double wt, double w,
double mh) :Vehicle(mo, en, ma, pri, maxv, w_p, ws, wt) {
weight_load = w;
maxhigh = mh;
}
void Truck::display() {
cout << endl<<"卡车类"<<endl;
Vehicle::display();
cout << "载重量:" << weight_load << "T" << endl;
cout << "限高:" << maxhigh << "M" << endl;
}
Truck::~Truck() {
cout << "卡车类退出" << endl;
}
void main()
{
Vehicle v("无", "无 ", "无 ",
0, 0, 0, 0, 0);
v.display();
Car c("2019款 180TUCRBO CTV尚悦版 国V", "1.0T 125马力 L3", "东风本田",
11.99, 200, 3, 4, 1.245,4,"红色");
c.display();
Truck t("2020款 1.5L 单排L3C 国VI", "1.5L 99马力 L4","上海通用五菱"
,4.98,103,3,4,1.340,0.83,2);
t.display();
}
运行截图如下:
代码仅供参考