注意:
若在继承时不将Vehicle类声明为虚基类,则在对象mc调用Vehicle类中的成员变量或成员方法时就会产生二义性。因为编译器不知道该成员变量或成员方法是来自Bicycle类还是来自Motorcar类。
/*-----------------------------------------------------
功能:用虚基类来避免成员函数或成员方法调用的二义性问题
运行结果:
60
12
2
100
Black
Vehicle is running...
Vehicle is stopping...
------------------------------------------------------
Author: Zhang Kaizhou
Date: 2019-4-9 16:40:13
-----------------------------------------------------*/
#include <iostream>
#include <string>
using namespace std;
class Vehicle{ // 定义一个交通工具类
public:
int maxSpeed;
int weight;
void setMaxSpeed(int s){ maxSpeed = s; }
void setWeight(int w){ weight = w; }
int getMaxSpeed()const{ return maxSpeed; }
int getWeight()const{ return weight; }
void run(){ cout << "Vehicle is running..." << endl; }
void stop(){ cout << "Vehicle is stopping..." << endl; }
};
class Bicycle : virtual