Difference between revisions of "Advanced-C++/C2/Function-Overloading-And-Overriding/English"
(Created page with ''''Title of script:''' Functions in C and C++ '''Author: '''Ashwini R. Patil '''Keywords: Functions, call by value, call by reference, Video Tutorial, C++ ''' {| style="bord…') |
|||
(One intermediate revision by the same user not shown) | |||
Line 1: | Line 1: | ||
− | '''Title of script | + | '''Title of script''': Function Overloading and Overriding in C++ |
− | '''Author: '''Ashwini | + | '''Author: '''Ashwini Patil |
− | '''Keywords: | + | '''Keywords: Overloading, Overriding, Video tutorial, C++.''' |
{| style="border-spacing:0;" | {| style="border-spacing:0;" | ||
− | + | | style="background-color:#ffffff;border-top:1pt solid #000000;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:0.25pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| <center>'''Visual Cue'''</center> | |
− | + | | style="background-color:#ffffff;border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| <center>'''Narration'''</center> | |
|- | |- | ||
− | | style="border-top: | + | | style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:0.25pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Slide 1 |
− | | style="border-top: | + | | style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Welcome to the spoken tutorial on '''function Overloading''' in '''C++.''' |
|- | |- | ||
− | | style="border-top: | + | | style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:0.25pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Slide 2 |
− | | style="border-top: | + | | style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| In this tutorial, we will learn, |
− | ''' | + | '''Function Overloading.''' |
− | ''' | + | '''Function Overriding.''' |
− | We will do this | + | We will do this with the help of examples. |
|- | |- | ||
− | | style="background-color: | + | | style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:0.25pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Slide 3 |
− | | style="background-color: | + | | style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| To record this tutorial, I am using |
− | '''Ubuntu | + | '''Ubuntu OS '''version 11.10 |
− | ''' | + | '''g++ compiler '''v. 4.6.1 |
|- | |- | ||
− | | style="background-color: | + | | style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:0.25pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Slide 4 |
− | | style="background-color: | + | | style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| '''Let us start with an introduction to function overloading.''' |
− | + | '''Function Overloading''' means two or more '''functions''' can have same name. | |
− | + | The number of '''arguments''' and the '''data-type''' of the '''arguments''' will be different. | |
− | + | When a '''function''' is called it is selected based on the argument list. | |
− | + | |- | |
+ | | style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:0.25pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Open '''funoverload.cpp''' | ||
+ | |||
+ | |||
+ | |||
+ | | style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Let us look at an example. | ||
+ | |||
+ | In this program we will perform addition operation. | ||
+ | |||
+ | I have already typed the code on the editor. | ||
|- | |- | ||
− | | style="background-color: | + | | style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:0.25pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Point the cursor |
− | + | ||
− | I have | + | '''overload.cpp''' |
+ | | style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Note that I have saved the file with the name '''overload.cpp''' | ||
− | + | Let me explain the code. | |
|- | |- | ||
− | | style="background-color: | + | | style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:0.25pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Highlight |
− | ''' | + | '''<nowiki>#include<iostream></nowiki>''' |
− | | style="background-color: | + | | style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| This is our '''header file iostream.''' |
− | + | |- | |
+ | | style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:0.25pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Highlight | ||
− | + | '''using namespace std;''' | |
+ | | style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Here we are using the std namespace. | ||
|- | |- | ||
− | | style="background-color: | + | | style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:0.25pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Highlight |
− | ''' | + | '''int add(int a, int b, int c)''' |
− | | style="background-color: | + | |
+ | Please write the function. | ||
+ | | style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Then we have '''add''' function defined as '''int.''' | ||
+ | |||
+ | In this we have passed three arguments. | ||
+ | |||
+ | '''Int a, int b and int c;''' | ||
+ | |||
+ | '''Then we perform addition of three numbers.''' | ||
+ | |||
+ | '''And we return the value.''' | ||
|- | |- | ||
− | | style="background-color: | + | | style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:0.25pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Highlight |
− | ''' | + | '''float add(float d, float e)''' |
− | '''{ ''' | + | '''{''' |
− | ''' | + | '''return (d + e);''' |
− | ''' | + | '''}Pls indicate the text to be highlighted. |
+ | | style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Here we overload the function '''add.''' | ||
− | + | It is declared as''' float.''' | |
− | + | ||
− | + | We pass two arguments. | |
+ | |||
+ | Then we perform the addition operation on two numbers. | ||
|- | |- | ||
− | | style="background-color: | + | | style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:0.25pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Highlight |
+ | |||
+ | '''int main() ''' | ||
+ | |||
+ | '''{''' | ||
+ | |||
+ | |||
− | + | | style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| This is our '''main''' function. | |
− | | style="background-color: | + | |
|- | |- | ||
− | | style="background-color: | + | | style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:0.25pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Highlight |
+ | |||
+ | '''int add(int, int, int); ''' | ||
+ | |||
+ | '''float add(float, float); ''' | ||
+ | |||
+ | '''int a,b,c; ''' | ||
+ | |||
+ | '''float d,e,sum; ''' | ||
+ | | style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| In''' function''' '''main '''we declare the '''add''' '''function''' with different '''arguments'''. | ||
+ | |||
+ | Then we declare the '''variables'''. | ||
+ | |||
+ | |||
− | |||
− | |||
|- | |- | ||
− | | style="background-color: | + | | style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:0.25pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Highlight |
− | ''' | + | '''<nowiki>cout << "Enter three integers\n"; </nowiki>''' |
− | + | ||
− | + | '''cin >> a >> b >>c; ''' | |
+ | |||
+ | '''sum = add(a, b, c); ''' | ||
+ | |||
+ | '''<nowiki>cout << "Sum of integers: " << sum << "\n";</nowiki>''' | ||
+ | |||
+ | |||
+ | |||
+ | | style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| '''Now we accept the values from the user.''' | ||
+ | |||
+ | Then we call the function '''add '''with three''' '''arguments. | ||
+ | |||
+ | And store the''' '''result in variable''' sum.''' | ||
+ | |||
+ | Here we print the result. | ||
|- | |- | ||
− | | style="background-color: | + | | style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:0.25pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Highlight |
− | ''' | + | '''<nowiki>cout << "Enter two floating point numbers\n"; </nowiki>''' |
− | | style="background-color: | + | |
+ | '''cin >> d >> e; ''' | ||
+ | |||
+ | '''sum = add(d, e); ''' | ||
+ | |||
+ | '''<nowiki>cout << "Sum of floats: " << sum << "\n";</nowiki>''' | ||
+ | | style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Here we call the '''add function '''with two '''arguments'''. | ||
+ | |||
+ | The result is stored in '''sum.''' | ||
+ | |||
+ | And we print the result. | ||
|- | |- | ||
− | | style="background-color: | + | | style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:0.25pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Highlight |
− | + | ||
− | + | '''return 0;''' | |
+ | | style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| And this is the return statement | ||
|- | |- | ||
− | | style="background-color: | + | | style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:0.25pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| |
− | | style="background-color: | + | | style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Let us execute the program |
|- | |- | ||
− | | style="background-color: | + | | style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:0.25pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Open the terminal |
− | ''' | + | '''Ctrl, Alt and T'''keys simultaneously |
+ | | style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Open the terminal by pressing '''Ctrl, Alt and T '''keys simultaneously on ypur keyboard. | ||
+ | |||
+ | |- | ||
+ | | style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:0.25pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Type | ||
+ | |||
+ | '''g++ overload.cpp -o ovr''' | ||
+ | |||
+ | To execute | ||
Type | Type | ||
− | '''./ | + | '''./ovr''' |
− | | style="background-color: | + | | style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| To execute type: |
+ | |||
+ | '''g++ overload.cpp -o ovr''' | ||
+ | |||
+ | '''Press Enter''' | ||
− | + | To execute type | |
− | + | '''./ovr''' | |
− | ''' | + | '''Press Enter''' |
|- | |- | ||
− | | style="background-color: | + | | style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:0.25pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Highlight |
− | Output | + | '''Output''' |
− | | style="background-color: | + | | style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Here it is displayed as: |
− | ''' | + | '''Enter three integers''' |
− | + | I will enter as: | |
− | + | ||
− | + | ||
− | + | '''10''' | |
− | + | '''25''' | |
− | + | ||
− | + | ||
− | + | '''48''' | |
− | + | The output is displayed as: | |
− | + | '''Sum of integers is 83''' | |
+ | |||
+ | Now we see: | ||
+ | |||
+ | '''Enter two floating point numbers''' | ||
+ | |||
+ | I will enter as: | ||
+ | |||
+ | '''4.5''' | ||
+ | |||
+ | '''8.9''' | ||
+ | |||
+ | The output is displayed as: | ||
+ | |||
+ | '''Sum of floating point numbers is 13.4''' | ||
|- | |- | ||
− | | style="background-color: | + | | style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:0.25pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Slide 5 |
− | | style="background-color: | + | | style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Now we will see '''function overriding'''. |
− | + | Let us switch back to our slides. | |
− | + | Redefining a '''base class''' '''function''' in the derived class. | |
+ | |||
+ | The derived '''class function''' overrides the base class function. | ||
+ | |||
+ | But the '''arguments''' passed are same. | ||
+ | |||
+ | And the return-type is also same. | ||
|- | |- | ||
− | | style="background-color: | + | | style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:0.25pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| |
+ | | style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Let us see an example on '''Function Overriding''' | ||
− | ''' | + | '''Here is an example on function Overriding''' |
− | + | ||
− | Let | + | '''Note that our filename is override.cpp.''' |
+ | |||
+ | Let us go through the code. | ||
|- | |- | ||
− | | style="background-color: | + | | style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:0.25pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Highlight |
− | '''<nowiki>#include< | + | '''<nowiki>#include <iostream> </nowiki>''' |
− | | style="background-color: | + | |
+ | '''using namespace std;''' | ||
+ | | style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| This is our header file. | ||
+ | |||
+ | Here we have the''' std namespace.''' | ||
|- | |- | ||
− | | style="background-color: | + | | style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:0.25pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Highlight |
− | ''' | + | '''class arithmetic ''' |
− | + | ||
− | + | '''{ ''' | |
− | + | '''protected: ''' | |
+ | |||
+ | '''int a, b, sum, sub, mul, div;''' | ||
+ | | style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Then we have '''class arithmetic.''' | ||
+ | |||
+ | In this we have declared integer variables''' '''as''' protected.''' | ||
|- | |- | ||
− | | style="background-color: | + | | style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:0.25pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Highlight |
− | ''' | + | '''public: ''' |
− | ''' | + | '''void values (int x, int y) ''' |
− | ''' | + | '''{ ''' |
− | ''' | + | '''a=x, b=y; ''' |
− | | style="background-color: | + | |
+ | '''}''' | ||
+ | | style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Then we have '''function values''' declared as '''public'''. | ||
− | |||
− | |||
− | |||
|- | |- | ||
− | | style="background-color: | + | | style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:0.25pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Highlight |
− | '''int | + | '''virtual int operations () ''' |
− | + | ||
− | + | '''{ ''' | |
− | + | ||
− | ''' | + | '''sum= a + b; ''' |
− | + | ||
− | + | '''<nowiki>cout<< "Addition of two numbers is "<< sum<<"\n"; </nowiki>''' | |
− | + | ||
− | ''' | + | '''} ''' |
− | ''' | + | '''};''' |
− | | style="background-color: | + | | style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Here we have '''virtual function''' as '''operations.''' |
− | + | In this we add the two numbers. | |
− | + | And print the sum. | |
− | + | ||
− | + | Here we close the class. | |
− | + | ||
|- | |- | ||
− | | style="background-color: | + | | style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:0.25pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Highlight |
− | ''' | + | '''class Subtract: public arithmetic ''' |
− | + | ||
− | + | '''{''' | |
− | | style="background-color: | + | | style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Now we have '''class Subtract''' as '''derived class.''' |
− | ''' | + | This inherits the''' base class arithmetic.''' |
− | + | ||
|- | |- | ||
− | | style="background-color: | + | | style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:0.25pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Highlight |
− | ''' | + | '''public: ''' |
− | + | ||
− | + | '''int operations () ''' | |
− | + | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0. | + | '''{ ''' |
+ | |||
+ | '''sub= a - b; ''' | ||
+ | |||
+ | '''<nowiki>cout<< "Difference of two numbers is "<<sub <<"\n"; </nowiki>''' | ||
+ | |||
+ | '''} ''' | ||
+ | |||
+ | '''};''' | ||
+ | | style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| In this we calculate the difference of two numbers. | ||
+ | |||
+ | And we print the difference. | ||
|- | |- | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right | + | | style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:0.25pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Highlight |
− | + | ||
− | + | '''class Multiply: public arithmetic ''' | |
+ | |||
+ | '''{ ''' | ||
+ | |||
+ | '''public: ''' | ||
+ | |||
+ | '''int operations () ''' | ||
+ | |||
+ | '''{ ''' | ||
+ | |||
+ | '''mul = a * b; ''' | ||
+ | |||
+ | '''<nowiki>cout<< "Product of two numbers is "<< mul<<"\n"; </nowiki>''' | ||
+ | |||
+ | '''} ''' | ||
+ | |||
+ | '''};''' | ||
+ | | style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Here we have another '''derived class''' as '''Multiply.''' | ||
+ | |||
+ | This also inherits''' base class arithmetic.''' | ||
+ | |||
+ | In this we calculate the product of two''' '''numbers. | ||
+ | |||
+ | And display the product. | ||
|- | |- | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right: | + | | style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:0.25pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Highlight |
− | ''' | + | '''class Divide: public arithmetic ''' |
− | + | '''{ ''' | |
− | ''' | + | '''public: ''' |
− | + | ||
− | ''' | + | '''int operations () ''' |
− | + | '''{ ''' | |
+ | |||
+ | '''div = a / b; ''' | ||
+ | |||
+ | '''<nowiki>cout<< "Division of two numbers is "<< div<<"\n"; </nowiki>''' | ||
+ | |||
+ | '''} ''' | ||
+ | |||
+ | '''};''' | ||
+ | | style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| And here we have '''class Divide '''this also''' '''inherits the''' base class arithmetic.''' | ||
+ | |||
+ | In this we calculate the division of two numbers. | ||
− | + | And then we display the division. | |
|- | |- | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right: | + | | style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:0.25pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Highlight |
− | ''' | + | '''int main() ''' |
− | + | ||
− | + | '''{ ''' | |
− | + | '''arithmetic *arith, p; ''' | |
− | + | '''Subtract subt; ''' | |
− | + | '''Multiply mult; ''' | |
+ | |||
+ | '''Divide divd; ''' | ||
+ | | style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Now this is our '''main function.''' | ||
+ | |||
+ | In this we create an''' object '''of''' class arithmetic as p.''' | ||
+ | |||
+ | '''arith '''is the pointer to the''' class arithmetic.''' | ||
+ | |||
+ | Then we have''' subt object '''of''' class Subtract.''' | ||
+ | |||
+ | '''mult object '''of''' class Multiply.''' | ||
+ | |||
+ | And''' divd object '''of''' class Divide.''' | ||
|- | |- | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right | + | | style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:0.25pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Highlight |
− | + | ||
− | + | '''arith=&p; ''' | |
+ | |||
+ | '''arith->values(30,12); ''' | ||
+ | |||
+ | '''arith->operations();''' | ||
+ | | style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| '''Here, p''' is set to the address of '''arith.Or arith is set to the address of p? | ||
+ | |||
+ | |||
+ | ''Reply to Rahul Katarey (15/03/2013, 16:33): "..."'' | ||
+ | |||
+ | '''P is set to the address of parallel.''' | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | Here we pass''' arguments '''as''' 30 '''and''' 12 '''in''' function values.''' | ||
+ | |||
+ | Then we call''' the function operations.''' | ||
|- | |- | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right: | + | | style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:0.25pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Highlight |
− | ''' | + | '''arith=&subt; ''' |
− | + | ||
− | + | '''arith->values(42,5); ''' | |
+ | |||
+ | '''arith->operations(); ''' | ||
+ | | style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Now here we set '''subt''' to the address of '''arith.''' | ||
+ | |||
+ | And we pass''' 42 '''and''' 5 '''as''' arguments.''' | ||
+ | |||
+ | Again we call''' function operations.''' | ||
|- | |- | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right: | + | | style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:0.25pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Highlight |
− | ''' | + | '''arith=&mult; ''' |
− | + | ||
− | ''' | + | '''arith->values(6,5); ''' |
+ | |||
+ | '''arith->operations(); ''' | ||
+ | |||
+ | |||
+ | |||
+ | | style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Now we set '''mult''' to the address of '''arith.''' | ||
+ | |||
+ | And pass''' arguments '''as''' 6 '''and''' 5.''' | ||
+ | |||
+ | Then''' '''we call''' function operations.''' | ||
|- | |- | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right: | + | | style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:0.25pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Highlight |
− | ''' | + | '''arith=&divd; ''' |
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0. | + | |
+ | '''arith->values(6,3); ''' | ||
+ | |||
+ | '''arith->operations(); ''' | ||
+ | | style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| And here again we set '''divd''' to the address of '''arith''' And pass '''arguments''' as '''6''' and '''3.''' | ||
+ | |||
+ | Then we''' call function operations.''' | ||
|- | |- | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right: | + | | style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:0.25pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Highlight |
− | ''' | + | '''return 0;''' |
− | + | '''}''' | |
+ | | style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| And this is our '''return statement.''' | ||
− | + | Let us execute the program. | |
− | + | |- | |
+ | | style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:0.25pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| On the terminal | ||
− | |||
− | + | '''g++ override.cpp -o over2''' | |
− | + | '''./over2''' | |
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0. | + | | style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Switch back to our terminal. |
− | + | To execute type: | |
− | + | '''g++ override.cpp -o over2''' | |
− | + | '''Press Enter''' | |
− | + | '''Type:''' | |
− | + | ||
− | ''' | + | '''./over2''' |
− | + | ||
− | + | '''Press Enter''' | |
|- | |- | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right | + | | style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:0.25pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Highlight |
− | + | ||
− | + | The output | |
+ | | style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| The output is displayed as: | ||
+ | |||
+ | '''Addition of two numbers is 42''' | ||
+ | |||
+ | '''Difference of two numbers is 37''' | ||
+ | |||
+ | '''Product of two numbers is 30 and''' | ||
+ | |||
+ | '''Division of two numbers is 2''' | ||
|- | |- | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0. | + | | style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:0.25pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| On the slides |
+ | | style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Let us go back to our slides. | ||
− | ''' | + | |- |
+ | | style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:0.25pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Slide 6 | ||
+ | | style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Let us see the difference of '''overloading''' and '''overriding'''. | ||
+ | '''Overloading''' can occurs without inheritance. | ||
− | + | '''Overriding''' occurs when one class is inherited from another. | |
− | ''' | + | In '''overloading''' the arguments and the return-type must differ. |
− | + | ||
− | ''' | + | In '''overriding''' the arguments and the return type must be same. |
+ | In '''overloading''' the function name is same. | ||
− | + | But it behaves differently depending on the arguments passed to them. | |
− | + | In '''overriding''' the function name, arguments passed and the return type is same. | |
− | + | '''Derived class '''function can perform different operations from the base class. | |
− | + | |- | |
+ | | style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:0.25pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| | ||
+ | | style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| This brings us to the end of this tutorial. | ||
|- | |- | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right: | + | | style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:0.25pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Slide 7 |
− | + | Summary | |
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0. | + | | style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Let us summarize: |
− | + | In this tutorial we have seen | |
− | + | '''Function overloading.''' | |
− | ''' | + | '''eg. int add(int, int, int); ''' |
− | ''' | + | '''We have passed three arguments.''' |
− | + | '''float add(float, float); ''' | |
− | ''' | + | '''Here we have passed two arguments.''' |
− | ''' | + | '''Function Overriding.''' |
− | + | '''eg. virtual int operations ()''' | |
− | + | ||
− | + | ||
− | + | '''int operations ()''' | |
− | + | '''that was overrided.''' | |
− | + | ||
− | + | ||
− | + | '''And then we saw,''' | |
− | + | '''Difference between both.''' | |
|- | |- | ||
− | | style="border-top: | + | | style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:0.25pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Slide 8 |
Assignment | Assignment | ||
− | | style="border-top: | + | | style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| As an assignment |
− | Write a | + | Write a program that will calculate the area of rectangle, square and circle. |
− | Using | + | Using function overloading. |
|- | |- | ||
− | | style="border-top: | + | | style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:0.25pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Slide 9 |
About the Spoken Tutorial Project | About the Spoken Tutorial Project | ||
− | | style="border-top: | + | | style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Watch the video available at the link shown |
− | + | ||
− | + | ||
− | It | + | It summarizes the Spoken Tutorial project |
If you do not have good bandwidth, you can download and watch it | If you do not have good bandwidth, you can download and watch it | ||
|- | |- | ||
− | | style="border-top: | + | | style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:0.25pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Slide 10 |
Spoken Tutorial Workshops | Spoken Tutorial Workshops | ||
− | | style="border-top: | + | | style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| The Spoken Tutorial Project Team |
Conducts workshops using spoken tutorials | Conducts workshops using spoken tutorials | ||
Line 457: | Line 646: | ||
Gives certificates to those who pass an online test | Gives certificates to those who pass an online test | ||
− | For more details, please write to | + | For more details, please write to, |
contact@spoken-tutorial.org | contact@spoken-tutorial.org | ||
|- | |- | ||
− | | style="border-top: | + | | style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:0.25pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Slide Number 11 |
Acknowledgement | Acknowledgement | ||
− | | style="border-top: | + | | style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Spoken Tutorial Project is a part of the Talk to a Teacher project |
It is supported by the National Mission on Education through ICT, MHRD, Government of India | It is supported by the National Mission on Education through ICT, MHRD, Government of India | ||
− | More information on this Mission is available at: http://spoken-tutorial.org\NMEICT-Intro | + | More information on this Mission is available at: [http://spoken-tutorial.org/NMEICT-Intro http://spoken-tutorial.org\NMEICT-Intro] |
|- | |- | ||
− | | style="border-top: | + | | style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:0.25pt solid #c0c0c0;border-left:1pt solid #000000;border-right:0.25pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| |
− | | style="border-top: | + | | style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:0.25pt solid #c0c0c0;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| This is Ashwini Patil from IIT Bombay signing off |
− | Thank You for joining | + | Thank You for joining. |
|} | |} |
Latest revision as of 11:00, 10 May 2013
Title of script: Function Overloading and Overriding in C++
Author: Ashwini Patil
Keywords: Overloading, Overriding, Video tutorial, C++.
|
|
Slide 1 | Welcome to the spoken tutorial on function Overloading in C++. |
Slide 2
|
In this tutorial, we will learn,
Function Overloading. Function Overriding. We will do this with the help of examples. |
Slide 3
|
To record this tutorial, I am using
Ubuntu OS version 11.10 g++ compiler v. 4.6.1 |
Slide 4 | Let us start with an introduction to function overloading.
Function Overloading means two or more functions can have same name. The number of arguments and the data-type of the arguments will be different. When a function is called it is selected based on the argument list. |
Open funoverload.cpp
|
Let us look at an example.
In this program we will perform addition operation. I have already typed the code on the editor. |
Point the cursor
overload.cpp |
Note that I have saved the file with the name overload.cpp
Let me explain the code. |
Highlight
#include<iostream> |
This is our header file iostream. |
Highlight
using namespace std; |
Here we are using the std namespace. |
Highlight
int add(int a, int b, int c) Please write the function. |
Then we have add function defined as int.
In this we have passed three arguments. Int a, int b and int c; Then we perform addition of three numbers. And we return the value. |
Highlight
float add(float d, float e) { return (d + e); }Pls indicate the text to be highlighted. |
Here we overload the function add.
It is declared as float. We pass two arguments. Then we perform the addition operation on two numbers. |
Highlight
int main() {
|
This is our main function. |
Highlight
int add(int, int, int); float add(float, float); int a,b,c; float d,e,sum; |
In function main we declare the add function with different arguments.
Then we declare the variables.
|
Highlight
cout << "Enter three integers\n"; cin >> a >> b >>c; sum = add(a, b, c); cout << "Sum of integers: " << sum << "\n";
|
Now we accept the values from the user.
Then we call the function add with three arguments. And store the result in variable sum. Here we print the result. |
Highlight
cout << "Enter two floating point numbers\n"; cin >> d >> e; sum = add(d, e); cout << "Sum of floats: " << sum << "\n"; |
Here we call the add function with two arguments.
The result is stored in sum. And we print the result. |
Highlight
return 0; |
And this is the return statement |
Let us execute the program | |
Open the terminal
Ctrl, Alt and Tkeys simultaneously |
Open the terminal by pressing Ctrl, Alt and T keys simultaneously on ypur keyboard. |
Type
g++ overload.cpp -o ovr To execute Type ./ovr |
To execute type:
g++ overload.cpp -o ovr Press Enter To execute type ./ovr Press Enter |
Highlight
Output |
Here it is displayed as:
Enter three integers I will enter as: 10 25 48 The output is displayed as: Sum of integers is 83 Now we see: Enter two floating point numbers I will enter as: 4.5 8.9 The output is displayed as: Sum of floating point numbers is 13.4 |
Slide 5 | Now we will see function overriding.
Let us switch back to our slides. Redefining a base class function in the derived class. The derived class function overrides the base class function. But the arguments passed are same. And the return-type is also same. |
Let us see an example on Function Overriding
Here is an example on function Overriding Note that our filename is override.cpp. Let us go through the code. | |
Highlight
#include <iostream> using namespace std; |
This is our header file.
Here we have the std namespace. |
Highlight
class arithmetic { protected: int a, b, sum, sub, mul, div; |
Then we have class arithmetic.
In this we have declared integer variables as protected. |
Highlight
public: void values (int x, int y) { a=x, b=y; } |
Then we have function values declared as public.
|
Highlight
virtual int operations () { sum= a + b; cout<< "Addition of two numbers is "<< sum<<"\n"; } }; |
Here we have virtual function as operations.
In this we add the two numbers. And print the sum. Here we close the class. |
Highlight
class Subtract: public arithmetic { |
Now we have class Subtract as derived class.
This inherits the base class arithmetic. |
Highlight
public: int operations () { sub= a - b; cout<< "Difference of two numbers is "<<sub <<"\n"; } }; |
In this we calculate the difference of two numbers.
And we print the difference. |
Highlight
class Multiply: public arithmetic { public: int operations () { mul = a * b; cout<< "Product of two numbers is "<< mul<<"\n"; } }; |
Here we have another derived class as Multiply.
This also inherits base class arithmetic. In this we calculate the product of two numbers. And display the product. |
Highlight
class Divide: public arithmetic { public: int operations () { div = a / b; cout<< "Division of two numbers is "<< div<<"\n"; } }; |
And here we have class Divide this also inherits the base class arithmetic.
In this we calculate the division of two numbers. And then we display the division. |
Highlight
int main() { arithmetic *arith, p; Subtract subt; Multiply mult; Divide divd; |
Now this is our main function.
In this we create an object of class arithmetic as p. arith is the pointer to the class arithmetic. Then we have subt object of class Subtract. mult object of class Multiply. And divd object of class Divide. |
Highlight
arith=&p; arith->values(30,12); arith->operations(); |
Here, p is set to the address of arith.Or arith is set to the address of p?
P is set to the address of parallel.
Then we call the function operations. |
Highlight
arith=&subt; arith->values(42,5); arith->operations(); |
Now here we set subt to the address of arith.
And we pass 42 and 5 as arguments. Again we call function operations. |
Highlight
arith=&mult; arith->values(6,5); arith->operations();
|
Now we set mult to the address of arith.
And pass arguments as 6 and 5. Then we call function operations. |
Highlight
arith=&divd; arith->values(6,3); arith->operations(); |
And here again we set divd to the address of arith And pass arguments as 6 and 3.
Then we call function operations. |
Highlight
return 0; } |
And this is our return statement.
Let us execute the program. |
On the terminal
./over2 |
Switch back to our terminal.
To execute type: g++ override.cpp -o over2 Press Enter Type: ./over2 Press Enter |
Highlight
The output |
The output is displayed as:
Addition of two numbers is 42 Difference of two numbers is 37 Product of two numbers is 30 and Division of two numbers is 2 |
On the slides | Let us go back to our slides. |
Slide 6 | Let us see the difference of overloading and overriding.
Overloading can occurs without inheritance. Overriding occurs when one class is inherited from another. In overloading the arguments and the return-type must differ. In overriding the arguments and the return type must be same. In overloading the function name is same. But it behaves differently depending on the arguments passed to them. In overriding the function name, arguments passed and the return type is same. Derived class function can perform different operations from the base class. |
This brings us to the end of this tutorial. | |
Slide 7
Summary |
Let us summarize:
In this tutorial we have seen Function overloading. eg. int add(int, int, int); We have passed three arguments. float add(float, float); Here we have passed two arguments. Function Overriding. eg. virtual int operations () int operations () that was overrided. And then we saw, Difference between both. |
Slide 8
Assignment |
As an assignment
Write a program that will calculate the area of rectangle, square and circle. Using function overloading. |
Slide 9
About the Spoken Tutorial Project |
Watch the video available at the link shown
It summarizes the Spoken Tutorial project If you do not have good bandwidth, you can download and watch it |
Slide 10
Spoken Tutorial Workshops |
The Spoken Tutorial Project Team
Conducts workshops using spoken tutorials Gives certificates to those who pass an online test For more details, please write to, contact@spoken-tutorial.org |
Slide Number 11
|
Spoken Tutorial Project is a part of the Talk to a Teacher project
It is supported by the National Mission on Education through ICT, MHRD, Government of India More information on this Mission is available at: http://spoken-tutorial.org\NMEICT-Intro |
This is Ashwini Patil from IIT Bombay signing off
Thank You for joining. |