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

What is your reaction to following line of code? , Q-What is your reaction ...

Q-What is your reaction to following line of code?               delete this; A: This is not a good programming Practice. A good programmer will insist that you must absolute

Jewel polishing, Byteland county is very famous for luminous jewels. Lumino...

Byteland county is very famous for luminous jewels. Luminous jewels are used in making beautiful necklaces. A necklace consists of various luminous jewels of particular colour. Nec

3, Write a program to find the area under the curve y = f(x) between x = a ...

Write a program to find the area under the curve y = f(x) between x = a and x = b, integrate y = f(x) between the limits of a and b. The area under a curve between two points can b

Assesors, what are assessor. elaborate with clear easy diagrams

what are assessor. elaborate with clear easy diagrams

C expressions - assume variables are double or float, Assume variables x, f...

Assume variables x, f, and d are of type int, float, and double, respectively.  Their values are arbitrary, except that neither f nor d equals +∞, -∞, or NaN .  For each of the fo

Plot an objects, write a C code that plots an object on the window and on t...

write a C code that plots an object on the window and on the user''s click of mouse on the window, the object starts rotating continuously until the user presses the mouse again

Decode the code, c++ coding decode the letters from numbers

c++ coding decode the letters from numbers

Evaluate the credit worthiness of a client, Write a C++ program that can be...

Write a C++ program that can be used to evaluate the credit worthiness of a client. The program reads the credit limit and the price and quantity of the item to be purchased by the

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