Over-riding Functions in C++

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. Program: strong>Over riding Functions Basic Step: Open Dev C++ then File > new > source file and start writing the code below.
  1. #include<iostream>
  2. #include<conio.h>
  3. 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.
  1. class stack
  2. {
  3. protected:
  4. int a[10];
  5. int top;
  6. public:
  7. stack()
  8. {
  9. top=-1;
  10. }
  11. void push(int var)
  12. {
  13. a[++top]=var;
  14. }
  15. int pop()
  16. {
  17. return a[top--];
  18. }
  19. };
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.
  1. class stack2 : public stack
  2. {
  3. public:
  4. void push(int var)
  5. {
  6. if (top>=9)
  7. {
  8. cout<<"Error: stack full";
  9. exit(1);
  10. }
  11. stack::push(var);
  12. }
  13. int pop()
  14. {
  15. if (top < 0)
  16. {
  17. cout<<"Error: stack empty\n"; exit(1);
  18. }
  19. return stack::pop ();
  20. }
  21. };
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.
  1. void main()
  2. {
  3. stack2 s;
  4. int a;
  5. cout<<"Please enter 10 numbers you want to push into stack\n"; //11th number gives error
  6. cout<<"Numbers are:";
  7. for(int y=0;y<10;y++)
  8. {
  9. cin>>a;
  10. s.push(a);
  11. }
  12. cout<<"Items are being Popped one after another, so popped items are:"<<endl;
  13. for(int x=0;x<11;x++)
  14. {
  15. cout<<s.pop()<<endl;
  16. }
  17. }
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 Output output

Comments

Submitted bySmithd94 (not verified)on Tue, 08/05/2014 - 15:49

Good writeup, I am normal visitor of one's site, maintain up the excellent operate, and It's going to be a regular visitor for a long time. dgdfedddeakbeakc

Add new comment