Reputation: 23
I have a specific class structure in mind, though I feel like it could become a little messy when the project gets bigger. The general idea is the following:
There is a class for all possible vehicles with basic implementations that do not change for any vehicle, regardless of its specific type. Then, for all the cars, there should be an interface with functions only cars can have. This interface is purely virtual though. Then, any car can implement these functions as needed. I included a very simplistic code example below:
class Vehicle { //general class for a vehicle
public:
void move() {
std::cout << "I moved\n";
}
void crash() {
std::cout << "I crashed\n"
}
};
class CarInterface : public Vehicle { //purely virtual interface for cars
public:
virtual void refuel() = 0;
};
class Ferrari: public CarInterface { //implementation: one specific car
public:
void refuel() override {
std::cout << "I use ferrari fuel\n"
};
};
In the c++ guidelines, it says you should distinguish between implementation inheritance and interface inheritance. Does this still fulfill that requirement?
Upvotes: 0
Views: 924
Reputation: 238311
Can a C++ interface inherit from a class with full implementation?
The C++ language doesn't distinguish between interfaces and other classes. The distinction is purely conventional. Thus from one perspective it would be correct to say that C++ interface cannot [whatever] because there is no such thing as C++ interface.
If we consider an interface as an abstract concept, then the answer depends on how one chooses to interpret the concept, and to precisely specify it in terms of the language. If the interpretation is that an interface class has only pure virtual member functions (and this is a common interpretation), then CarInterface
is not an interface because it has non-pure-virtual member functions through inheritance.
In the c++ guidelines
I suppose that you intend to refer to the "C++ Core Guidelines". This is how the concepts are defined there:
Note Definition:
- interface inheritance is the use of inheritance to separate users from implementations, in particular to allow derived classes to be added and changed without affecting the users of base classes.
- implementation inheritance is the use of inheritance to simplify implementation of new facilities by making useful operations available for implementers of related new operations (sometimes called “programming by difference”).
A pure interface class is simply a set of pure virtual functions; see I.25.
This is a little bit vague, but I would interpret that CarInterface
is a form of both interface inheritance, and implementation inheritance, and it is not a pure interface.
Below the definitions, as well as in section I.25, the guidelines demonstrate such mixed inheritance as a bad example, and suggests an alternative that uses a pure interface instead.
Your example could achieve both pure interfaces, and reuse (which is what your Vehicle
class presumably is for) by using virtual inheritance:
// pure interface
class VehicleInterface {
public:
virtual void move() = 0;
virtual void crash() = 0;
};
// pure interface
class CarInterface : public virtual VehicleInterface {
public:
virtual void refuel() = 0;
};
class CommonVehicle : public virtual VehicleInterface {
public:
void move() override;
void crash() override;
};
class Ferrari: public CarInterface, private CommonVehicle {
public:
void refuel() override;
};
Upvotes: 3