C++实例练习:交通工具(多重继承)

编程入门 行业动态 更新时间:2024-10-24 16:21:42

C++实例练习:<a href=https://www.elefans.com/category/jswz/34/1702611.html style=交通工具(多重继承)"/>

C++实例练习:交通工具(多重继承)

题目描述

1、建立如下的类继承结构:

1)一个车类CVehicle作为基类,具有max_speed、speed、weight等数据成员,display()等成员函数

2)从CVehicle类派生出自行车类CBicycle,添加属性:高度height

3)从CVehicle类派生出汽车类CMotocar,添加属性:座位数seat_num

4)从CBicycle和CMotocar派生出摩托车类CMotocycle

2、分别定义以上类的构造函数、输出函数display及其他函数(如需要)。

3、在主函数中定义各种类的对象,并测试之,通过对象调用display函数产生输出。

输入

第一行:最高速度 速度 重量 第二行:高度 第三行:座位数

输出

第一行:Vehicle: 第二行及以后各行:格式见Sample

样例输入

100 60 20
28
2

样例输出

Vehicle:
max_speed:100
speed:60
weight:20Bicycle:
max_speed:100
speed:60
weight:20
height:28Motocar:
max_speed:100
speed:60
weight:20
seat_num:2Motocycle:
max_speed:100
speed:60
weight:20
height:28
seat_num:2
#include<iostream>
using namespace std;class CVehicle
{
protected:int max_speed;int speed;int weight;	public:CVehicle(){}CVehicle(int ms, int s, int w):max_speed(ms),speed(s),weight(w){}void display(){cout<<"Vehicle:"<<endl;cout<<"max_speed:"<<max_speed<<endl;cout<<"speed:"<<speed<<endl;cout<<"weight:"<<weight<<endl;cout<<endl;}
};class CBicycle:public CVehicle
{
protected:int height;public:CBicycle(int ms, int s, int w, int h):CVehicle(ms,s,w),height(h){}void display(){cout<<"Bicycle:"<<endl;cout<<"max_speed:"<<max_speed<<endl;cout<<"speed:"<<speed<<endl;cout<<"weight:"<<weight<<endl;cout<<"height:"<<height<<endl;cout<<endl;}
};class CMotocar:public CVehicle
{
protected:int seat_num;public:CMotocar(int ms, int s, int w, int seat):CVehicle(ms,s,w){	seat_num = seat; }void display(){cout<<"Motocar:"<<endl;cout<<"max_speed:"<<max_speed<<endl;cout<<"speed:"<<speed<<endl;cout<<"weight:"<<weight<<endl;cout<<"seat_num:"<<seat_num<<endl;cout<<endl;}
};class CMotocycle:public CBicycle, public CMotocar
{
public:CMotocycle(int ms, int s, int w, int h, int seat):CBicycle(ms,s,w,h),CMotocar(ms,s,w,seat){}void display(){cout<<"Motocycle:"<<endl;cout<<"max_speed:"<<CBicycle::max_speed<<endl;cout<<"speed:"<<CBicycle::speed<<endl;cout<<"weight:"<<CBicycle::weight<<endl;cout<<"height:"<<height<<endl;cout<<"seat_num:"<<seat_num<<endl;cout<<endl;}
};int main()
{int max_speed,speed,weight,height,seat_num;cin>>max_speed>>speed>>weight;cin>>height;cin>>seat_num;CVehicle vehicle(max_speed,speed,weight);CBicycle bicycle(max_speed,speed,weight,height);CMotocar motocar(max_speed,speed,weight,seat_num);CMotocycle motocycle(max_speed,speed,weight,height,seat_num);vehicle.CVehicle::display();bicycle.CBicycle::display();motocar.CMotocar::display();motocycle.CMotocycle::display();
}
#include<iostream>
#include<string>
using namespace std;
class CVehicle
{
protected:int max_speed, speed, weight;
public:CVehicle(int ms, int s, int w) :max_speed(ms), speed(s), weight(w){}~CVehicle() {}void display(){cout << "Vehicle:" << endl;cout << "max_speed:" << max_speed << endl;cout << "speed:" << speed << endl;cout << "weight:" << weight << endl;cout << endl;}
};
class CBicycle :virtual public CVehicle
{
protected:int height;
public:CBicycle(int h, int ms, int s, int w) :height(h), CVehicle(ms, s, w){}~CBicycle() {}void display(){cout << "Bicycle:" << endl;cout << "max_speed:" << max_speed << endl;cout << "speed:" << speed << endl;cout << "weight:" << weight << endl;cout << "height:" << height << endl;cout << endl;}
};
class CMotocar :virtual public CVehicle
{
protected:int seat_num;
public:CMotocar(int sn, int ms, int s, int w) :seat_num(sn), CVehicle(ms, s, w){}~CMotocar() {}void display(){cout << "Motocar:" << endl;cout << "max_speed:" << max_speed << endl;cout << "speed:" << speed << endl;cout << "weight:" << weight << endl;cout << "seat_num:" << seat_num << endl;cout << endl;}
};class CMotocycle :public CBicycle, public CMotocar
{
protected:public:CMotocycle(int h, int sn, int ms, int s, int w) :CVehicle(ms, s, w), CBicycle(h, ms, s, w), CMotocar(sn, ms, s, w){}~CMotocycle() {}void display(){cout << "Motocycle:" << endl;cout << "max_speed:" << max_speed << endl;cout << "speed:" << speed << endl;cout << "weight:" << weight << endl;cout << "height:" << height << endl;cout << "seat_num:" << seat_num << endl;}
};
int main()
{int max_speed, speed, weight, height, seat_num;cin >> max_speed >> speed >> weight >> height >> seat_num;CMotocycle sb(max_speed, speed , weight,height,seat_num);sb.CVehicle::display();sb.CBicycle::display();sb.CMotocar::display();sb.CMotocycle::display();return 0;}

更多推荐

C++实例练习:交通工具(多重继承)

本文发布于:2024-02-26 13:14:12,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1702597.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:交通工具   实例

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!