I'm trying to write a program with four classes: vehicle, car, airplane and flying car.
Flying car inherits from car and airplane, car and airplane both inherit from vehicle.
vehicle.h
class vehicle
{
public:
vehicle();
vehicle(char*, int, char*);
virtual void setmodel(char*);
//more functions..
~vehicle();
protected:
char* model;
int speed;
char* color;
};
car.h
#include "vehicle.h"
class car:public virtual vehicle
{
public:
car();
car(char*, int, char*);
void driving(std::ostream&);
};
airplane.h
#include "vehicle.h"
class airplane:public virtual vehicle
{
public:
airplane();
airplane(char*, int, char*);
void flying(std::ostream&);
};
flyingCar.h
#include "car.h"
#include "airplane.h"
class flyingCar: public car, public airplane
{
public:
flyingCar();
flyingCar(char*, int, char*);
};
Also, the source files for every class include the related header files (so thart vehicle.cpp
includes vehicle.h
, car.cpp
includes car.h
and so on.. ).
The main function includes the vehicles.h
header and ifndef..
.
When I compile I get an error for redefinition of class vehicle in car.h, but since I already used the virtual
I don't understand how I could solve the problem.
I suspect including the class headers in the class source files may be incorrect, since the inherited headers are already included in the inheriting headers themselves, but if I don't include them here the compiler will not refer to the class prototypes. As an attempt I enclosed everything in ifndef..
with no positive result.