Explain hiding overloaded functions, C/C++ Programming

Assignment Help:

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

 


Related Discussions:- Explain hiding overloaded functions

Mr. D. Mclean, A Network of routers have been configured for the ...

A Network of routers have been configured for the purposes of handling data traffic within your company. You would like to have an application that does a network

Random question, Ask question #write statement that assign random integer t...

Ask question #write statement that assign random integer to the varaible n in the (100

If i wish a local to "die" before the close} of the scope, What if I wish a...

What if I wish a local to "die" before the close} of the scope wherein it was created? Can I call a destructor on a local if I want to?

201 it, overload assignment opertor to assign the data of one object to ant...

overload assignment opertor to assign the data of one object to anthor

Symbolic constant, S y m b o li c c o n s t a n t : c ...

S y m b o li c c o n s t a n t : c on s t m a x = 1 0 0 ; T h e d e f a u l t d a t a t y p e w i l l b e i n t e

#Class objects, make use of class objects two calculate average

make use of class objects two calculate average

Fraction coding, Create a program that asks the user for two integers which...

Create a program that asks the user for two integers which represent the numerator and denominator parts of fraction. Print out a simplification of the fraction. Example: Enter th

Need android app development, Project Description: I am seeking a develo...

Project Description: I am seeking a developer who can start an app from scratch and get it delivered to me as soon as possible. It is a little android based app. A background on

Decode the code, Smugglers are becoming very smart day by day. Now they hav...

Smugglers are becoming very smart day by day. Now they have developed a new technique of sending their messages from one smuggler to another. In their new technology, they are send

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd