[英]Multiple inheritance diamond
Here is the code: 这是代码:
class Vehicle {
public :
Vehicle () {
cout << " Vehicle Constructor " << endl ;
}
virtual ~ Vehicle () {
cout << " Vehicle Destructor " << endl ;
}
virtual void accelerate () {
cout << " Vehicle Accelerating " << endl ;
}
void setAcceleration ( double a ) {
acceleration = a;
}
double getAcceleration () {
return acceleration ;
}
protected :
double acceleration ;
};
class Car : public Vehicle {
public :
Car () {
cout << " Car Constructor " << endl ;
}
virtual ~ Car () {
cout << " Car Destructor " << endl ;
}
virtual void accelerate () {
cout << " Car Accelerating " << endl ;
}
virtual void drive () {
cout << " Car Driving " << endl ;
}
};
class Jet : public Vehicle {
public :
Jet () {
cout << " Jet Constructor " << endl ;
}
virtual ~ Jet () {
cout << " Jet Destructor " << endl ;
}
virtual void fly () const {
cout << " Jet flying " << endl ;
}
};
class JetCar : public Car , public Jet {
public :
JetCar () {
cout << " JetCar Constructor " << endl ;
}
virtual ~ JetCar () {
cout << " JetCar Destructor " << endl ;
}
virtual void drive () {
cout << " JetCar driving " << endl ;
}
virtual void fly () {
cout << " JetCar flying " << endl ;
}
};
void analyzeCarPerformance ( Car * testVehicle ) {
testVehicle -> drive () ;
}
void analyzeJetPerformance ( Jet * testVehicle ) {
testVehicle -> fly () ;
}
int main () {
Car myCar ;
Jet myJet ;
JetCar myJetCar ;
cout << endl << endl ;
cout << " Car testing in progress " << endl ;
analyzeCarPerformance (& myCar );
analyzeCarPerformance (& myJetCar ) ;
cout << " Jet testing in progress " << endl ;
analyzeJetPerformance (& myJet );
analyzeJetPerformance (& myJetCar ) ;
cout << endl << endl ;
return 0;
}
In the main when I call the function analyzeJetPerformance (& myJetCar )
the compiler gives me Jat flying
not JetCar flying
what I expected. 在主要的当我调用函数
analyzeJetPerformance (& myJetCar )
,编译器给我Jat flying
而不是JetCar flying
我所期望的。 What is a problem here? 这有什么问题?
In Jet you have the following: 在Jet你有以下几点:
virtual void fly () const {
cout << " Jet flying " << endl ;
}
while in JetCar you have: 在JetCar中你有:
virtual void fly () {
cout << " JetCar flying " << endl ;
}
those are not the same function. 那些功能不一样。 Make the function declaration the same and it should work.
使函数声明相同,它应该工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.