#include<iostream> using namespace std; class Vehicle { protected: int speed; int wheel; public: Vehicle(){} virtual void Display() = 0; }; class Bicycle :virtual public Vehicle { public: Bicycle(int s, int w) { s = speed, w = wheel; } void Display() { cout << "Bicycle:" << speed << "," << wheel << endl; } }; class Car :virtual public Vehicle { public: Car(int s, int w) { s = speed, w = wheel; } void Display() { cout << "Car:" << speed << "," << wheel << endl; } }; class Motocycle :public Bicycle, public Car { public: Motocycle(int s, int w):Bicycle(s,w),Car(s,w){} void Display() { cout << "Motocycle:" << speed << "," << wheel << endl; } }; int main() { Bicycle b(20, 2); b.Display(); Car c(40, 4); c.Display(); Motocycle m(30, 2); m.Display(); return 0; }运行结果为什么不对
时间: 2024-03-24 09:35:58 浏览: 56
程序的运行结果不对是因为在 Bicycle 和 Car 类的构造函数中,将传入的参数赋值给了成员变量时,出现了错误。正确的做法应该是将成员变量和传入的参数名字进行区分,如下所示:
```
class Bicycle :virtual public Vehicle {
public:
Bicycle(int s, int w) { speed = s, wheel = w; }
void Display() { cout << "Bicycle:" << speed << "," << wheel << endl; }
};
class Car :virtual public Vehicle {
public:
Car(int s, int w) { speed = s, wheel = w; }
void Display() { cout << "Car:" << speed << "," << wheel << endl; }
};
```
在 Motocycle 类的构造函数中也需要对 Bicycle 和 Car 的构造函数进行调用,正确的写法如下:
```
Motocycle(int s, int w):Bicycle(s,w),Car(s,w),Vehicle(){}
```
修改后的程序运行结果如下:
```
Bicycle:20,2
Car:40,4
Motocycle:30,2
```
相关问题
#include<iostream> using namespace std; class vehicle { private: int MaxSpeed; int Weight; public: virtual void Run() { cout<<"Run"<<endl; } virtual void Stop() { cout<<"Stop"<<endl; } }; class bicyle:public vehicle { private: int Height; }; class motorcar:public vehicle { private: int SeatNum; }; class motocycle { }; int main(){ motocycle a; a.Run(); a.Stop(); return 0; }有什么问题
在这段代码中,motocycle类没有继承vehicle类,因此它没有Run()和Stop()方法。在main()函数中,使用motocycle类的对象a调用Run()和Stop()方法是不合法的。如果想要使用Run()和Stop()方法,可以让motocycle类继承vehicle类或者让motocycle类成为vehicle类的派生类之一。
补充以下代码,完成函数定义,使其可以正常执行。 #include<iostream> using namespace std; class vehicle{ protected: int size; int speed; public: void set(int s){ speed=s; } (1) get(){ return speed/10; } }; class car:public vehicle{ public: int get(){ return speed; } }; class truck:public vehicle{ public: int get(){ return speed/2; } }; int max( (2) ){ //形参为引用类型 if(v1.get()>v2.get()) return 1; else return 2; } int main() { truck t; car c; t.set(160); c.set(80); cout<<max(t,c)<<endl; //输出:2 return 0; }
下面是补充完整的代码:
```
#include<iostream>
using namespace std;
class vehicle{
protected:
int size;
int speed;
public:
void set(int s){
speed=s;
}
int get(){
return speed/10;
}
};
class car:public vehicle{
public:
int get(){
return speed;
}
};
class truck:public vehicle{
public:
int get(){
return speed/2;
}
};
int max(vehicle& v1, vehicle& v2){ // 形参为引用类型
if(v1.get()>v2.get())
return 1;
else
return 2;
}
int main()
{
truck t;
car c;
t.set(160);
c.set(80);
cout<<max(t,c)<<endl; // 输出:2
return 0;
}
```
需要注意的是:
1. 在 `vehicle` 类中,`get()` 函数返回的是 `speed/10`,所以其返回值类型应该是 `int`。
2. 在 `max()` 函数中,参数类型应该是 `vehicle&`,即引用类型。
3. 在 `car` 和 `truck` 类中,都重载了 `get()` 函数,返回不同的值。
阅读全文