我已经公开了父类的成员函数。但是我还是得到一个错误
没有在类'MotorBike'中声明的'void MotorBike :: speeddown()'成员函数speedup
和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);
}
最佳答案
您的MotorBike
没有功能speedup
或speeddown
。如果需要,您必须在MotorBike
类中声明它,例如
class MotorBike:public Bicycle
{
private:
int currentGear;
int currentFuel;
public:
MotorBike(int,char*,int,int);
void speedup();
void speeddown();
};