I would like to ask a question about the programming style in this case of the derived class:
class A { public: virtual foo1()=0; } class B: public A { public: virtual foo1(); virtual foo2(); } class C: public A { public: virtual foo1(); } int main() { B mB(); C mC(); mB.foo2() //OK! mC.foo2() // obviously, it is not correct return 0;}
Therefore, should a derived class have less or equal public methods than an abstract base class?
If derived classes require more methods, should they be private?
There is nothing wrong with this class structure. There is nothing wrong with the fact that the derived class has more methods than the parent class - it is quite commonplace. The string mC.foo2();is incorrect, and this is not a class error.
mC.foo2();
, . : , , .
. (, , ). , , . (foo2 A). . A, .
foo2
A
class Intermediate : public A { public: virtual foo1()=0; virtual foo2()=0; } class B: public Intermediate { public: virtual foo1(); virtual foo2(); }
, foo2, Intermediate, , foo2, Intermediate.
Intermediate
. . , ., , ( IS-A -relationships). private inheritance, .
IS-A
private inheritance
In almost ALL projects that I worked on, we have base classes that have less functionality than a derived class. In extreme cases, the base class may even have almost no functionality, but the derived class has dozens of functions and half a dozen member variables.
This is exactly what derived classes should do.
Obviously, you need to KNOW what kind of derived class you have, and only use the "extra" functions when the derived classes are available.