题目一

【问题描述】

要求:(1)实现交通工具这个类;(2)定义并实现一个小车类car,是它的公有派生类,小车本身的私有属性有载人数,小车的函数有构造函数,getpassenger(获取载人数),print(打印车轮数,重量和载人数)。

【输入形式】

1
2
3
vehicle input:4 50

car input:4 45 5

【输出形式】

1
2
3
4
5
vehicle is:车轮数:4,重量:50

car is:车轮数:4,重量:45

可载人数:5

【代码】

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <iostream>
using namespace std;
class vehicle
{protected:
int wheels;
float weight;
public:
vehicle(int awheels,float aweight);
int get_wheels();
float get_weight();
void print();
};

vehicle::vehicle(int awheels,float aweight)
{wheels=awheels;
weight=aweight;
}
int vehicle::get_wheels()
{return wheels;
}
float vehicle::get_weight()
{return weight;}
void vehicle::print()
{cout<<"车轮数:"<<wheels<<",重量:"<<weight<<endl;}

class car:public vehicle
{private:
int passengers;
public:
car(int awheels,float aweight,int pass);
int getpassenger();
void print();
};
car::car(int awheels,float aweight,int pass):vehicle(awheels,aweight)
{
passengers=pass;}
int car::getpassenger()
{return passengers;}
void car::print()
{
cout<<"车轮数:"<<wheels<<",重量:"<<weight<<endl;
cout<<"可载人数:"<<passengers<<endl;
}
int main()
{
int wheels;
float weight;
int pass;
cout<<"vehicle input:";
cin>>wheels>>weight;
vehicle v1(wheels,weight);
cout<<"car input:";
cin>>wheels>>weight>>pass;
car c1(wheels,weight,pass);
cout<<endl;
cout<<"vehicle is:";
v1.print();
cout<<"car is:";
c1.print();
return 0;
}