Loading....
Coupon Accepted Successfully!

 

Overloading Member Function

The derived class can have a similar function name as the base class member function. An object of the derived class invokes the member function of the derived class even if a similar function is preset in the base class.

11.31 Write a program to overload member function in base and derived class.
 
OUTPUT
In base class function
In derived class function
In base class function
 
Explanation: In this program, the class D is derived from the class B. Both the classes have a similar function show() as a member function. In function main(), the objects of both classes B and D are declared. The object b invokes the member function show(). The object b is the object of the base class; hence, it invokes the member function of show() of the base class. In addition, the object of the base class cannot invoke the member function of the derived class, because the base class does not have information on the classes derived under it.

The object
d of the derived class invokes the function show(). When functions with a similar name and argument list are present in both the base and derived classes, the object of the derived class gives first priority to the function of its own class. Thus, the statement d.show() invokes the function of the derived class. To invoke the function of the base class with the object of the derived class, the class name and scope access operator precede the matching function name. From the above program, it is also clear that we can declare the objects of both base and derived classes. The objects of both base and derived classes are independent of one another.





Test Your Skills Now!
Take a Quiz now
Reviewer Name