What is ambiguity and function overriding? How they can be resolved? Explain each of them with suitable example.

Ambiguity means when the base and derived class have the same name of member function OR data members.

These are the two ways to access the overridden members/solve the overridden problem in inheritance.

1) Accessing the overridden members by the help of a member function of a base class

2) Accessing the overridden members by the help of a derived class object

 

In both the cases, the classname::name of member should be specified.

1)By the help of a member function of a base class

Clue for writing program is that we need to access the overridden members.

That means the base and derived class will have same function name and data members name.

We will be using the help of member function of a base class.

We will do it by Base::input(). Now below is the source code.

 

Output-:

enter the value of n of a base class 5
enter the value to n of a derived class 2
the total value is 7

In the next blog post, we will discuss about the second part (accessing overridden members of a base class by the help of derived class objects.)

Here is the link to the next blog post-:

http://www.scitechtop.com/what-is-ambiguity-and-function-overriding-how-they-can-be-resolved-explain-each-of-them-with-suitable-example/

 

Leave a Comment

Your email address will not be published. Required fields are marked *

Do NOT follow this link or you will be banned from the site! Scroll to Top