Function Overriding in C++ with the methods of accessing the overridden methods of the base class

Inheritance in C++

 

The process of creating a new class from a given class is called inheritance in C++.

 

C++ supports the concept of inheritance .

 

the classes in C++ can be used again an again as per requirement.

 

This inheritance is achieved by creating a new class from the existing one.

 

The class that is derived from the given class is called derived class.

 

If a base class is animal, then a derived class is cow.

If a base class is plants, then a derived class is leaves,stem etc.

 

Syntax for derived class declaration-:

Class derived_class_name:visibility mode Base_class_name

{

//body of derived class

}

Here, colon after derived class name specifies that the new class is formed from the existing one.

 

And the visibility mode( or access specifiers) can be either public, private or protected.

 

Did you get it?

 

What the visibility modes can be?

Private, public and protected.

Remember this.

 

Only the protected and public information of a base class are inherited in a derived class.

 

 

However private information of a base class do not get inherited at all.

 

Protected access specifiers and its role in inheritance-:

 

Protected and private information of a class is only accessible by the member function of a class, and a friend function declared within a class.

 

 

 

This means that the effect of private and protected access specifiers is same, when there is no inheritance process

BUT,

However, their effects are seen in the case of inheritance process.

 

i.e to say that protected information of a base class are available directly in a derived class, however private information of a base class are not available in a derived class.

 

 

 

Data members and member function overriding-:

 

If base class and derived class have same name of data members and same name of member functions then the process is called data member and member function overriding.

 

Confused!

Overriding=same name of variable and function in program

 

(This means that if base class and derived class have same name of data members and member functions, then derived class members=data + functions hides or displaces or overrides the members of a base class.)

 

 

So, our next program is going to be about overriding.

 

How are we going to write the program?

 

We need base class and derived class.

Then, we need to override.

 

i.e we need to have the same function names in the derived class.

 

As well as the same variables names in the second class.

 

And in the main function, we are going make a object of derived class.

 

And call the function using the object (of derived class).

 

Source code is as follows-:

Output-:

 

enter the value to n of a derived class11
enter the value to n of a derived class33
the value to both the n are66

 

i.e the base class value of n was hidden…and in programming it is said to be overridden.

 

 

I want you to actively participate with me while learning.

So, at least, copy the code into code blocks and see the output.

 

If you want to learn faster, write the whole blog in your copy…yes the whole blog, they are the notes……..don’t your write almost all notes in your class when your professor teaches you??

 

Believe  that you are definitely going to master C++ language…..I will even make projects tutorials, so you don’t need keep thinking about making projects in C++/C.

 

 

How to solve this problem of overriding?

Overridden members of the base class can be accessed by 2 ways.

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

2) By the help of a derived class object.

  Now we will discuss each of them step by step……….

 

 

if you are bored, go and get a coffee.

 

 

 

don’t forget to write what i am giving here in your copy and compiler(if you have time)

….

 

 

all the programs works…..

 

so no need to worry about running the source code…..

i am leaving too much spaces so it becomes clear to understand a concept…

 

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

How to write this program?

Say the base class is named as base.

Then, assume that the member function of the base class is “input()” and “display”.

 

 

 

Then override the member functions (by keeping same name in base).

 

As our purpose is to access the overridden member and we do it by

 

The member function of a base class.

i.e Base::input();;

and Base::display();

remember this.

 

 

I repeat that we need to use the member function of the base class to access the hidden functions/members.

 

And for that we do

Base::input()

This will get us all the things that are inside the input function of the base class.

 

Likewise base::display() will help us to access all the members of the display function of the base class.

 

 

Output-:

 

enter the value of n of a base class

5

enter the value of n  of  a derived class

5

the total value=10

so, how did the program worked?

First, it went to  the derived class input function by the help of D.input().

 

Then, inside that the input of the base class was also got by the help of Base::input().

 

 

And, we thus got 2 inputs for each base and derived class.

 

 

Now the display function of derived class was called through D.display() and that added both the numbers…..

Base::n means that the n whose scope is inside base, is being accessed by the derived class.

 ———————————————————————————————————————————————————————————————

Now,

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

 

How to write this program without looking at source code?

 

you are going to need a derived class object…

so there will be 2 classes (base and derived)

 

 

and the derived class objects will access the base class functions by this syntax;

D.Base::input();

 

so no need of Base::input() inside the derived class input function …..

and everything else remains same…..

Try this source code yourself first

Output:

 

enter the value of n of a base class
36
enter the value of n of a derived class
78
the total value=114

 

 

When I listened to many learners, I found that…..shorter the blog post…..longer the retention capacity of learners….

 

 

so i have decided to make the blog post shorter instead of writing really longer blog posts as I used to write earlier…

Furthur reading-:

Advantages/Disadvantages of inheritance -:

https://www.quora.com/What-are-the-advantages-and-disadvantages-of-inheritance-in-C++

 

Differences between function overloading and overriding-:

https://www.youtube.com/watch?v=5ou6CP3rloI

 

Comment if you still like a long blog post, I will change my way….

 

 

 

 

Bye

Be the first to comment

Leave a Reply

Your email address will not be published.


*