hiding overloaded functionswe cannot overload a


Hiding overloaded functions

We cannot overload a base class function by redefining it in a derived class with a dissimilar argument list. Consider examples to see if similar function name exists in base as well as derived classes.

e.g.

                class base

                 {

                  public :

                                                void f(int n )

                                                 {

                                                                cout << " n = " << n;

                                                 }

                 };

                class derived : public base

                 {

                  public :a

                                                void f(float n )

                                                 {

                                                                cout << " n = " << n;

                                                 }

                  };

                                void main()

                 {

                  derived d;

                                                d.f(1);

                }

Output :

                  n = 1.000000

Even though we have passed an integer, the compiler uses the function of the derived class because it is the one, compiler knows about. If there is no convincible match within the scope of the derived class, the inherited members from the class will not be examined. The following example shows this.

                class base

                 {

                  public :

                                                void f(char *ptr)

                                                 {

                                                                cout << " ptr = " << ptr;

                                                 }

                 };

 

                class derived : public base

                 {

                  public :

                                                void f(float n )

                                                 {

                                                                cout << " n = " << n;

                                                 }

                  };

                void main()

 {

                derived d;

 

                                d.f("A");

 }

Output:

                'Can not convert 'char*' to 'double'

 

Though, we can still access the hidden members from the base class using the scope resolution operator. So in the main if we call

d.base::f("A");

The output will be :

ptr = A

 

Request for Solution File

Ask an Expert for Answer!!
C/C++ Programming: hiding overloaded functionswe cannot overload a
Reference No:- TGS0309442

Expected delivery within 24 Hours