我尝试使用我的几个例子创建一些多态类,我尽可能地遵循它们并且不得不解决它,不太确定为什么这不是编译。
#include<string>
#include<iostream>
#include<vector>
#include<memory>
#include<ctime>
using namespace std;
class Vehicle {
public:
string colour;
int wheels;
//deconstructor
virtual ~Vehicle(){}
virtual void setColour(string col) {}
virtual string getColour() { return colour; }
virtual int getWheels() { return wheels; }
virtual void setWheels(int numWheels){};
Vehicle(int numWheels, string col) : wheels(numWheels), colour(col){}
};
class Auto : public Vehicle{
public:
Auto(int numWheels, string col) : Vehicle(numWheels, col){}
void setWheels(int numWheels){ wheels = numWheels; }
void setColour(string col){ colour = col; }
int getWheels();
string getColour();
};
class Bike : public Vehicle{
private:
string sCol;
int iWheel;
public:
Bike(int numWheels, string col) : Vehicle(numWheels, col){}
void setWheels(int numWheels){ wheels = numWheels; }
void setColour(string col){ colour = col; }
int getWheels();
string getColour();
};
int main(){
Auto car1(4, "white");
cout << car1.getColour() << endl;
Bike bike1(2, "grey");
cout << bike1.getColour() << endl;
}
我得到了一个奇怪的错误,我有一个大约12个小时的期末考试,我不知道为什么这个C ++代码不会编译。