|
怎么把这个程序改成,show()动态联编,可以输出多组数据的?
- #include<bits/stdc++.h>
-
- using namespace std;
-
- class vehicle{//车类,作为基类
-
- protected:
-
- int MaxSpeed ,Weight ;
-
- //最大速度,重量
-
- public:
-
- void virtual show(){
-
- };
-
- };
-
- class bicycle: virtual public vehicle {//自行车类:公有继承虚基类vehicle
-
- protected:
-
- int Height ;
-
- //高度
-
- public:
-
- void Setdata1(int ms,int wt,int ht)
-
- { MaxSpeed=ms; Weight=wt; Height=ht;};
-
- void Show(){
-
- cout<<"公共属性:maxSpeed is"<<MaxSpeed<<",weight is"<<Weight
-
- <<",Bicycle属性:height is"<<Height<<endl;
-
- }
-
- };
-
- class motorcar : public vehicle {//摩托车类:公有继承自行车类和汽车类
-
- protected:
-
- int SeatNum ; //座位数
-
- public:
-
- void Setdata(int ms,int wt,int sn)
-
- { MaxSpeed=ms; Weight=wt;SeatNum=sn; };
-
- void Show(){
-
- cout<<"公共属性:maxSpeed is"<<MaxSpeed<<",weight is"<<Weight
-
- <<",Vehicle属性:seatNum is"<<SeatNum<<endl;
-
- }
-
- };
-
- main(){
-
- bicycle mt;
-
- motorcar mr;
-
- while(1){
-
- cout<<"Bicycle请输入-0\n"<<"Motocar请输入-1\n"<<"退出结束-2\n";
-
- cout<<"请输入:";
-
- int m;
-
- cin >>m;
-
- int maxspeed1,weight1,height1,seatnum1;
-
- switch(m){
-
- case 0:
-
- cout<<"请输入maxSpeed:" ;
-
- cin>>maxspeed1;
-
- cout<<"请输入weight:";
-
- cin>>weight1;
-
- cout<<"请输入height:";
-
- cin>>height1;
-
- mt.Setdata1(maxspeed1,weight1,height1);
-
- break;
-
- case 1:
-
- cout<<"请输入maxSpeed:" ;
-
- cin>>maxspeed1;
-
- cout<<"请输入weight:";
-
- cin>>weight1;
-
- cout<<"请输入seatNum:";
-
- cin>>seatnum1;
-
- mr.Setdata(maxspeed1,weight1,seatnum1);
-
- break;
-
- case 2:
-
- mt.Show();
-
- mr.Show();
-
- system("pause");
-
- return 0;
-
- }
-
- }
-
- }
-
-
-
复制代码
输入
0
1
2
3
1
5
6
7
2
输出
Bicycle请输入-0
Motocar请输入-1
退出结束-2
请输入:0
请输入maxSpeed:1
请输入weight:2
请输入height:3
Bicycle请输入-0
Motocar请输入-1
退出结束-2
请输入:1
请输入maxSpeed:5
请输入weight:6
请输入seatNum:7
Bicycle请输入-0
Motocar请输入-1
退出结束-2
请输入:2
公共属性:maxSpeed is1,weight is2,Bicycle属性:height is3
公共属性:maxSpeed is5,weight is6,Vehicle属性:seatNum is7 |
上一篇: 静态库编译后在实例程序中编译运行没有报错,但是mfc中报语法错误下一篇: Windows编程的数据类型
|