Over-riding functions in C++
In this tutorial you will learn:
1. What is over-riding of a function?
2. Why to over-riding a function?
3. How to over-ride a function in a program?
4. Basic C++ syntax
What is meant by over-riding a function?
By over-riding a function it means that we add some extra functionality in the function which is present in the derived class and that function has the name same as that of the base class function. By overriding a function we basically redefine a function in the derived class that is already present in the base class.
Why to over-ride a function?
We over-ride function when we want to add some error checking mechanism in a function or to take some more functionality from the derived class object. So that whenever a derived class object is made, as it may also contain some additional data member and functions so we can redefine the already present functions in derived class to handle the new additions in the derived class.
How to use over-ride functions?
We can over-ride a function just be calling the previously made function of the base class through binary scope resolution operator(::) which has the same name as that of the newly made function and adding some extra definition(code) to that function also. It must be noted that the function will be called as over-rided function only if the name of the function is same as of the function that was in base class. The program below will make things more clearer.
strong>Over riding Functions
Open Dev C++ then File > new > source file and start writing the code below.
using namespace std;
These are the basic header files which are required for C++ programming. Using namespace std is used at the top of the program because if its not written then we have to write ‘std::’ with every line of the program. #include includes the C++ built in libraries and iostream is used for input and output of the program. Conio.h is used to show output on the screen, if it is not present , the screen flashes and disappears hence not showing output.
void push(int var)
In this program we have demonstrated the use of over riding functions using stack. So first we have made a base class named stack. Which have an array and a variable top as its data members and general functions of stack like push and pop are present in this class. The default constructor initializes the value of top by -1 because if we want to push in an integer then first there will be an increment in top means it value changes to zero and then the new integer will be pushed.
class stack2 : public stack
void push(int var)
cout<<"Error: stack full";
if (top < 0)
cout<<"Error: stack empty\n"; exit(1);
return stack::pop ();
The class stack2 has been derived from the class stack which has two over riding function push and pop. In the push function we have placed a check that no more than ten elements(numbers) could be entered into the stack because we have made the stack of size equal to ten. Additional values will not work fine here. In the pop function we have placed a check that if it is found that there is no number in the stack it will display an error message that is “Stack Empty”.
It must be noted here that the both over rided functions have called the same named functions of push and pop of the base class using binary scope resolution so it can be said so that in derived class an additional functionality is added to the functions of the base class.
cout<<"Please enter 10 numbers you want to push into stack\n"; //11th number gives error
cout<<"Items are being Popped one after another, so popped items are:"<<endl;
In the main of the program first we have made an object of class stack 2 named ‘s’ and then we have just entered the ten numbers in the stack and then popped them using two for loops and displayed them on the screen. It must be noted here that when we tried to pop the eleventh number and error appeared that the stack is empty. This error notification comes up due to the over riding function in the derived class.
Execute > compile
then Execute > run