0

So assume there is class A with function foo.
Class A is the parent of a few classes which all have the function foo.
All of the child classes need the functionality of A::foo and adds upon it.
For example:

    class A
    {
    public:
      void foo()
      {
         print('A');
      }
    };

    class B:public A
    {
    public: 
       void foo()
       {
          print("B");
       }
    };
    class C:public A
    {
    public:
      void foo()
        {
           print("C");
        }
    };
    void main()
    {
       B b;
       C c;
       c.foo()// now only goes to C::foo(), meaning print C. I want it to to also go into A::foo() meaning print AC
       b.foo()//now only goes to B::foo(),meaning print B. want it to print AB
    }

And if I want to add class D : A with foo function it will also do A::foo() and then D::foo()

Sorry if I am missing something obvious.

EDIT : since it wasn't clear the question was if there is an automatic way to do so.

EDIT : found a workaround:

    class A
    {
    virtual void foo2(){}
    public:
      void foo()
      {
         print('A');
         foo2();
      }

    };

    class B:public A
    {
       void foo2()
       {
          print("B");
       }
    public: 
    };
    class C:public A
    {
              void foo2()
        {
           print("C");
        }
        public:
    };
    void main()
    {
       B b;
       C c;
       c.foo()// now prints AC
       b.foo()//now prints AB
    }

seems redundant since now there are 2 functions now.

shaiel cohen
  • 41
  • 1
  • 9

2 Answers2

3

You can call the parent class's implementation of a function using A::foo(). In your case, simply adding that function call will achieve the result you want:

class B:public A
{
public: 
   void foo()
   {
      A::foo();
      print("B");
   }
};

As a side note, whenever you intend to override functions, you should declare it as virtual:

 class A
{
public:
  virtual void foo()
  {
     print('A');
  }
};

And then when overriding it, use the override keyword:

class B:public A
{
public: 
   void foo() override
   {
      print("B");
   }
};
xEric_xD
  • 506
  • 1
  • 5
  • 12
  • thx for the answear! EDIT: but you missed my point. I will have to do this in EVERY class that inherits A. My question is is there a way to set that for ALL derived classes. – shaiel cohen Nov 22 '17 at 15:05
  • There's no way to do it automatically, you'll just have to do this for every derived class – xEric_xD Nov 22 '17 at 15:07
1

What you've called a workaround is an established solution to this question, and whilst some people might call it a workaround many wouldn't.

Its even got a name that covers it. The non-virtual interface pattern.

herb sutter :- http://www.gotw.ca/publications/mill18.htm

One example from S.O., there's probably more:- Non-virtual interface design pattern in C#/C++

I'd argue that this IS a way to do it automatically.

As to your aside that its redundant because there are 2 functions - well there were 2 before (the base version and derived version).

One benefit of this approach in some designs is that if the virtual method foo2 is pure virtual (which would make A abstract) then this forces all immediate derived classes to implement foo2. (I say immediate because if B derives from A it must implement foo2 but if C then derives from B C isn't forced to implement foo2, it has access to B::foo2 )

ROX
  • 1,256
  • 8
  • 18
  • the only question is does it compile the same way that a virtual function will meaning only compiling the relevant function when it's called or will this compile as a function within a function – shaiel cohen Nov 22 '17 at 20:55