Difference between revisions of "Advanced-C++/C2/Function-Overloading-And-Overriding/English-timed"
From Script | Spoken-Tutorial
PoojaMoolya (Talk | contribs) |
Sandhya.np14 (Talk | contribs) |
||
Line 5: | Line 5: | ||
|- | |- | ||
|00:01 | |00:01 | ||
− | | Welcome to the spoken tutorial on '''function Overloading and Overriding''' in '''C++.''' | + | | Welcome to the '''spoken tutorial''' on '''function Overloading and Overriding''' in '''C++.''' |
|- | |- | ||
|00:09 | |00:09 | ||
− | | In this tutorial, we will learn | + | | In this tutorial, we will learn: |
|- | |- | ||
|00:11 | |00:11 | ||
− | |'''Function Overloading | + | |* '''Function Overloading''' |
|- | |- | ||
|00:12 | |00:12 | ||
− | |'''Function Overriding.''' | + | |* '''Function Overriding.''' |
|- | |- | ||
Line 25: | Line 25: | ||
|- | |- | ||
|00:18 | |00:18 | ||
− | | To record this tutorial, I am using | + | | To record this tutorial, I am using: |
|- | |- | ||
|00:21 | |00:21 | ||
− | |'''Ubuntu OS '''version 11.10 | + | |* '''Ubuntu OS '''version '''11.10''' |
|- | |- | ||
|00:26 | |00:26 | ||
− | |'''g++ compiler ''' | + | |* '''g++ compiler '''version '''4.6.1''' |
|- | |- | ||
Line 49: | Line 49: | ||
|- | |- | ||
|00:47 | |00:47 | ||
− | |When a '''function''' is called it is selected based on the argument list. | + | |When a '''function''' is called, it is selected based on the '''argument list'''. |
|- | |- | ||
Line 57: | Line 57: | ||
|- | |- | ||
|00:56 | |00:56 | ||
− | |I have already typed the code on the editor. | + | |I have already typed the '''code''' on the '''editor'''. |
|- | |- | ||
|00:59 | |00:59 | ||
− | |In this program we will perform addition operations | + | |In this program, we will perform addition operations. |
|- | |- | ||
|01:03 | |01:03 | ||
− | |Note that our file name is '''overload.cpp''' | + | |Note that our file name is '''overload.cpp'''. |
|- | |- | ||
Line 81: | Line 81: | ||
|- | |- | ||
|01:17 | |01:17 | ||
− | |Then we have '''add''' function defined as '''int.''' | + | |Then we have '''add()''' function defined as '''int.''' |
|- | |- | ||
|01:21 | |01:21 | ||
− | |In this we have passed three arguments. | + | |In this, we have passed three arguments. |
|- | |- | ||
|01:24 | |01:24 | ||
− | |'''Int a, int b''' and '''int c''' | + | |'''Int a, int b''' and '''int c'''. |
|- | |- | ||
Line 97: | Line 97: | ||
|- | |- | ||
|01:33 | |01:33 | ||
− | |Here we overload the function '''add.''' | + | |Here we '''overload''' the function '''add().''' |
|- | |- | ||
Line 105: | Line 105: | ||
|- | |- | ||
|01:38 | |01:38 | ||
− | |We | + | |We have passed two '''arguments''' '''float d''' and '''float e'''. |
|- | |- | ||
Line 113: | Line 113: | ||
|- | |- | ||
|01:48 | |01:48 | ||
− | |This is our '''main''' function. | + | |This is our '''main()''' function. |
|- | |- | ||
|01:50 | |01:50 | ||
− | |In | + | |In function '''main()''', we declare the '''add()''' '''function''' with different '''arguments'''. |
|- | |- | ||
Line 129: | Line 129: | ||
|- | |- | ||
|02:03 | |02:03 | ||
− | |Then we call the function '''add '''with three ''' arguments | + | |Then we call the function '''add() '''with three ''' arguments''' |
|- | |- | ||
|02:07 | |02:07 | ||
− | | | + | |and store the result in variable''' sum.''' |
|- | |- | ||
|02:09 | |02:09 | ||
− | |Here we print the result. | + | |Here we '''print''' the result. |
|- | |- | ||
|02:12 | |02:12 | ||
− | |Now here we accept '''floating point numbers''' from the user. | + | |Now, here we accept '''floating point numbers''' from the user. |
|- | |- | ||
|02:17 | |02:17 | ||
− | |Then we call the add function with two arguments. | + | |Then we call the '''add()''' function with two arguments. |
|- | |- | ||
Line 153: | Line 153: | ||
|- | |- | ||
|02:23 | |02:23 | ||
− | |And this is our '''return''' statement | + | |And this is our '''return''' statement. |
|- | |- | ||
|02:26 | |02:26 | ||
− | |Now let us execute the program | + | |Now, let us '''execute''' the program. |
|- | |- | ||
|02:29 | |02:29 | ||
− | |Open the terminal window by pressing '''Ctrl, Alt and T '''keys simultaneously on your keyboard. | + | |Open the '''terminal window''' by pressing '''Ctrl, Alt''' and '''T''' keys simultaneously on your keyboard. |
|- | |- | ||
|02:38 | |02:38 | ||
− | |To execute type: '''g++ space overload dot cpp space hyphen o space over''' | + | |To execute, type: '''g++ space overload dot cpp space hyphen o space over''' |
|- | |- | ||
|02:49 | |02:49 | ||
− | |Press '''Enter''' | + | |Press '''Enter'''. |
|- | |- | ||
|02:51 | |02:51 | ||
− | |Type '''dot slash over''' | + | |Type: '''dot slash over''' |
|- | |- | ||
|02:53 | |02:53 | ||
− | |Press '''Enter''' | + | |Press '''Enter'''. |
|- | |- | ||
|02:55 | |02:55 | ||
− | |It is displayed '''Enter three integers''' | + | |It is displayed- '''Enter three integers''' |
|- | |- | ||
Line 189: | Line 189: | ||
|- | |- | ||
|03:04 | |03:04 | ||
− | |The output is displayed as: '''Sum of integers is 83''' | + | |The '''output''' is displayed as: '''Sum of integers is 83''' |
|- | |- | ||
|03:09 | |03:09 | ||
− | |Now we see | + | |Now we see- '''Enter two floating point numbers''' |
|- | |- | ||
Line 201: | Line 201: | ||
|- | |- | ||
|03:17 | |03:17 | ||
− | |Press '''Enter''' | + | |Press '''Enter'''. |
|- | |- | ||
|03:19 | |03:19 | ||
− | |The output is displayed as: '''Sum of floating point numbers is 13.4''' | + | |The '''output''' is displayed as: '''Sum of floating point numbers is 13.4''' |
|- | |- | ||
Line 213: | Line 213: | ||
|- | |- | ||
|03:29 | |03:29 | ||
− | |Let us switch back to our slides. | + | |Let us switch back to our '''slides'''. |
|- | |- | ||
|03:31 | |03:31 | ||
− | |Redefining a '''base class''' '''function''' in the derived class. | + | |Redefining a '''base class''' '''function''' in the '''derived class'''. |
|- | |- | ||
|03:36 | |03:36 | ||
− | |The | + | |The '''derived class''' function '''overrides''' the '''base class''' function. |
|- | |- | ||
Line 233: | Line 233: | ||
|- | |- | ||
|03:47 | |03:47 | ||
− | |Let us see an example | + | |Let us see an example. |
|- | |- | ||
|03:49 | |03:49 | ||
− | |Here is an example on '''function Overriding''' | + | |Here is an example on '''function Overriding'''. |
|- | |- | ||
Line 249: | Line 249: | ||
|- | |- | ||
|04:00 | |04:00 | ||
− | |This is our header file as '''iostream''' | + | |This is our '''header file''' as '''iostream'''. |
|- | |- | ||
Line 261: | Line 261: | ||
|- | |- | ||
|04:09 | |04:09 | ||
− | |In this we have declared integer variables as as '''protected.''' | + | |In this, we have declared '''integer variables''' as as '''protected.''' |
|- | |- | ||
Line 269: | Line 269: | ||
|- | |- | ||
|04:18 | |04:18 | ||
− | |In these we have passed two arguments '''int x''' and '''int y'''. | + | |In these, we have passed two '''arguments'''- '''int x''' and '''int y'''. |
|- | |- | ||
Line 281: | Line 281: | ||
|- | |- | ||
|04:30 | |04:30 | ||
− | |In this we add the two numbers and print the '''sum.''' | + | |In this, we add the two numbers and print the '''sum.''' |
|- | |- | ||
|04:34 | |04:34 | ||
− | |Here we close the class. | + | |Here we close the '''class'''. |
|- | |- | ||
Line 297: | Line 297: | ||
|- | |- | ||
|04:45 | |04:45 | ||
− | |In this we calculate the difference of two numbers and we print the difference. | + | |In this, we calculate the difference of two numbers and we print the difference. |
|- | |- | ||
Line 309: | Line 309: | ||
|- | |- | ||
|04:57 | |04:57 | ||
− | |In this we calculate the product of two numbers and display the product. | + | |In this, we calculate the product of two numbers and display the product. |
|- | |- | ||
|05:03 | |05:03 | ||
− | |Then we have '''class Divide ''' | + | |Then we have '''class Divide '''. This also inherits the''' base class arithmetic.''' |
|- | |- | ||
|05:09 | |05:09 | ||
− | |In this we calculate the division of two numbers and we display the division. | + | |In this, we calculate the division of two numbers and we display the division. |
|- | |- | ||
|05:15 | |05:15 | ||
− | |Note that the return type of the function is same and the arguments passed are also same. | + | |Note that the '''return type''' of the function is same and the arguments passed are also same. |
|- | |- | ||
|05:23 | |05:23 | ||
− | |This is our '''main | + | |This is our '''main()''' function. |
|- | |- | ||
|05:26 | |05:26 | ||
− | |In this we create an''' object '''of ''' class arithmetic''' as '''p.''' | + | |In this, we create an''' object '''of ''' class arithmetic''' as '''p.''' |
|- | |- | ||
Line 349: | Line 349: | ||
|- | |- | ||
|05:46 | |05:46 | ||
− | |Now here, '''p''' is set to the address of '''arith. | + | |Now here, '''p''' is set to the address of '''arith'''. |
|- | |- | ||
Line 357: | Line 357: | ||
|- | |- | ||
|05:56 | |05:56 | ||
− | |Now we call the '''function operations.''' | + | |Now we call the '''function operations().''' |
|- | |- | ||
Line 365: | Line 365: | ||
|- | |- | ||
|06:02 | |06:02 | ||
− | |Here we set '''subt''' to the address of '''arith.''' | + | |Here we set '''subt''' to the '''address''' of '''arith.''' |
|- | |- | ||
Line 373: | Line 373: | ||
|- | |- | ||
|06:11 | |06:11 | ||
− | |Again we call''' function operations.''' | + | |Again we call''' function operations().''' |
|- | |- | ||
Line 381: | Line 381: | ||
|- | |- | ||
|06:18 | |06:18 | ||
− | |Now, here we set '''mult''' to the address of '''arith.''' | + | |Now, here we set '''mult''' to the '''address''' of '''arith.''' |
|- | |- | ||
|06:22 | |06:22 | ||
− | |And we pass ''' 6 '''and''' 5''' as | + | |And we pass ''' 6 '''and''' 5''' as arguments. |
|- | |- | ||
|06:26 | |06:26 | ||
− | |We call '''function operations.''' | + | |We call '''function operations().''' |
|- | |- | ||
Line 397: | Line 397: | ||
|- | |- | ||
|06:33 | |06:33 | ||
− | | | + | |At last, we set '''divd''' to the '''address''' of '''arith''' and we pass '''6''' and '''3''' as '''arguments.''' |
|- | |- | ||
|06:41 | |06:41 | ||
− | |Now we | + | |Now we call '''operations()''' function. |
|- | |- | ||
Line 413: | Line 413: | ||
|- | |- | ||
|06:50 | |06:50 | ||
− | |Let us execute the program. Switch back to our terminal. | + | |Let us '''execute''' the program. Switch back to our '''terminal'''. |
|- | |- | ||
Line 421: | Line 421: | ||
|- | |- | ||
|07:04 | |07:04 | ||
− | |Press '''Enter''' | + | |Press '''Enter'''. |
|- | |- | ||
Line 429: | Line 429: | ||
|- | |- | ||
|07:09 | |07:09 | ||
− | |Press '''Enter''' | + | |Press '''Enter'''. |
|- | |- | ||
|07:11 | |07:11 | ||
− | |The output is displayed as: | + | |The '''output''' is displayed as: |
|- | |- | ||
Line 449: | Line 449: | ||
|- | |- | ||
|07:25 | |07:25 | ||
− | |Let us switch back to our slides. | + | |Let us switch back to our '''slides'''. |
|- | |- | ||
Line 457: | Line 457: | ||
|- | |- | ||
|07:31 | |07:31 | ||
− | |'''Overloading''' can | + | |'''Overloading''' can occur without '''inheritance'''. |
|- | |- | ||
|07:35 | |07:35 | ||
− | |'''Overriding''' occurs when one class is inherited from another. | + | |'''Overriding''' occurs when one '''class''' is inherited from another. |
|- | |- | ||
|07:41 | |07:41 | ||
− | |In '''overloading''' the arguments and the return-type must differ. | + | |In '''overloading''', the '''arguments''' and the '''return-type''' must differ. |
|- | |- | ||
|07:46 | |07:46 | ||
− | |In '''overriding''' the arguments and the return type must be same. | + | |In '''overriding''', the '''arguments''' and the '''return-type''' must be same. |
|- | |- | ||
|07:51 | |07:51 | ||
− | |In '''overloading''' the function name is same. | + | |In '''overloading''', the function name is same. |
|- | |- | ||
Line 481: | Line 481: | ||
|- | |- | ||
|08:01 | |08:01 | ||
− | |In '''overriding''' the function name is same. | + | |In '''overriding''', the function name is same. |
|- | |- | ||
|08:05 | |08:05 | ||
− | |'''Derived class '''function can perform different operations from the base class. | + | |'''Derived class '''function can perform different operations from the '''base class'''. |
|- | |- | ||
Line 493: | Line 493: | ||
|- | |- | ||
|08:13 | |08:13 | ||
− | |In this tutorial learnt | + | |In this tutorial, we learnt: |
|- | |- | ||
|08:15 | |08:15 | ||
− | |'''Function overloading | + | |* '''Function overloading''' |
|- | |- | ||
Line 509: | Line 509: | ||
|- | |- | ||
|08:24 | |08:24 | ||
− | |'''Function Overriding | + | |* '''Function Overriding''' |
|- | |- | ||
Line 517: | Line 517: | ||
|- | |- | ||
|08:31 | |08:31 | ||
− | | | + | |* Functions with the same argument and same return-type and difference between both. |
|- | |- | ||
|08:38 | |08:38 | ||
− | |As an assignment | + | |As an assignment: |
|- | |- | ||
Line 529: | Line 529: | ||
|- | |- | ||
|08:46 | |08:46 | ||
− | | | + | |using '''function overloading.''' |
|- | |- | ||
|08:48 | |08:48 | ||
− | |Watch the video available at the link shown below | + | |Watch the video available at the link shown below. |
|- | |- | ||
|08:52 | |08:52 | ||
− | |It summarizes the Spoken Tutorial project | + | |It summarizes the Spoken Tutorial project. |
|- | |- | ||
|08:55 | |08:55 | ||
− | |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. |
|- | |- | ||
|08:59 | |08:59 | ||
− | |The Spoken Tutorial Project Team | + | |The Spoken Tutorial Project Team: |
|- | |- | ||
|09:02 | |09:02 | ||
− | |Conducts workshops using spoken tutorials | + | |Conducts workshops using spoken tutorials. |
|- | |- | ||
|09:05 | |09:05 | ||
− | |Gives certificates to those who pass an online test | + | |Gives certificates to those who pass an online test. |
|- | |- | ||
|09:09 | |09:09 | ||
− | |For more details, please write to | + | |For more details, please write to: |
|- | |- | ||
Line 565: | Line 565: | ||
|- | |- | ||
|09:16 | |09:16 | ||
− | |Spoken Tutorial Project is a part of the Talk to a Teacher project | + | |Spoken Tutorial Project is a part of the "Talk to a Teacher" project. |
|- | |- | ||
|09:20 | |09:20 | ||
− | |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. |
|- | |- | ||
|09:27 | |09:27 | ||
− | |More information on this | + | |More information on this mission is available at the link shown below. |
|- | |- | ||
| 09:32 | | 09:32 | ||
− | | This is Ashwini Patil from IIT Bombay signing off | + | | This is Ashwini Patil from IIT Bombay, signing off. |
|- | |- |
Revision as of 21:27, 28 June 2015
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 version 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 have passed 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 arguments. |
06:26 | We call function operations(). |
06:29 | This will perform multiplication of two numbers. |
06:33 | At last, we set divd to the address of arith and we pass 6 and 3 as arguments. |
06:41 | Now we call operations() function. |
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 occur 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, we 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. |