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;
- class stack
- int a;
- int top;
- void push(int var)
- int pop()
- return a[top--];
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.
- void main()
- stack2 s;
- int a;
- cout<<"Please enter 10 numbers you want to push into stack\n"; //11th number gives error
- cout<<"Numbers are:";
- for(int y=0;y<10;y++)
- cout<<"Items are being Popped one after another, so popped items are:"<<endl;
- for(int x=0;x<11;x++)
Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.
Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.
FOR YOUR OWN SAFETY, PLEASE:
1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.