Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overloading a virtual function in a child class

Tags:

I am just testing with virtual keyword and inheritance concepts in c++. I have written a small program:

#include<stdio.h> #include<iostream>  using namespace std;  class cna_MO {   public:     virtual void print()     {         cout << "cna_MO" << endl;     } };  class cna_bsc:public cna_MO {   public:     void print()     {         cna_MO::print();     }      void print(int a)     {         cout << "cna_BSC" << endl;     } };  class cna_Mo {     cna_MO *_mo;    public:     cna_Mo()     {         _mo = new cna_bsc;     }      virtual void print(int a)     {         cout << "cna_Mo with arg" << endl;         _mo->print(5);     }      virtual void print()     {         cout << "cna_Mo" << endl;         _mo->print();     } };  int main() {     cna_Mo valid_mo;     cout << "old case is started" << endl;     valid_mo.print();     cout << "new case is started" << endl;     valid_mo.print(5);     return 0; } 

What I have done here is I have overloaded a virtual function in parent class in child class! Is this not the right thing to do?

I am getting the compilation errors as below:

"temp10.cc", line 45: Error: Too many arguments in call to "cna_MO::print()".

like image 773
Vijay Avatar asked Jan 11 '12 09:01

Vijay


People also ask

Can a virtual function be overloaded?

It is not possible for these functions to get overloaded.

Which virtual function is example of function overloading?

Example 2: Overloading Using Different Number of Parameters Note: In C++, many standard library functions are overloaded. For example, the sqrt() function can take double , float , int, etc. as parameters. This is possible because the sqrt() function is overloaded in C++.

How do you overload a class function?

Function Overloading in C++ You can have multiple definitions for the same function name in the same scope. The definition of the function must differ from each other by the types and/or the number of arguments in the argument list. You cannot overload function declarations that differ only by return type.

Can virtual function be declared in derived class?

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.


1 Answers

Once you overload a function from Base class in Derived class all functions with the same name in the Base class get hidden in Derived class.

Once you added the function cna_bsc::print(int a) to your derived class the function cna_MO::::print() is no longer visible to users of the Derived class. This is known as function hiding.

Solution: In order to make the hidden function visible in derived class, You need to add:

using cna_MO::print; 

in the public section of your derived class cna_bsc.

Good Read:

What's the meaning of, Warning: Derived::f(char) hides Base::f(double)?

like image 83
Alok Save Avatar answered Nov 01 '22 22:11

Alok Save