0

コードは次のとおりです。

   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;
      }

主に、関数を呼び出すとanalyzeJetPerformance (& myJetCar )、コンパイラは私が期待したものを提供しませJat flyingん。JetCar flyingここで何が問題なのですか?

4

1 に答える 1

2

Jet には次のものがあります。

   virtual void fly () const {
         cout << " Jet flying " << endl ;
                   }

JetCarにいる間、次のことができます:

   virtual void fly () {
          cout << " JetCar flying " << endl ;
                       }

それらは同じ機能ではありません。関数宣言を同じにすると、機能するはずです。

于 2012-05-14T23:34:30.703 に答える