1

I would like to ask a question about programming style in this case of 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 the abstract base class?

If the derived classes require more methods, should these be private?

Infinite Recursion
  • 6,511
  • 28
  • 39
  • 51
Ale
  • 1,089
  • 2
  • 11
  • 16
  • One of the reasons for creating a derived class is to add functionality. It doesn't make sense to do that and then insist that the additional functionality cannot be used by an external client. – Roger Rowland Mar 13 '13 at 14:08
  • 1
    what you have here is an example of an [interface](http://stackoverflow.com/questions/761194/interface-vs-abstract-class-general-oo). Interfaces are used to add functionality to a class. So A is doing its job by add the `foo1` functionality. – andre Mar 13 '13 at 14:36

4 Answers4

2

Derived classes will almost always have more public functions than base classes. This is the point of inheritance: you can define an abstract base class which only outlines the basic behavior of a variable, then derived classes can expand upon this basic behavior for specific cases.

An inherited class is always a specialization of the base class. It implements more specific functions (and usually more functions all together). In you're example, you're expecting two different specializations to behave the same way outside of the behavior defined by the base class. (foo2 is not defined in A). That's where the problem lies. If you need to define common behavior outside of A, the solution would be to create an intermediate class.

class Intermediate : public A
{
public:
    virtual foo1()=0;
    virtual foo2()=0;
}

class B: public Intermediate
{
public:
    virtual foo1();
    virtual foo2();
}

Now any class which can implement foo2 should extend Intermediate, and any function which requires functionality foo2 should ask for a variable with at least type Intermediate.

JSQuareD
  • 4,641
  • 2
  • 18
  • 27
0

There is nothing wrong with this class structure. There is nothing wrong with a derived class having more methods than the parent class-- it's quite commonplace. The line mC.foo2(); is just wrong, and that is not the fault of the classes.

Beta
  • 96,650
  • 16
  • 149
  • 150
0

A derived class must at least implement ALL abstract methods from the base class. This is the minimum. If you do add other methods, members or whatever is up to you.
But it would be not very smart to derive from the class and add nothing, because this is not what inheritance is for (at least IS-A-relationships). If you go for private inheritance it might be different.

bash.d
  • 13,029
  • 3
  • 29
  • 42
  • one solution could be: abstract class with all basic abstract methods (set and get) and derived classes that implement all abstract methods adding functionality just by means of private methods – Ale Mar 13 '13 at 14:22
0

In nearly ALL projects I've worked on, we have had baseclasses that have less functionality than the derived class - in extreme cases, the baseclass may even just have nearly no functionality, but the derived class has dozens of functions and a half a dozen member variables.

This is exactly what derived classes are meant to do.

Obviously, you need to KNOW what kind of derived class you have, and only use the derived classes "extra" functions when they are available.

Mats Petersson
  • 126,704
  • 14
  • 140
  • 227
  • Yes, I wonder if this can be considered a bad code since it can generates a bug. – Ale Mar 13 '13 at 14:18
  • 1
    It's not a bug - it's a compile time error, which is MUCH better on a scale of bad things. Clearly, if `C` doesn't need a `foo2()` method, then it shouldn't have one. There may of course be cases where you do want all of the derived classes to be "the same". But it's not wrong to have a class that have members/functions that aren't in others. – Mats Petersson Mar 13 '13 at 14:21