-4

I have an pure virtual base class and a derived class. I know I am allowed to implement a virtual (not pure) method in the base class. What I do not understand is why I HAVE to also implement the same method in the derived class if what I want is simply to use the base implementation:

#include <iostream>

using namespace std;

class Abstract {
public:
    int x;
    Abstract(){
        cout << "Abstract constructor" << endl;
        x = 1;
    }
    virtual void foo() = 0;
    virtual void bar(){
        cout << "Abstract::bar" << endl;
    }
};

class Derived : Abstract {
public:
    int y;
    Derived(int _y):Abstract(){
        cout << "Derived constructor" << endl;
    }
    virtual void foo(){
        cout << "Derived::foo" << endl;
    }
    virtual void bar(){
        Abstract::bar();
    }
};

int main()
{
   cout << "Hello World" << endl;
   Derived derived(2);
   derived.foo();
   derived.bar(); //HERE I HAVE TO DEFINE Derived::bar to use it
   return 0;
}
statquant
  • 13,672
  • 21
  • 91
  • 162
  • In `Derived` say `using Abstract::bar()`? Also make `Abstract` a `public` base class. – πάντα ῥεῖ Sep 12 '16 at 20:23
  • There's no reason why `bar` *must* be implemented in the derived class. What errors are you getting telling it's wrong? – Captain Obvlious Sep 12 '16 at 20:24
  • Your question is based on a completely incorrect premise. You don't have to reimplement non-pure member functions from base class. What made you think you have to to that? – AnT stands with Russia Sep 12 '16 at 20:25
  • 1
    If I do not implement `Derived::bar` it does not compile – statquant Sep 12 '16 at 20:25
  • 3
    Read up on private inheritance. – juanchopanza Sep 12 '16 at 20:25
  • 2
    You're using `private` inheritance. That makes `public` methods in the Base class become `private` in derived class. You have to use `public` inheritance and you can do what you want – BiagioF Sep 12 '16 at 20:26
  • 3
    Why is this question downvoted so much? It is a legitimate question with a rather small example illustrating the point. – Petter Sep 12 '16 at 20:27
  • 2
    @statquant: It is not about having to reimplement `Derived::bar`. It is about making it *accessible* through `Derived`. You made it *inaccessible* by using *private* inheritance. Your reimplementation works around that inaccessibility. But if you made it accessible from the beginning, you woudn't need that workaround. – AnT stands with Russia Sep 12 '16 at 20:28
  • 1
    Thanks all I got the public/private inheritance point that I missed ! – statquant Sep 12 '16 at 20:29

1 Answers1

4

You don’t have to do that. You can do the following:

class Derived : public Abstract {

That way, you can use the public methods from the base class.

Petter
  • 37,121
  • 7
  • 47
  • 62
  • 1
    That's a comment at best. – πάντα ῥεῖ Sep 12 '16 at 20:23
  • 1
    I tried to not implement `Derived::bar` but then it does not compile – statquant Sep 12 '16 at 20:23
  • If you're going to ninja edit start with something more than a comment requesting clarification. – Captain Obvlious Sep 12 '16 at 20:25
  • 1
    Ahhh ok ! indeed this public changes everything ! Thanks ! As usual on C++ SO you get -10 with no reason... when you do not know can can you do ? – statquant Sep 12 '16 at 20:27
  • @statquant Yeah it is pretty strange. Your question was well formulated IMO. – Petter Sep 12 '16 at 20:30
  • @statquant You're experiencing the _c++ sharktank_, where I'm part of it. We expecting a [MCVE] and in depth research shown, before asking such questions. – πάντα ῥεῖ Sep 12 '16 at 20:35
  • 1
    That's fine I'm happy to pay for the lightening fast answers – statquant Sep 12 '16 at 20:36
  • 3
    The question text and formulation is good. The misunderstanding that lead to the asking of the question is the problem. [This is covered early on in any good C++ text.](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) And yes, you should read the text before asking. Insufficient research is reasonable cause for a downvote. – user4581301 Sep 12 '16 at 20:37
  • 2
    @user4581301 this is too subjective I think... surely we cannot expect people to read a C++ book before asking questions. unsufficient research should relate to something like top 3 google hit on a easy research. – statquant Sep 12 '16 at 20:43
  • The problem with Googling the answer is you didn't know enough to know what to Google for. That's where books come in very, very handy. Without the initial terminology kick you get from some guided learning, you're screwed. Even if you do find something, it's a crap shoot whether or not the presented solution is good if you don't know what good is. Start with the book. Internet later. – user4581301 Sep 12 '16 at 21:03