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

Python培训第一课( 继承 多态) 面向对象 顾名思义就是把现实中的事务都抽象成为程序设计中的“对象”,其基本思想是万物皆对象 几个概念 (Class):用来描述具有相同的属性和方法的对象的集合。它定义了该集合中每个对象所共有的属性和方法。对象是实例。 方法重写:如果从父继承的方法不能满足子的需求,可以对其进行改写,这个过程叫方法的覆盖(override),也称为方法的重写。 继承:即一个派生(derived class)继承(base class)的字段和方法。继承也允许把一个派生的对象作为一个对象对待。例. 阅读详情

题目描述

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:20

Bicycle:
max_speed:100
speed:60
weight:20
height:28

Motocar:
max_speed:100
speed:60
weight:20
seat_num:2

Motocycle:
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++ 多重继承详解 C++ 多重继承多重继承的语法多重继承下的构造函数多重继承下的命名冲突派生对象中数据成员的排列形式 多重继承的语法 多重继承的语法也很简单,将多个基用逗号隔开即可。例如已声明了A、B和C,那么可以这样来声明派生D: class D: public A, private B, protected C{ //D新增加的成员 } D 是多继承形式的派生,它以公有的方式继承 A ,以私有的方式继承 B ,以保护的方式继承 C 。D 根据不同的继承方式获取 A、B、C 中的成员,确定它 阅读详情

相关推荐

专治疑难系列 - 解决打印机凭证冲突问题

一、问题描述 二、解决方法 三、问题已解决

Passerby_Wang的博客 2万+

定义一个交通工具(Vehicle)

请定义一个交通工具(Vehicle),其中有:  属性:速度(speed),体积(size)等  方法:移动(move()),设置速度(setSpeed(int speed)),设置体积(setSize(int size))加速speedUp(),减速speedDown()等  在测试Vehicle中的main()实例一个交通工具对象,通过方法给它初始化speed,size的值,并

qq_40168212的博客 1万+

(十五)CX20106A 中超声波测量的应用

(十五)CX20106A 中超声波测量的应用

m0_52555663的博客 4465

C#定义汽车继承交通工具

设计一个交通工具Vehicle,包含的数据成员有车轮个数wheels和车重weight。以及带有这两个参数的构造方法,具有Run方法,Run中方法输出running字样。小车Car是它的子,其中增加了数据成员车载人数passenger_load。Car中有带参数的构造方法,重写Run方法:输出Car is running。并重写ToString()方法:显示car中信息(显示车轮数、车重、车载人数)。最后编写主方法,定义car的对象,并调用Run方法,以及显示car中信息。 输入格式: 分三行.

weixin_63565034的博客 2412

定义一个交通工具(Vehicle)的

定义一个交通工具(Vehicle)的 代码的封装 对外提供get和set方法外部访问封装后的属性

heliuerya的博客 2515

C++ 交通工具多重继承

题目描述 1、建立如下的继承结构: 1)一个CVehicle作为基,具有max_speed、speed、weight等数据成员,display()等成员函数 2)从CVehicle派生出自行车CBicycle,添加属性:高度height 3)从CVehicle派生出汽车CMotocar,添加属性:座位数seat_num 4)从CBicycle和CMotocar派生出摩托车CMo...

hhhertzzz的博客 4338

C++多重继承

大多数应用程序使用单个基的公用继承,但是在某些情况下,单继承是不够的,必须使用多继承C++允许为一个派生指定多个基,这样的继承结构被称做多重继承. 举个例子,交通工具可以派生出汽车和船连个子,但拥有汽车和船共同特性水陆两用汽车就必须继承来自汽车与船的共同属性。如下图示: 代码实现: //多重继承 #include &lt;iostream&gt; using namesp...

yhc166188的博客 1万+

C++高级主题】多重继承

C++ 的面向对象编程中,继承(Inheritance)是实现代码复用和型扩展的核心机制。我们熟悉的 “单继承”(Single Inheritance)允许一个派生一个继承属性和方法,但现实中的复杂场景往往需要更灵活的模型 —— 例如,一个 “智能手表” 可能需要同时继承 “计时器”(Timer)和 “通信模块”(Communication)两个独的基。这时,C++ 提供的多重继承(Multiple Inheritance)就能大显身手。

byte轻骑兵的技术小窝 1938

C++ 多重继承

C++ 编程语言技术知识积累

xiaogangjava的博客 5530

C++继承多重继承

多重继承一个一个派生,这个派生又有一个派生继承一个派生有多个基 下面我们看一个多重继承的例子 #include &lt;iostream&gt; #include &lt;stdlib.h&gt; #include &lt;string&gt; using namespace std; /** * 定义人: Person * 数据成员: m_strN...

成长日记的博客 1万+

C++ 继承(2): 多重继承, 多继承, 虚继承(virtual)

C++远征之继承篇 视频教程 笔记 方便自己查阅和复习,温故而知新。 接着C++ 继承(1): 继承方式(public, protected, private), 继承中的特殊关系(隐藏 , is-a) 继续做笔记 目录 4 多继承多重继承 4.1 多重继承 4.2 多继承 代码示例 5 虚继承 代码示例 参考资料 4 多继承多重继承 4.1...

TechArtisan6的博客 5096

C++中的多重继承

C++中的多重继承的细节详解

一怀明月的博客 1535

C++ 多重继承的优缺点

举例:现实生活中,许多事物可能有两个或者两个以上事物的属性,为了描述这种情况,C++引入了多继承。 允许一个指向多个基,这样继承的结构叫多重继承。 概念 多重继承:常规情况,一个只有一个,而C++支持多重继承,即一个可以继承自多个。 人(Person)可以吃饭和睡觉,既可以是作家也可以是程序员,作家可以写文章,程序员可以写程序, 即是作家又是程序员的人能够写文章...

入门记 1万+

c++交通工具的派生)

一个交通工具vehicle,将它为 基派生的有派生小车car,卡车truck和轮船boat,定义这些,并使其能够显示其各交通工具的详细信息。包含的信息有如下几种:1.名字 -----与输入的名字相符合2.时速(km/h) -----最高时速3.耗油量(L/100km) -----在经济时速下测得耗油量。

wyt1344的博客 665

厦大计算机系C++程序设计实验(四)

1 实验目的 掌握运算符重载的方法,理解虚函数与多态性,实现运行时多态性的程序设计。 2 实验内容 (1)定义一个复数(Complex),并重载运算符+和-,使得两个复数可以进行加法和减法的运算,注意:加法和减法可能是复数加减上另一个复数,也可能是复数加上一个普通的整数。 (2)定义一个交通工具(Vehicles)基,包含run、stop成员函数,由此派生出自行车(Bicycle)

荔枝的FM 1417

请定义一个交通工具(Vehicle),其中有: 属性:速度(speed),体积(size)

请定义一个交通工具(Vehicle),其中有: 属性:速度(speed),体积(size)等 方法:移动(move()),设置速度(setSpeed(int speed)),设置体积(setSize(int size))加速speedUp(),减速speedDown()等 在测试Vehicle中的main()实例一个交通工具对象,通过方法给它初始化speed,size的值,并打印出来。...

weixin_44349707的博客 1万+

Vehicle基

题目 定义一个(Vehicle),有 run、stop 等成员函数,由此派生出自行车 (Bicycle) 、汽车 (Motorcar) ,从 Bicycle 和 Motorcar 派生出摩托车(Motorcycle),它们都有run、stop 等成员函数。观察虚函数的作用。 相关阅读 相关阅读 完整代码 #include<bits/stdc++.h> using namespace std; class Vehicle{ public: virtual void run

Lhw_666的博客 2231

请定义一个交通工具(Vehicle)的,其中有属性:速度(speed)、体积(size)等。

方法:移动(move())、设置速度(jetspeed(int speed)、加速(speedUp())、减速(speedDown())等。 : package tt; public class Vehicle { int speed,size; public Vehicle(int a,int b) { speed=a; size=b; } public Vehicle() ...

qq_63108747的博客 6818

请定义一个交通工具(Vehicle)

属性:体积、重量、速度、是否启动等方法:构造方法(初始化交通工具属性的值)、加速、减速、打印交通工具状态数据等。在main()实例一个交通工具对象,初始化此对象,并且打印其状态。然后,调用加速、减速的方法对速度进行改变,并且打印其状态。......

m0_65420451的博客 1327

磁共振成像硬件基本知识.pptx

磁共振成像硬件基本知识.pptx

上一篇: C++实例练习:Point(类与构造)
下一篇: C++实例练习:分数运算(类+构造)
tony996
博客等级 码龄7年 28粉丝 14原创
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值