Say I have an abstract class “Base”, of which another abstract class “Subclass” extends. Base has a number of abstract member functions, and “Subclass” implements a number of them, but leaves it’s own subclasses to provide the implementation of others. Should I include the signature of the member functions which I don’t implement in the intermediate class or not?
In other words, assuming I have the following class structure:
Car.hpp:
class Car {
public:
virtual std::string getMake() = 0;
virtual std::string getType() = 0;
virtual ~Car { };
}
SportsCar.hpp:
class SportsCar : public Car {
public:
std::string getType();
// Do I need to specify virtual std::string getMake() = 0; here?
}
SportsCar.cpp:
std::string SportsCar::getType()
{
return "sports";
}
FerrariSportsCar.hpp:
class FerrariSportsCar : public SportsCar {
public:
std::string getMake();
}
FerrariSportsCar.cpp:
std::string FerrariSportsCar::getMake()
{
return "ferrari";
}
Should I still need include virtual std::string getMake() = 0; in SportsCar.hpp?
The code compiles whether I include it or not, and the program seems to execute exactly the same.
You don’t need to specify the abstract method again in the intermediate class. It will be inherited by the intermediate class as pure and still require it to be overridden in a further child.
Note that your intermediate class will still be abstract and you couldn’t create instances of it.