Difference between revisions of "Advanced-C++/C2/Function-Overloading-And-Overriding/English"

From Script | Spoken-Tutorial
Jump to: navigation, search
(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…')
 
Line 1: Line 1:
'''Title of script:''' Functions in C and C++
+
'''Title of script''': Function Overloading and Overriding in C++
  
'''Author: '''Ashwini R. Patil
+
'''Author: '''Ashwini Patil
  
'''Keywords: Functions, call by value, call by reference, Video Tutorial, C++ '''
+
'''Keywords: Overloading, Overriding, Video tutorial.'''
  
  
  
 
{| style="border-spacing:0;"
 
{| style="border-spacing:0;"
! <center>Visual Cue</center>
+
| 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>
! <center>Narration</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:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Slide 1
+
| 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:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Welcome to the spoken tutorial on '''Function calls in C and C++'''
+
| 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:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Slide 2
+
| 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:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| In this tutorial we will learn about type of '''function calls''', namely
+
| 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,
  
'''call by value'''.
+
'''Function Overloading.'''
  
'''call by reference'''.
+
'''Function Overriding.'''
  
We will do this through an example.
+
We will do this with the help of examples.
  
 
|-
 
|-
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Slide 3  
+
| 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:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| To record this tutorial, I am using
+
| 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 Operating system''' version 11.10
+
'''Ubuntu OS '''version 11.10
  
'''gcc''' and g'''++ Compiler''' version 4.6.1 on Ubuntu
+
'''g++ compiler '''v. 4.6.1
  
 
|-
 
|-
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Slide 4
+
| 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:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us start with the introduction to '''functions call by value'''
+
| 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;"| Function Overloading means two or more functions can have same name.
  
It is a method of passing '''arguments''' to the function.
+
The number of arguments and the data-type of the arguments will be different.
  
When we pass a variable by value it makes a copy of the variable .
+
When a function is called it is selected based on the argument list.
  
Changes made to the arguments inside the function will remain in the function.
+
|-
 +
| 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'''
  
It will not be affected outside 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;"| 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:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"|
+
| 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
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us see a program on '''function call by value'''.
+
  
I have already typed the program on the editor.
+
'''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'''
  
So i will just open it.
+
Let me explain the code now.
  
 
|-
 
|-
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Point the cursor to
+
| 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
  
'''callbyval.c'''
+
'''<nowiki>#include<iostream></nowiki>'''
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Please note that our filename is '''callbyval.c'''
+
| 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.'''
  
In this program we will calculate the cube of a number.
+
|-
 +
| 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
  
Let me explain the code now.
+
'''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;"| This is the using statement.
  
 
|-
 
|-
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Highlight
+
| 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 <stdio.h></nowiki>'''
+
'''int add(int a, int b, int c)'''
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| This is our '''header file'''
+
 
 +
 
 +
 
 +
| 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 have '''add function''' defined as '''int'''.
 +
 
 +
In this we have passed three arguments.
 +
 
 +
 
 +
Then we perform the addition of three numbers.
  
 
|-
 
|-
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Highlight
+
| 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 cube(int x) '''
 
  
'''{ '''
 
  
'''x=x*x*x; '''
+
| 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.'''
  
'''return(x); '''
+
It is declared as''' float.'''
  
'''}'''
+
We pass two arguments.
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Here we have function''' cube '''having''' '''an''' argument '''as''' x.'''
+
  
In this function, we calculate the cube of '''x''' and return the value of '''x'''.''' '''
+
Then we perform the addition of two numbers.
  
 
|-
 
|-
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Highlight
+
| 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()'''
+
'''int main() '''
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| This is our '''main 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;"| This is our '''main''' function.
  
 
|-
 
|-
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Highlight
+
| 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 main function we declare the add function with different arguments.
 +
 
 +
Then we declare the variables.
 +
 
 +
 
  
'''int n=8;'''
 
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Here we give the value of '''n''' as '''8.'''
 
  
 
|-
 
|-
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Highlight
+
| 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  
  
'''printf("Cube of %d is %d\n",n,cube(n));'''
+
'''<nowiki>cout << "Enter three integers\n"; </nowiki>'''
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Then we call the function''' cube.'''
+
  
And print the value of''' n '''and the cube of''' n.'''
+
'''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.'''
 +
 
 +
And we print the result.
  
 
|-
 
|-
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Highlight
+
| 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>'''
 +
 
 +
'''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:#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;'''
 
'''return 0;'''
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| This is our return statement.
+
| 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 the return statement
  
 
|-
 
|-
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"|  
+
| 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;"| Click on '''Save'''
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| click on Save.
+
| 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;"| Now Click on '''Save'''
  
Now let us execute the program.
+
|-
 +
| 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:#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
  
 
|-
 
|-
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Press 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:0.25pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Open the terminal
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Open the terminal by pressing Ctrl, Alt and T keys simultaneously
+
 
 +
'''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
  
 
|-
 
|-
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Type
+
| 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  
  
'''gcc callbyval.c -o val'''
+
'''g++ overload.cpp -o ovr'''
 +
 
 +
To execute
  
 
Type
 
Type
  
'''./val'''
+
'''./ovr'''
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| To compile the program, type
+
| 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 compile the program type  
  
'''gcc callbyval.c -o val'''
+
'''g++ overload.cpp -o ovr'''
  
To execute, type
+
To execute type
  
'''./val'''
+
'''./ovr'''
  
 
|-
 
|-
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Highlight
+
| 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:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| We see the output is displayed as
+
| 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:
  
'''Cube of 8 is 512.'''
+
'''Enter three integers'''
  
|-
+
I will enter as:
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"|
+
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now we will see '''function call''' by reference.
+
  
Let us go back to our slides.
+
'''10'''
 +
 
 +
'''25'''
 +
 
 +
'''48'''
 +
 
 +
The output is displayed as:
 +
 
 +
'''Sum of integers is 83'''
 +
 
 +
Then we see:
 +
 
 +
'''Enter two floating point numbers'''
 +
 
 +
I will enter as:
 +
 
 +
'''4.5'''
 +
 
 +
'''8.9'''
 +
 
 +
'''Sum of floating point numbers is 13.4'''
  
 
|-
 
|-
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Slide 5
+
| 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:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| It is another method of passing '''arguments''' to the function.
+
| 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.
  
This method copies the address of the argument instead of the value.
+
Redefining a base class function in the derived class.
  
Changes made to the arguments inside a function can affect them outside.
+
The derived class function overrides the base class function.
  
In this we need to declare the arguments as pointer type.
+
But the arguments passed are same.
 +
 
 +
And the return-type is also same.
  
 
|-
 
|-
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| On the editor
+
| 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:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us see an example on function call by reference.
+
| 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
  
 
I have already typed the code on the editor.
 
I have already typed the code on the editor.
Line 176: Line 261:
  
 
|-
 
|-
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Point to the filename
+
| 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
  
'''callbyref.c'''
+
'''<nowiki>#include <iostream> </nowiki>'''
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Note that our filename is '''callbyref.c'''
+
  
Let me explain the code now.
+
'''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 as '''iostream.'''
 +
 
 +
This is our''' std namespace.'''
  
 
|-
 
|-
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Highlight
+
| 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<stdio.h></nowiki>'''
+
'''class arithmetic '''
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| This is our headerfile as '''stdio.h'''
+
  
|-
+
'''{ '''
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Highlight
+
  
'''int swap(int *a, int *b)'''
+
'''protected: '''
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Then we have function '''swap'''
+
  
This function will exchange the values of the variables.
+
'''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;"| Here we have '''class arithmetic.'''
  
You can see that the''' arguments '''passed in the function are''' pointer type.'''
+
In this we have declared integer variables''' '''as''' protected.'''
  
 
|-
 
|-
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Highlight
+
| 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 t; '''
+
'''public: '''
  
'''t=*a; '''
+
'''void values (int x, int y) '''
  
'''<nowiki>*a=*b; </nowiki>'''
+
'''{ '''
  
'''<nowiki>*b=t;</nowiki>'''
+
'''a=x, b=y; '''
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Here we have declared an '''integer''' variable '''t.'''
+
 
 +
'''}'''
 +
| 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'''.
  
First value of '''<nowiki>*a</nowiki>''' is stored in '''t.'''
 
  
Then value of '''<nowiki>*b</nowiki>''' is stored in '''<nowiki>*a.</nowiki>'''
 
  
Then value of '''t''' is stored in '''<nowiki>*b</nowiki>'''.
 
  
 
|-
 
|-
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Highlight
+
| 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()'''
+
'''virtual int operations () '''
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| This is our '''main''' function.
+
  
|-
+
'''{ '''
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Highlight
+
  
'''int i, j;'''
+
'''sum= a + b; '''
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Here we have declared two integer variables as i and j.
+
  
|-
+
'''<nowiki>cout<< "Addition of two numbers is "<< sum<<"\n"; </nowiki>'''
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Highlight
+
  
'''printf("Enter the values "); '''
+
'''} '''
  
'''scanf("%d%d",&i,&j);'''
+
'''};'''
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Then we take the values of i and j as user inputs.
+
| 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 '''virtual function''' as '''operations.'''
  
'''&i''' and '''&j''' will give the memory address of '''i''' and '''j.'''
+
In this we calculate the addition of two numbers.
  
|-
+
And print the sum.
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Highlight
+
  
'''printf("Before swapping %d and %d\n",i,j);'''
+
Here we close the class.
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| First we print the values before swapping.
+
  
 
|-
 
|-
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Highlight
+
| 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
  
'''swap(&i,&j);'''
+
'''class Subtract: public arithmetic '''
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Here we call the function '''swap'''
+
  
|-
+
'''{'''
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Highlight
+
| 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 Subtract''' as '''derived class.'''
  
'''printf("After swapping %d and %d\n",i,j);'''
+
This inherits the''' base class arithmetic.'''
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now we print the values after swapping.
+
  
 
|-
 
|-
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Highlight
+
| 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;'''
+
'''public: '''
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| This is our return statement.
+
  
|-
+
'''int operations () '''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Click on '''Save'''
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Click on''' Save'''
+
  
|-
+
'''{ '''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| On the terminal
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us execute the program
+
  
Come back to our terminal
+
'''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.
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Type
+
| 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
  
'''gcc callbyref.c -o ref'''
+
'''class Multiply: public arithmetic '''
  
Type
+
'''{ '''
  
'''./ref'''
+
'''public: '''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now compile the program as before,
+
  
'''gcc callbyref.c -o ref'''
+
'''int operations () '''
  
let us execute
+
'''{ '''
  
'''./ref'''
+
'''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;"| Then we have another '''derived class''' as '''Multiply.'''
 +
 
 +
This also inherits''' class arithmetic.'''
 +
 
 +
In this we calculate the product of two''' '''numbers.
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Highlight
+
| 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
  
'''Output'''
+
'''class Divide: public arithmetic '''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| We see it is output is displayed as:
+
  
Enter the values
+
'''{ '''
  
I will enter as '''6''' and '''4'''
+
'''public: '''
  
Before swapping''' 6 '''and''' 4'''
+
'''int operations () '''
  
After swapping''' 4 '''and''' 6'''
+
'''{ '''
  
|-
+
'''div = a / b; '''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"|
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| NOW LET US SEE HOW TO EXECUTE THE SAME PROGRAM IN C++
+
  
I have the code, lets go through it.
+
'''<nowiki>cout<< "Division of two numbers is "<< div<<"\n"; </nowiki>'''
  
|-
+
'''} '''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Point cursor
+
  
'''callbyref.cpp'''
+
'''};'''  
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Note that our filename is '''callbyref.cpp'''
+
| 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.'''
  
Let me explain the code now.
+
In this we calculate the division of two numbers.
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Type
+
| 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><iostream></nowiki>'''
+
'''int main() '''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| First we will change the header file
+
  
'''<nowiki><iostream></nowiki>'''
+
'''{ '''
 +
 
 +
'''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;"| 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:none;padding:0.097cm;"| Type
+
| 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
  
'''using namespace std;'''
+
'''arith=&p; '''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| We have used the '''std namespace'''
+
 
 +
'''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;"| '''p''' is set to the address of '''arith.'''
 +
 
 +
Here we pass''' arguments '''as''' 30 '''and''' 12 '''in''' function values.'''
 +
 
 +
Then we call''' function operations.'''
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Highlight
+
| 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 swap(int &x, int &y) '''
+
'''arith=&subt; '''
  
{
+
'''arith->values(42,5); '''
  
int temp;  
+
'''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 we set '''subt''' to the address of '''arith.'''
  
temp = x;
+
Then we pass''' 42 '''and''' 5 '''as''' arguments.'''
  
x = y;
+
Again we call''' function operations.'''
  
y = temp;  
+
|-
 +
| 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; '''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| The function declaration is same in C++.
+
  
In this we pass the arguments as '''&x''' and '''&y'''.
+
'''arith->values(6,5); '''
  
This will give the memory address of '''x''' and '''y'''.
+
'''arith->operations(); '''
  
Here we swap the values.
+
 
 +
 
 +
| 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.'''
 +
 
 +
Now''' '''we call''' function operations.'''
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Highlight
+
| 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 code'''
+
'''arith=&divd; '''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Rest of the code is similar to our C code.
+
  
The '''printf''' and '''scanf''' statement is replaced by '''cout''' and '''cin '''statement.
+
'''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;"| Then we set '''divd''' to the address of '''arith''' and pass '''arguments''' as '''6''' and '''3.'''
 +
 
 +
Now we''' call function operations.'''
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| On the terminal
+
| 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
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us compile the program
+
  
Come back to the terminal
+
'''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;"| This is our '''return statement.'''
 +
 
 +
Now let us execute the program.
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Type
+
| 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++callbyref.cpp -o ref1'''
 
  
 +
'''g++ override.cpp -o over2'''
  
Type
+
'''./over2'''
 +
| 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;"| Come back to our terminal.
  
'''./ref1'''
+
To execute type:
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Type
+
  
'''g++ callbyref.cpp -o ref1'''
+
'''g++ override.cpp -o over2'''
  
 +
'''./over2'''
  
press''' Enter'''
+
|-
 +
| 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
  
To execute
+
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:
  
Type
+
'''Addition of two numbers is 42'''
 +
 
 +
'''Difference of two numbers is 37'''
 +
 
 +
'''Product of two numbers is 30'''
  
'''./ref1'''
+
'''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.097cm;"| Highlight
+
| 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.
  
'''Output'''
+
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Here it is displayed as:  
+
| 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;"| Now we will see the difference of overloading and overriding.
  
'''Enter values of a and b'''
+
Overloading can occurs without inheritance.
  
I will enter
+
Overriding occurs when one class is inherited from another.
  
'''4'''
+
In overloading the arguments and the return-type must differ.
  
'''3'''
+
In overriding the arguments and the return type must be same.
  
The output is displayed as
+
In overloading the function name is same.
  
'''Before swapping a and b: 4 and 3'''
+
But it behaves differently depending on the arguments passed to them.
  
'''After swapping a and b: 3 and 4'''
+
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="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"|  
+
| 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="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| This brings us to the end of this tutorial.
+
| 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.
 
+
Let us go back to our slides.
+
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Slide 6
+
| 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
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us summarize, In this tutorial we learn't:
+
  
Function call by value.
+
Summary
 +
| 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 tutorial we have seen
  
Function call by reference.
+
'''Function overloading.'''
 +
 
 +
'''eg. int add(int, int, int); '''
 +
 
 +
'''float add(float, float); '''
 +
 
 +
'''Function Overriding.'''
 +
 
 +
'''eg. virtual int operations ()'''
 +
 
 +
'''int operations ()'''
 +
 
 +
'''Difference between both.'''
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Slide 7
+
| 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:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| As an assignment
+
| 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 similar program to calculate the cube of a number.
+
Write a program that will calculate the area of rectangle, square and circle.
  
Using call by value in C++.
+
Using function overloading.
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Slide 8
+
| 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:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Watch the video available at  
+
| 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
 
+
http://spoken-tutorial.org /What\_is\_a\_Spoken\_Tutorial
+
  
It summarises the Spoken Tutorial project  
+
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:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Slide 9
+
| 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:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| The Spoken Tutorial Project Team  
+
| 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 601:
 
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:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Slide 10
+
| 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:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Spoken Tutorial Project is a part of the Talk to a Teacher project
+
| 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:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"|  
+
| 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:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| This is Ashwini Patil from IIT Bombay
+
| 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.
  
 
|}
 
|}

Revision as of 13:33, 7 May 2013

Title of script: Function Overloading and Overriding in C++

Author: Ashwini Patil

Keywords: Overloading, Overriding, Video tutorial.


Visual Cue
Narration
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 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 now.

Highlight

#include<iostream>

This is our header file iostream.
Highlight

using namespace std;

This is the using statement.
Highlight

int add(int a, int b, int c)


Here we have add function defined as int.

In this we have passed three arguments.


Then we perform the addition of three numbers.

Highlight


Here we overload the function add.

It is declared as float.

We pass two arguments.

Then we perform the addition of 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 main function 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.

And 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;

This is the return statement
Click on Save Now Click on Save
Let us execute
Open the terminal

Ctrl, Alt and Tkeys simultaneously

Open the terminal by pressing Ctrl, Alt and T keys simultaneously
Type

g++ overload.cpp -o ovr

To execute

Type

./ovr

To compile the program type

g++ overload.cpp -o ovr

To execute type

./ovr

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

Then we see:

Enter two floating point numbers

I will enter as:

4.5

8.9

Sum of floating point numbers is 13.4

Slide 5 Now we will see function overriding.

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

I have already typed the code on the editor.

Lets take a look.

Highlight

#include <iostream>

using namespace std;

This is our header file as iostream.

This is our std namespace.

Highlight

class arithmetic

{

protected:

int a, b, sum, sub, mul, div;

Here 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";

}

};

Then we have virtual function as operations.

In this we calculate the addition of two numbers.

And print the sum.

Here we close the class.

Highlight

class Subtract: public arithmetic

{

Then 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.
Highlight

class Multiply: public arithmetic

{

public:

int operations ()

{

mul = a * b;

cout<< "Product of two numbers is "<< mul<<"\n";

}

};

Then we have another derived class as Multiply.

This also inherits class arithmetic.

In this we calculate the product of two numbers.

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.

Highlight

int main()

{

arithmetic *arith, p;

Subtract subt;

Multiply mult;

Divide divd;

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();

p is set to the address of arith.

Here we pass arguments as 30 and 12 in function values.

Then we call function operations.

Highlight

arith=&subt;

arith->values(42,5);

arith->operations();

Here we set subt to the address of arith.

Then 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.

Now we call function operations.

Highlight

arith=&divd;

arith->values(6,3);

arith->operations();

Then we set divd to the address of arith and pass arguments as 6 and 3.

Now we call function operations.

Highlight

return 0;

}

This is our return statement.

Now let us execute the program.

On the terminal


g++ override.cpp -o over2

./over2

Come back to our terminal.

To execute type:

g++ override.cpp -o over2

./over2

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

Division of two numbers is 2

On the slides Let us go back to our slides.
Slide 6 Now we will 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

In this tutorial we have seen

Function overloading.

eg. int add(int, int, int);

float add(float, float);

Function Overriding.

eg. virtual int operations ()

int operations ()

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


Acknowledgement

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.

Contributors and Content Editors

Ashwini