0

Hello I am trying to learn c++ from a book "C++ an introduction to programming by Jesse Liberty and Jim Keogh" I am doing the questions for chapter 12 multiple inheritance. Q4 asks me to derive car and bus from vehicle with car as an adt and then derive sportscar, wagon and coupe from car and implement a non pure virtual function from vehicle in car. It is compiling on codelite but at build time it gives the error

undefined reference to 'vtable for Coupe' on the constructor and destructor for Coupe

Please can anyone tell me what I am doing wrong so I can learn more about how to handle virtual function definitions correctly with vtables.

#include <iostream>

using namespace std;

class Vehicle
{
public:
Vehicle(){};
virtual ~Vehicle(){};
virtual int GetItsSpeed() = 0;
int GetItsTyreSize();
virtual int GetItsRegistration() { return ItsRegistration; }
protected:
int ItsSpeed;
int ItsRegistration;
};

class Car : public Vehicle
{
public:
Car(){};
virtual ~Car(){};
virtual int GetItsSpeed() = 0;
int GetItsBootSize() { return ItsBootSize; }
int GetItsRegistration() { return ItsRegistration; }
virtual int GetItsRadioVolume() = 0;
int GetItsTyreSize() { return ItsTyreSize; }
protected:
int ItsBootSize;
int ItsRadioVolume;
int ItsTyreSize;
};

class Bus : public Vehicle
{
Bus(){};
~Bus(){};
public:
int GetItsSpeed() { return ItsSpeed; }
int GetItsPassengerSize() { return ItsPassengerSize; }
private:
int ItsPassengerSize;
};

class SportsCar : public Car
{
public:
SportsCar(){};
~SportsCar(){};
int GetItsSpeed() { return ItsSpeed; }
int GetItsPowerSteeringAccuracy() { return ItsPowerSteeringAccuracy; }
private:
int ItsPowerSteeringAccuracy;
};

class Wagon : public Car
{
public:
Wagon(){};
~Wagon(){};
int GetItsSpeed() { return ItsSpeed; }
int GetItsTrailerSize() { return ItsTrailerSize; }
private:
int ItsTrailerSize;
};

class Coupe : public Car
{
public:
Coupe(){ ItsTyreSize = 10; }
~Coupe(){};
int GetItsSpeed();
int GetItsInteriorStyle() { return ItsInteriorStyle; }
int GetItsRadioVolume() { return ItsRadioVolume; }
private:
int ItsInteriorStyle;
};

void startof()
{
Coupe MariesCoupe;
cout << "Maries Coupe has a tyre size of "
<< MariesCoupe.GetItsTyreSize() << " .\n\n";

}

int main()
{
startof();
return 0;
}
abhishek_naik
  • 1,287
  • 2
  • 15
  • 27
AlleyCat
  • 41
  • 6

1 Answers1

2

You should implement GetItsSpeed in Coupe. For example,

int Coupe::GetItsSpeed() {return 0;};
Shangtong Zhang
  • 1,579
  • 1
  • 11
  • 18