Difference between revisions of "Advanced-C++/C2/Constructor-And-Destructor/English-timed"
From Script | Spoken-Tutorial
PoojaMoolya (Talk | contribs) |
Sandhya.np14 (Talk | contribs) |
||
Line 6: | Line 6: | ||
|- | |- | ||
| 00:01 | | 00:01 | ||
− | | Welcome to the spoken tutorial | + | | Welcome to the '''spoken tutorial''' on '''Constructors and Destructors''' in '''C++'''. |
|- | |- | ||
| 00:07 | | 00:07 | ||
− | | In this tutorial we will learn | + | | In this tutorial, we will learn: |
|- | |- | ||
| 00:09 | | 00:09 | ||
− | | '''Constructors''' | + | |* '''Constructors''' |
|- | |- | ||
| 00:10 | | 00:10 | ||
− | | '''Types of constructors''' | + | |* '''Types of constructors''' |
|- | |- | ||
| 00:12 | | 00:12 | ||
− | | '''Destructors.''' | + | |* '''Destructors.''' |
|- | |- | ||
Line 30: | Line 30: | ||
|- | |- | ||
| 00:17 | | 00:17 | ||
− | | To record this tutorial, I am using | + | | To record this tutorial, I am using: |
|- | |- | ||
| 00:20 | | 00:20 | ||
− | | '''Ubuntu Operating System''' version 11.10, | + | |* '''Ubuntu Operating System''' version 11.10, |
|- | |- | ||
| 00:23 | | 00:23 | ||
− | | '''g++''' '''Compiler''' version 4.6.1. | + | |* '''g++''' '''Compiler''' version 4.6.1. |
|- | |- | ||
Line 58: | Line 58: | ||
|- | |- | ||
| 00:40 | | 00:40 | ||
− | | It is automatically called when an object is created. | + | | It is automatically called when an '''object''' is created. |
|- | |- | ||
Line 66: | Line 66: | ||
|- | |- | ||
| 00:46 | | 00:46 | ||
− | | '''Parameterized Constructors.''' | + | |* '''Parameterized Constructors.''' |
|- | |- | ||
| 00:49 | | 00:49 | ||
− | | '''Copy Constructors.''' | + | |* '''Copy Constructors.'''and |
|- | |- | ||
| 00:50 | | 00:50 | ||
− | | | + | |* '''Default Constructors.''' |
|- | |- | ||
Line 94: | Line 94: | ||
|- | |- | ||
| 01:07 | | 01:07 | ||
− | | Let us see an example on '''Constructors''' and '''Destructors''' | + | | Let us see an example on '''Constructors''' and '''Destructors'''. |
|- | |- | ||
| 01:11 | | 01:11 | ||
− | | I have already typed the code on the editor, I will open it. | + | | I have already typed the code on the '''editor''', I will open it. |
|- | |- | ||
| 01:15 | | 01:15 | ||
− | | Note that our filename is '''cons hyphen dest dot cpp''' | + | | Note that our filename is '''cons hyphen dest dot cpp'''. |
|- | |- | ||
| 01:20 | | 01:20 | ||
− | | In this program we will perform the addition of two numbers using '''constructor.''' | + | | In this program, we will perform the addition of two numbers using '''constructor.''' |
|- | |- | ||
Line 122: | Line 122: | ||
|- | |- | ||
| 01:33 | | 01:33 | ||
− | | Then we have class '''Addition'''. | + | | Then we have class '''Addition'''. 'a' and 'b' are the '''integer variables'''. |
|- | |- | ||
| 01:38 | | 01:38 | ||
− | |These are the private members of class '''Addition.''' | + | |These are the '''private''' members of class '''Addition.''' |
|- | |- | ||
Line 134: | Line 134: | ||
|- | |- | ||
| 01:44 | | 01:44 | ||
− | | Then we have the '''construtor''' '''Addition''' | + | | Then we have the '''construtor''' '''Addition'''. |
|- | |- | ||
| 01:47 | | 01:47 | ||
− | | The '''constructor''' has the same name as the class name. | + | | The '''constructor''' has the same name as the '''class''' name. |
|- | |- | ||
| 01:52 | | 01:52 | ||
− | | We have passed two | + | | We have passed two '''arguement'''s here. |
|- | |- | ||
Line 150: | Line 150: | ||
|- | |- | ||
| 01:57 | | 01:57 | ||
− | | For this we use a '''tilde''' sign followed by the ''' | + | | For this we use a '''tilde''' sign followed by the '''destructor's''' name. |
|- | |- | ||
| 02:02 | | 02:02 | ||
− | | This is a public function '''add.''' | + | | This is a '''public''' function '''add.''' |
|- | |- | ||
| 02:05 | | 02:05 | ||
− | | It returns sum of a and b. | + | | It returns sum of 'a' and 'b'. |
|- | |- | ||
| 02:08 | | 02:08 | ||
− | | Here we access the constructor using the '''scope resolution operator.''' | + | | Here, we access the '''constructor''' using the '''scope resolution operator.''' |
|- | |- | ||
| 02:12 | | 02:12 | ||
− | | a and b are private members. | + | | 'a' and 'b' are '''private''' members. |
|- | |- | ||
| 02:15 | | 02:15 | ||
− | | To access the private members we use x and y. | + | | To access the private members, we use 'x' and 'y'. |
|- | |- | ||
Line 178: | Line 178: | ||
|- | |- | ||
| 02:21 | | 02:21 | ||
− | | In this we print '''Memory Deallocation.''' | + | | In this, we print '''Memory Deallocation.''' |
|- | |- | ||
Line 186: | Line 186: | ||
|- | |- | ||
| 02:28 | | 02:28 | ||
− | | Here we create an object '''obj''' of class '''Addition'''. | + | | Here, we create an object '''obj''' of '''class''' '''Addition'''. |
|- | |- | ||
| 02:32 | | 02:32 | ||
− | | Then we pass two | + | | Then we pass two '''argument'''s as '''3''' and '''4'''. |
|- | |- | ||
| 02:36 | | 02:36 | ||
− | | '''3''' will be stored in | + | | '''3''' will be stored in 'x' and '''4''' will be stored in 'y'. |
|- | |- | ||
| 02:40 | | 02:40 | ||
− | | This means | + | | This means 'a's value is '''3 '''and 'b's value is '''4.''' |
|- | |- | ||
| 02:45 | | 02:45 | ||
− | | The constructor that has | + | | The '''constructor''' that has '''argument'''s is called '''parameterized constructor.''' |
|- | |- | ||
Line 210: | Line 210: | ||
|- | |- | ||
| 02:53 | | 02:53 | ||
− | | Here we call the function '''add '''using the object''' obj'''. | + | | Here, we call the function '''add '''using the object''' obj'''. |
|- | |- | ||
| 02:58 | | 02:58 | ||
− | |And we print the sum. | + | |And we '''print''' the sum. |
|- | |- | ||
Line 222: | Line 222: | ||
|- | |- | ||
| 03:02 | | 03:02 | ||
− | | Now let us execute the program. | + | | Now let us '''execute''' the program. |
|- | |- | ||
Line 230: | Line 230: | ||
|- | |- | ||
| 03:12 | | 03:12 | ||
− | | To compile | + | | To compile, type: '''g++ space cons hyphen dest dot cpp space hyphen o space cons''' |
|- | |- | ||
| 03:21 | | 03:21 | ||
− | | '''Press Enter''' | + | | '''Press Enter'''. |
|- | |- | ||
| 03:23 | | 03:23 | ||
− | | Type '''dot slash cons''' | + | | Type: '''dot slash cons''' |
|- | |- | ||
| 03:25 | | 03:25 | ||
− | | Press '''Enter''' | + | | Press '''Enter'''. |
|- | |- | ||
| 03:27 | | 03:27 | ||
− | | The output is displayed as | + | | The output is displayed as: |
|- | |- | ||
| 03:29 | | 03:29 | ||
− | | '''Sum is 7''' and '''Memory Deallocation''' | + | | '''Sum is 7''' and '''Memory Deallocation'''. |
|- | |- | ||
Line 258: | Line 258: | ||
|- | |- | ||
| 03:37 | | 03:37 | ||
− | | Come back | + | | Come back to our program. |
|- | |- | ||
Line 274: | Line 274: | ||
|- | |- | ||
| 03:48 | | 03:48 | ||
− | | | + | | then the '''compiler''' assumes a '''default constructor''' for the '''class'''. |
|- | |- | ||
Line 282: | Line 282: | ||
|- | |- | ||
| 03:55 | | 03:55 | ||
− | | This is our header file as '''iostream.''' | + | | This is our '''header file''' as '''iostream.''' |
|- | |- | ||
Line 294: | Line 294: | ||
|- | |- | ||
| 04:04 | | 04:04 | ||
− | | Then we have a and b declared as''' public.''' | + | | Then we have 'a' and 'b' declared as''' public.''' |
|- | |- | ||
| 04:08 | | 04:08 | ||
− | | Here we have '''sub''' function. | + | | Here, we have '''sub''' function. |
|- | |- | ||
Line 306: | Line 306: | ||
|- | |- | ||
| 04:15 | | 04:15 | ||
− | | This returns the subtraction of two numbers | + | | This returns the subtraction of two numbers 'a' and 'b.' |
|- | |- | ||
Line 314: | Line 314: | ||
|- | |- | ||
| 04:22 | | 04:22 | ||
− | | Here we access the default constructor using the''' scope resolution operator.''' | + | | Here, we access the default constructor using the''' scope resolution operator.''' |
|- | |- | ||
Line 322: | Line 322: | ||
|- | |- | ||
| 04:29 | | 04:29 | ||
− | | In this we have defined a variable | + | | In this we have defined a variable 'x.' |
|- | |- | ||
| 04:34 | | 04:34 | ||
− | | Here we create an '''object | + | | Here we create an '''object''' 's' of class '''subtraction.''' |
|- | |- | ||
Line 338: | Line 338: | ||
|- | |- | ||
| 04:47 | | 04:47 | ||
− | | Then the result is stored in variable | + | | Then the result is stored in variable' x.' |
|- | |- | ||
| 04:51 | | 04:51 | ||
− | | And here we print the difference. | + | | And here we '''print''' the difference. |
|- | |- | ||
Line 350: | Line 350: | ||
|- | |- | ||
| 04:56 | | 04:56 | ||
− | | Now let us execute the program. | + | | Now, let us '''execute''' the program. |
|- | |- | ||
| 04:58 | | 04:58 | ||
− | | Come back to our terminal. | + | | Come back to our '''terminal'''. |
|- | |- | ||
| 05:01 | | 05:01 | ||
− | | Let us compile type '''g++ space default dot cpp space hyphen o space def''' | + | | Let us compile, type: '''g++ space default dot cpp space hyphen o space def''' |
|- | |- | ||
| 05:09 | | 05:09 | ||
− | | '''Press Enter''' | + | | '''Press Enter'''. |
|- | |- | ||
| 05:10 | | 05:10 | ||
− | | Type '''dot slash def''' | + | | Type: '''dot slash def''' |
|- | |- | ||
| 05:12 | | 05:12 | ||
− | | '''Press Enter''' | + | | '''Press Enter'''. |
|- | |- | ||
| 05:14 | | 05:14 | ||
− | | The output is | + | | The output is returned as: |
|- | |- | ||
| 05:16 | | 05:16 | ||
− | | '''Difference is 4''' | + | | '''Difference is 4'''. |
|- | |- | ||
Line 386: | Line 386: | ||
|- | |- | ||
| 05:20 | | 05:20 | ||
− | | Here you can see that we have passed the | + | | Here you can see that we have passed the '''argument'''s within the '''function'''. |
|- | |- | ||
| 05:25 | | 05:25 | ||
− | | And in our previous example we have passed the arguments within the '''Object.''' | + | | And in our previous example, we have passed the arguments within the '''Object.''' |
|- | |- | ||
Line 398: | Line 398: | ||
|- | |- | ||
| 05:34 | | 05:34 | ||
− | | Now let us go back to our | + | | Now let us go back to our '''slide'''s. |
|- | |- | ||
| 05:38 | | 05:38 | ||
− | | Let us summarize | + | | Let us summarize. In this tutorial we learned: |
|- | |- | ||
| 05:41 | | 05:41 | ||
− | | '''Constructors.''' eg. '''Addition''' | + | |* '''Constructors.''' eg. '''Addition''' |
|- | |- | ||
| 05:43 | | 05:43 | ||
− | | '''Parameterized Constructor.''' eg. '''Addition obj (3, 4);''' | + | |* '''Parameterized Constructor.''' eg. '''Addition obj (3, 4);''' |
|- | |- | ||
| 05:48 | | 05:48 | ||
− | | '''Destructors''' eg.''' ~Addition''' | + | |* '''Destructors.''' eg.''' ~Addition''' |
|- | |- | ||
| 05:52 | | 05:52 | ||
− | | '''Default Constructor.''' eg. '''Subtraction''' | + | |* '''Default Constructor.''' eg. '''Subtraction''' |
|- | |- | ||
| 05:55 | | 05:55 | ||
− | | As an assignment, | + | | As an assignment, create a '''class''' named '''Division.''' |
|- | |- | ||
| 05:59 | | 05:59 | ||
− | | Create a constructor for the class. | + | | Create a '''constructor''' for the '''class'''. |
|- | |- | ||
Line 434: | Line 434: | ||
|- | |- | ||
| 06:06 | | 06:06 | ||
− | | Watch the video available at the link shown below | + | | Watch the video available at the link shown below. |
|- | |- | ||
| 06:09 | | 06:09 | ||
− | | It | + | | It summarizes the Spoken Tutorial project. |
|- | |- | ||
Line 446: | Line 446: | ||
|- | |- | ||
| 06:16 | | 06:16 | ||
− | | The Spoken Tutorial Project Team, Conducts workshops using spoken tutorials . | + | | The Spoken Tutorial Project Team, Conducts workshops using spoken tutorials. . |
|- | |- | ||
Line 454: | Line 454: | ||
|- | |- | ||
| 06:25 | | 06:25 | ||
− | | For more details, please write to | + | | For more details, please write to: |
|- | |- | ||
Line 462: | Line 462: | ||
|- | |- | ||
| 06:31 | | 06:31 | ||
− | | 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. |
|- | |- | ||
Line 470: | Line 470: | ||
|- | |- | ||
| 06:42 | | 06:42 | ||
− | | More information on this | + | | More information on this mission is available at the link shown below. |
|- | |- | ||
| 06:47 | | 06:47 | ||
− | | This is Ashwini Patil from IIT Bombay signing off. Thank You for joining. | + | | This is Ashwini Patil from IIT Bombay, signing off. Thank You for joining. |
|} | |} |
Revision as of 10:37, 27 June 2015
Time | Narration |
00:01 | Welcome to the spoken tutorial on Constructors and Destructors in C++. |
00:07 | In this tutorial, we will learn: |
00:09 | * Constructors |
00:10 | * Types of constructors |
00:12 | * Destructors. |
00:13 | We will do this with the help of some examples. |
00:17 | To record this tutorial, I am using: |
00:20 | * Ubuntu Operating System version 11.10, |
00:23 | * g++ Compiler version 4.6.1. |
00:28 | Let us start with an introduction to Constructors. |
00:31 | A constructor is a member function. |
00:33 | It has the same name as the class name. |
00:37 | Constructors cannot return values. |
00:40 | It is automatically called when an object is created. |
00:44 | Types of Constructors : |
00:46 | * Parameterized Constructors. |
00:49 | * Copy Constructors.and |
00:50 | * Default Constructors. |
00:53 | Let us move on to Destructors. |
00:56 | Destructors are used to deallocate memory. |
00:59 | They are called when an object is destroyed. |
01:02 | A destructor takes no arguments and has no return types. |
01:07 | Let us see an example on Constructors and Destructors. |
01:11 | I have already typed the code on the editor, I will open it. |
01:15 | Note that our filename is cons hyphen dest dot cpp. |
01:20 | In this program, we will perform the addition of two numbers using constructor. |
01:25 | Let me explain the code now. |
01:27 | This is our header file as iostream |
01:30 | Here we have used std namespace. |
01:33 | Then we have class Addition. 'a' and 'b' are the integer variables. |
01:38 | These are the private members of class Addition. |
01:42 | Here we have the Public specifier. |
01:44 | Then we have the construtor Addition. |
01:47 | The constructor has the same name as the class name. |
01:52 | We have passed two arguements here. |
01:54 | Now we have defined a Destructor. |
01:57 | For this we use a tilde sign followed by the destructor's name. |
02:02 | This is a public function add. |
02:05 | It returns sum of 'a' and 'b'. |
02:08 | Here, we access the constructor using the scope resolution operator. |
02:12 | 'a' and 'b' are private members. |
02:15 | To access the private members, we use 'x' and 'y'. |
02:19 | Then we access the destructor. |
02:21 | In this, we print Memory Deallocation. |
02:25 | This is our main function. |
02:28 | Here, we create an object obj of class Addition. |
02:32 | Then we pass two arguments as 3 and 4. |
02:36 | 3 will be stored in 'x' and 4 will be stored in 'y'. |
02:40 | This means 'a's value is 3 and 'b's value is 4. |
02:45 | The constructor that has arguments is called parameterized constructor. |
02:50 | Hence this one is the prameterized constructor. |
02:53 | Here, we call the function add using the object obj. |
02:58 | And we print the sum. |
03:00 | This is our return statement. |
03:02 | Now let us execute the program. |
03:05 | Open the terminal window by pressing Ctrl, Alt and T keys simultaneously on your keyboard. |
03:12 | To compile, type: g++ space cons hyphen dest dot cpp space hyphen o space cons |
03:21 | Press Enter. |
03:23 | Type: dot slash cons |
03:25 | Press Enter. |
03:27 | The output is displayed as: |
03:29 | Sum is 7 and Memory Deallocation. |
03:33 | Now let us see an example on Default constructors. |
03:37 | Come back to our program. |
03:39 | I have already typed the code. |
03:41 | Note that our filename is default dot cpp |
03:45 | If a constructor is not declared in the class, |
03:48 | then the compiler assumes a default constructor for the class. |
03:53 | Let me explain the code. |
03:55 | This is our header file as iostream. |
03:58 | Here we have used std namespace. |
04:02 | Then we have class Subtraction. |
04:04 | Then we have 'a' and 'b' declared as public. |
04:08 | Here, we have sub function. |
04:10 | We have passed two arguments int a and int b. |
04:15 | This returns the subtraction of two numbers 'a' and 'b.' |
04:19 | This is the defalut constructor. |
04:22 | Here, we access the default constructor using the scope resolution operator. |
04:27 | This is our main function. |
04:29 | In this we have defined a variable 'x.' |
04:34 | Here we create an object 's' of class subtraction. |
04:39 | Then we call the function sub using the object s. |
04:42 | And pass two arguments as 8 and 4. |
04:47 | Then the result is stored in variable' x.' |
04:51 | And here we print the difference. |
04:54 | This is our return statement. |
04:56 | Now, let us execute the program. |
04:58 | Come back to our terminal. |
05:01 | Let us compile, type: g++ space default dot cpp space hyphen o space def |
05:09 | Press Enter. |
05:10 | Type: dot slash def |
05:12 | Press Enter. |
05:14 | The output is returned as: |
05:16 | Difference is 4. |
05:18 | Come back to our program. |
05:20 | Here you can see that we have passed the arguments within the function. |
05:25 | And in our previous example, we have passed the arguments within the Object. |
05:30 | And here we have passed the arguments using the Object. |
05:34 | Now let us go back to our slides. |
05:38 | Let us summarize. In this tutorial we learned: |
05:41 | * Constructors. eg. Addition |
05:43 | * Parameterized Constructor. eg. Addition obj (3, 4); |
05:48 | * Destructors. eg. ~Addition |
05:52 | * Default Constructor. eg. Subtraction |
05:55 | As an assignment, create a class named Division. |
05:59 | Create a constructor for the class. |
06:01 | And Create a function divide which divides two given numbers. |
06:06 | Watch the video available at the link shown below. |
06:09 | It summarizes the Spoken Tutorial project. |
06:11 | If you do not have good bandwidth, you can download and watch it. |
06:16 | The Spoken Tutorial Project Team, Conducts workshops using spoken tutorials. . |
06:21 | Gives certificates to those who pass an online test . |
06:25 | For more details, please write to: |
06:27 | contact@spoken hyphen tutorial dot org |
06:31 | Spoken Tutorial Project is a part of the "Talk to a Teacher" project. |
06:36 | It is supported by the National Mission on Education through ICT, MHRD, Government of India. |
06:42 | More information on this mission is available at the link shown below. |
06:47 | This is Ashwini Patil from IIT Bombay, signing off. Thank You for joining. |