Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why must I re-declare a virtual function from an inherited class?

I'm working on a simple C++ program and am having a difficult time understanding a compiler error I was getting. The issue was caused by me attempting to create a derived class from a base class. I've posted my code below with the same structure but have changed the names.

BaseClass.h

#ifndef BASECLASS_H
#define BASECLASS_H

class BaseClass {

    public:
        BaseClass(void);

        virtual int method1(void) = 0;
        virtual int method2(void) = 0;
        virtual float method3(void) = 0;

};

#endif // BASECLASS_H

DerivedClass.h

#ifndef DERIVEDCLASS_H
#define DERIVEDCLASS_H

#include "DerivedClass.h"

class DerivedClass: public BaseClass
{

    public:
        DerivedClass(void);     
};

#endif // DERIVEDCLASS_H

DerivedClass.cpp

#include "DerivedClass.h"

DerivedClass::DerivedClass(void)
{
}

int DerivedClass::method1(void)
{
  // TODO
} 

int DerivedClass::method2(void)
{
  // TODO
}

float DerivedClass::method3(void) 
{
  // TODO
}

When attempting to compile this, I get the following error for all the virtual methods:

no 'int DerivedClass::methodX()' member function declared in class 'DerivedClass'

As soon as I declare these methods in the 'DerivedClass.h', the errors go away since the compiler is now aware of the methods.

However, I'm confused. Why was it necessary to re-declare the pure virtual functions in DerivedClass.h? When I #include DerivedClass.h, that will automatically include BaseClass.h, thus I assume my DerivedClass.cpp should be fully aware of the methods. Am I doing something incorrect?

like image 483
Izzo Avatar asked Oct 18 '16 22:10

Izzo


People also ask

Why are virtual member functions needed in inheritance?

A virtual function allows derived classes to replace the implementation provided by the base class. The compiler makes sure the replacement is always called whenever the object in question is actually of the derived class, even if the object is accessed by a base pointer rather than a derived pointer.

What happens if we don't use a virtual function in the inheritance?

If you don't use virtual functions, you don't understand OOP yet. Because the virtual function is intimately bound with the concept of type, and type is at the core of object-oriented programming, there is no analog to the virtual function in a traditional procedural language.

Is it mandatory to override virtual function in derived?

The virtual keyword can be used when declaring overriding functions in a derived class, but it is unnecessary; overrides of virtual functions are always virtual. Virtual functions in a base class must be defined unless they are declared using the pure-specifier.

Can virtual function be inherited?

Base classes can't inherit what the child has (such as a new function or variable). Virtual functions are simply functions that can be overridden by the child class if the that child class changes the implementation of the virtual function so that the base virtual function isn't called. A is the base class for B,C,D.


1 Answers

When you declare a method in a derived class, you say to the compiler:

I want to override this method in this class

So if you don't declare a method in the derived class, you say:

I don't want to override this method; derived class's implementation is the same as the one in the base class

In your case, the base class declares them as pure virtual, so in this case it can be paraphrased:

I don't want to implement this method in this class

If you try to define a method but not declare it, you contradict yourself. The compiler detects that (to protect you from your own negligence).

like image 185
anatolyg Avatar answered Nov 15 '22 19:11

anatolyg