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

Assignment help, It''s that spooky time of year when the dead walk the Eart...

It''s that spooky time of year when the dead walk the Earth, things go bump in the night, and the Centers for Disease Control (CDC) recognizes the need for zombie preparedness (

Parking garage, A parking garage charges a $2.00 minimum fee to park for up...

A parking garage charges a $2.00 minimum fee to park for up to three hours and an additional $0.50 per hour for each hour or part thereof over three hours. The maximum charge for a

Distinguish between a class and an object, Problem: (a) Distinguish bet...

Problem: (a) Distinguish between a class and an object. (b) Define instantiation. (c) What is the importance of the "this" variable in java. (d) What is encapsulation?

Array, write a function that take as parameters an array of integer and its...

write a function that take as parameters an array of integer and its size and return the sum of its value

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

C program, what will be the c program for ideal gas equation.

what will be the c program for ideal gas equation.

Radix sort - c program, Radix sort - C program: Write a program in c t...

Radix sort - C program: Write a program in c to define a radix sort. void main() {  int array[100],n;  int i;  void radix(int *,int);  printf ("How many nos\

Write a program that calculates points along a curve, write a program that ...

write a program that calculates points along a rhodonea curve.... ? int fillArray( double data[ ], int nValues, double min, double max ); o Used to fill in the theta array. Return

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