Advanced-C++/C2/Function-Overloading-And-Overriding/English-timed
From Script | Spoken-Tutorial
Revision as of 16:12, 27 October 2014 by PoojaMoolya (Talk | contribs)
Time | Narration |
00:01 | Welcome to the spoken tutorial on function Overloading and Overriding in C++. |
00:09 | In this tutorial, we will learn, |
00:11 | Function Overloading. |
00:12 | Function Overriding. |
00:14 | We will do this with the help of examples. |
00:18 | To record this tutorial, I am using |
00:21 | Ubuntu OS version 11.10 |
00:26 | g++ compiler v. 4.6.1 |
00:30 | Let us start with an introduction to function overloading. |
00:34 | Function Overloading means two or more functions can have same name. |
00:41 | The number of arguments and the data-type of the arguments will be different. |
00:47 | When a function is called it is selected based on the argument list. |
00:53 | Let us look at an example. |
00:56 | I have already typed the code on the editor. |
00:59 | In this program we will perform addition operations |
01:03 | Note that our file name is overload.cpp |
01:08 | Let me explain the code now. |
01:10 | This is our header file as iostream. |
01:13 | Here we are using the std namespace. |
01:17 | Then we have add function defined as int. |
01:21 | In this we have passed three arguments. |
01:24 | Int a, int b and int c |
01:28 | Then we perform addition of three numbers. And we return the value. |
01:33 | Here we overload the function add. |
01:36 | It is declared as float. |
01:38 | We had pass two arguments float d and float e |
01:44 | Then we perform the addition operation on two numbers. |
01:48 | This is our main function. |
01:50 | In function main we declare the add function with different arguments. |
01:56 | Then we declare the variables. |
01:58 | Here we accept integer values from the user. |
02:03 | Then we call the function add with three arguments. |
02:07 | And store the result in variable sum. |
02:09 | Here we print the result. |
02:12 | Now here we accept floating point numbers from the user. |
02:17 | Then we call the add function with two arguments. |
02:21 | And here we print the sum. |
02:23 | And this is our return statement |
02:26 | Now let us execute the program |
02:29 | Open the terminal window by pressing Ctrl, Alt and T keys simultaneously on your keyboard. |
02:38 | To execute type: g++ space overload dot cpp space hyphen o space over |
02:49 | Press Enter |
02:51 | Type dot slash over |
02:53 | Press Enter |
02:55 | It is displayed Enter three integers |
02:58 | I will enter 10, 25 and 48 |
03:04 | The output is displayed as: Sum of integers is 83 |
03:09 | Now we see: Enter two floating point numbers |
03:13 | I will enter as: 4.5 and 8.9 |
03:17 | Press Enter |
03:19 | The output is displayed as: Sum of floating point numbers is 13.4 |
03:25 | Now we will see function overriding. |
03:29 | Let us switch back to our slides. |
03:31 | Redefining a base class function in the derived class. |
03:36 | The derived class function overrides the base class function. |
03:40 | But the arguments passed are same. |
03:44 | And the return-type is also same. |
03:47 | Let us see an example |
03:49 | Here is an example on function Overriding |
03:53 | Note that our filename is override.cpp. |
03:57 | Let us go through the code. |
04:00 | This is our header file as iostream |
04:03 | Here we are using the std namespace. |
04:06 | Then we have class arithmetic. |
04:09 | In this we have declared integer variables as as protected. |
04:14 | Then we have function values declared as public. |
04:18 | In these we have passed two arguments int x and int y. |
04:23 | Then we stored the value in a and b. |
04:26 | Here we have virtual function as operations. |
04:30 | In this we add the two numbers and print the sum. |
04:34 | Here we close the class. |
04:37 | Now we have class Subtract as derived class. |
04:41 | This inherits the base class arithmetic. |
04:45 | In this we calculate the difference of two numbers and we print the difference. |
04:50 | Now we have another derived class as Multiply. |
04:54 | This also inherits base class arithmetic. |
04:57 | In this we calculate the product of two numbers and display the product. |
05:03 | Then we have class Divide this also inherits the base class arithmetic. |
05:09 | In this we calculate the division of two numbers and we display the division. |
05:15 | Note that the return type of the function is same and the arguments passed are also same. |
05:23 | This is our main function. |
05:26 | In this we create an object of class arithmetic as p. |
05:31 | arith is the pointer to the class arithmetic. |
05:35 | Then we have subt object of class Subtract. |
05:39 | mult object of class Multiply. |
05:42 | And divd object of class Divide. |
05:46 | Now here, p is set to the address of arith. |
05:50 | Then we pass arguments as 30 and 12 in function values. |
05:56 | Now we call the function operations. |
05:59 | This will perform the addition operation. |
06:02 | Here we set subt to the address of arith. |
06:07 | And we pass 42 and 5 as arguments. |
06:11 | Again we call function operations. |
06:14 | This will perform subtraction of two numbers. |
06:18 | Now, here we set mult to the address of arith. |
06:22 | And we pass 6 and 5 as argument |
06:26 | We call function operations. |
06:29 | This will perform multiplication of two numbers. |
06:33 | Atlast we set divd to the address of arith And we pass 6 and 3 as arguments. |
06:41 | Now we call operations functions |
06:44 | This will perform division of two numbers. |
06:48 | And this is our return statement. |
06:50 | Let us execute the program. Switch back to our terminal. |
06:54 | Type: g++ space override dot cpp space hyphen o space over2 |
07:04 | Press Enter |
07:06 | Type: dot slash over2 |
07:09 | Press Enter |
07:11 | The output is displayed as: |
07:13 | Addition of two numbers is 42 |
07:16 | Difference of two numbers is 37 |
07:19 | Product of two numbers is 30 and Division of two numbers is 2 |
07:25 | Let us switch back to our slides. |
07:27 | Let us see the difference of overloading and overriding. |
07:31 | Overloading can occurs without inheritance. |
07:35 | Overriding occurs when one class is inherited from another. |
07:41 | In overloading the arguments and the return-type must differ. |
07:46 | In overriding the arguments and the return type must be same. |
07:51 | In overloading the function name is same. |
07:55 | But it behaves differently depending on the arguments passed to them. |
08:01 | In overriding the function name is same. |
08:05 | Derived class function can perform different operations from the base class. |
08:11 | Let us summarize: |
08:13 | In this tutorial learnt |
08:15 | Function overloading. |
08:16 | eg. int add with three different arguments and |
08:21 | float add with two different arguments. |
08:24 | Function Overriding. |
08:26 | eg. virtual int operations () and int operations () |
08:31 | functions with the same argument and same return type and difference between both. |
08:38 | As an assignment |
08:39 | Write a program that will calculate the area of rectangle, square and circle |
08:46 | Using function overloading. |
08:48 | Watch the video available at the link shown below |
08:52 | It summarizes the Spoken Tutorial project |
08:55 | If you do not have good bandwidth, you can download and watch it |
08:59 | The Spoken Tutorial Project Team |
09:02 | Conducts workshops using spoken tutorials |
09:05 | Gives certificates to those who pass an online test |
09:09 | For more details, please write to, |
09:12 | contact@spoken-tutorial.org |
09:16 | Spoken Tutorial Project is a part of the Talk to a Teacher project |
09:20 | It is supported by the National Mission on Education through ICT, MHRD, Government of India |
09:27 | More information on this Mission is available at the link shown below |
09:32 | This is Ashwini Patil from IIT Bombay signing off |
09:36 | Thank You for joining. |