2016-05-25 9 views
1

親クラスのメンバー関数をpublicにしました。しかし、それでもまだ、私はエラー継承を使用して親クラスの関数を定義する方法は?

'バイク' speedupspeeddownため

両方のクラスで宣言されていない '無効バイク:: speeddown()' メンバ関数を取得します。私は親クラスを公に継承していますが。

#include <iostream> 
#include<string.h> 
using namespace std; 

class Bicycle 
{ 
protected: 
    int speed; 
    char color[8]; 
public: 
    Bicycle(int, char*); 
    void speedup(); 
    void speeddown(); 
    void turnLeft(); 
    void turnRight(); 
}; 

class MotorBike:public Bicycle 
{ 
private: 
    int currentGear; 
    int currentFuel; 
public: 
    MotorBike(int,char*,int,int); 

}; 

Bicycle::Bicycle(int Speed,char* Color) 
{ 
    speed=Speed; 
    strcpy(color,Color); 
    cout<<"The bike is now moving north!"<<endl; 
} 

MotorBike::MotorBike(int Speed,char* Color,int CurrentGear,int CurrentFuel): Bicycle(speed,color) 
{ 
    speed=Speed; 
    strcpy(color,Color); 
    currentGear=CurrentGear; 
    currentFuel=CurrentFuel; 
    cout<<"The motorbike is now moving north!"<<endl; 
} 

void Bicycle::speedup()//error line 
{ 
    cout<<"Speed is increased!"<<endl; 
    speed+=1; 
} 

void Bicycle::speeddown()//error line 
{ 
    if(speed>1) 
    { 
     cout<<"Speed is decreased!"<<endl; 
     speed-=1; 
    } 
    else 
     cout<<"The bike is already at rest!"<<endl; 

} 

void MotorBike::speedup()//error line 
{ 
    if(currentGear==4) 
     cout<<"You are already at maximum speed!"<<endl; 
    else 
    { 
     switch(currentGear) 
     { 
     case 0: 
      currentGear=1; 
      currentFuel-=0.01; 
      speed+=0.25; 
      break; 
     case 1: 
      currentGear=2; 
      currentFuel-=0.02; 
      speed+=0.5; 
      break; 
     case 2: 
      currentGear=3; 
      currentFuel-=0.03; 
      speed+=0.75; 
      break; 
     case 3: 
      currentGear=4; 
      currentFuel-=0.04; 
      speed+=1; 
      break; 

     } 
    } 
} 

void MotorBike::speeddown()//error line 
{ 
    if(speed==1||speed>1) 
    speed-=1; 
    else 
     cout<<"You are already at minimum speed!"<<endl; 
} 

int main() 
{ 
    Bicycle B1(0,"Black"); 
    MotorBike MB1(0,"Black",0,100); 
} 

答えて

1

あなたMotorBike機能speedupspeeddownを持っていません。あなたが希望したい場合は、MotorBikeクラスでそれを宣言する必要があり、

class MotorBike:public Bicycle 
{ 
private: 
    int currentGear; 
    int currentFuel; 
public: 
    MotorBike(int,char*,int,int); 
    void speedup(); 
    void speeddown(); 
}; 
+0

私の自転車にはそれがあり、私は自転車から継承しています。 –

+0

@AhmadShafique、true - しかしあなたはすでに定義を継承しています。子孫クラスに異なる定義を提供する場合は、**それらをオーバーライドする必要があります。 – SergeyA

+0

はい。ありがとう! –

0

ように私はこの1つだけに上から下に行くつもりです:

#include <string>にご非推奨#include <string.h>を変更します。

あなたの自転車には何の定義もない機能があり、何かを呼び出そうとするとLNK2019(未解決の外観)が出ます。定義するか、削除するか、自転車を抽象クラスにするかのいずれかです。自転車にも子供がいますので、仮想デストラクタを付けてください。

Bicycle(int, char*); 
virtual ~Bicycle() = default; 
virtual void speedup(); 
virtual void speeddown(); 
virtual void turnLeft(); 
virtual void turnRight(); 

MotorBikeのメンバーであるが、決して宣言していない機能を定義しています。

class MotorBike:public Bicycle 
{ 
private: 
    int currentGear; 
    int currentFuel; 
public: 
    Motorbike(int,char*,int,int); 
    void speedup(); 
    void speeddown(); 
}; 

使用バイクで自転車の機能をオーバーライドoverrideキーワード:

class MotorBike:public Bicycle 
{ 
private: 
    int currentGear; 
    int currentFuel; 
public: 
    Motorbike(int,char*,int,int); 
    void speedup() override; // like this 
    void speeddown() override; // and this 
}; 

あるいは、バイクに自転車で実装されるデフォルトの機能を使用します。

バイクの宣言に追加します
class MotorBike:public Bicycle 
{ 
private: 
    int currentGear; 
    int currentFuel; 
public: 
    Motorbike(int,char*,int,int); 
    using Bicycle::speedup; // like this 
    using Bicycle::speeddown; // and this 
}; 

データタイプを修正してください。 MotorBikeはcurrentGearcurrentFuelを整数として宣言しますが、たとえばMotorBike::speedupには浮動小数点値を追加しています。これらの値の小数部分が切り捨てられるため、これは機能しません。

関連する問題