Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inherited member is not allowed, why? [duplicate]

The basic issue boils down to this:

// header
class Calculator 
{
public:
    virtual int calculate(int a, int b);
};

class Adder : public Calculator 
{

};

// class file
int Adder::calculate(int a, int b) {return a + b;} // gives error

Why am I not allowed to do that?

I would've assumed that since Adder inherits Calculator's members, I can just define their bodies as if they were its own, but apparently that isn't possible...

I've seen other answers in which they simply have to write the member definition again in the child class, but that to me seems like an awful lot of lines of code wasted repeating the same text over and over, so the question is...

Why can't I do it without overriding it in the child class's definition?

Also, please note that this isn't a question as to how to fix an error, it's a question about the mechanics of C++, and why something is or is not permitted to do

like image 516
Electric Coffee Avatar asked Jan 04 '14 00:01

Electric Coffee


People also ask

Which inheritance may lead to duplication?

Explanation : Multipath inheritance may lead to duplication of inherited members from a "grandparent" base class.

How does inheritance avoid duplication Java?

- [Instructor] In Java, we can achieve inheritance by using the keyword extends. The subclass will use this keyword with the superclass in its class definition to inherit all the properties and behaviors of the superclass.

Why should we avoid multiple inheritance?

Allowing multiple inheritance makes the rules about function overloads and virtual dispatch decidedly more tricky, as well as the language implementation around object layouts. These impact language designers/implementors quite a bit and raise the already high bar to get a language done, stable, and adopted.

Can a class be inherited multiple times?

Yes, it's legal to inherit the same class twice. If the inheritance is non-virtual, as in your example (I just fixed the syntax and formatting), class Base {}; class Foo : public Base {}; class Bar : public Base {}; class Baz : public Foo, public Bar {};


2 Answers

You can't declare member functions outside the class definition. Any methods you are going to define for a class (whether overriding or hiding with respect to a base class) must be declared in the class definition.

class Adder : public Calculator { };

Defines a class that derives from Calculator, but provides no additional functionality. Also you probably meant to make the Calculator::calculate method virtual. You are currently (edit: OP edited the question making calculate() virtual) hiding the base class method, rather than overriding it. It makes a difference if you are going to deal with Calculator objects that may in fact be Adder objects at runtime.

like image 144
Aaron Avatar answered Oct 13 '22 15:10

Aaron


You cannot do it because you haven't explicitly said that Adder has a calculate method - and C++ is strict about it. What would happen if you did then write Calculator::calculate in a separate source file?

like image 26
Rob Avatar answered Oct 13 '22 16:10

Rob