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

Luminous Jewels - The Polishing Game, Byteland county is very famous for lu...

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

What are all of the implicit member functions of the class? , What are all ...

What are all of the implicit member functions of the class? Or what are all of the functions that compiler implements for us if we don't describe one? A: 1.      copy ctor

Identifier and constant, What are  Id e n t i f ie rs a n d C o...

What are  Id e n t i f ie rs a n d C o n s t a n ts in C++? Id e n t i f ie r a n d C o n s t a n t : I d e n t i f ie r

Luminous jewel - a polishing game, Byteland county is very famous for lumin...

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

Develop libcurl xml project, Develop libcurl XML project I have a Projec...

Develop libcurl XML project I have a Project using XML with C++. You will use things like LIbcurl etc....and a XML parser to finish the project. Developer must know how to use X

Describe about the container class, Describe about the container class. ...

Describe about the container class. -Class to hold objects in external storage or memory. It acts as a generic holder. - It has a predefined behaviour and a known interface.

For loop, write a c++ program for the sum of 10 numbers and print out the a...

write a c++ program for the sum of 10 numbers and print out the average

I need file transfer solution, We move a high quantity of large Video files...

We move a high quantity of large Video files (200mb - 2 GB each) across the internet. We have a Linux server (ubuntu) hosted in a datacenter (1Gbps) operating an FTP server. Our

C program to display a rectangle, Aim: To implement a program to display a...

Aim: To implement a program to display a rectangle, circle and triangle. Code:                       class shape {             public:                         vir

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