Difference between revisions of "Advanced-C++/C2/Constructor-And-Destructor/English-timed"
From Script | Spoken-Tutorial
(Created page with '{| border = 1 |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 | '…') |
|||
(9 intermediate revisions by 4 users not shown) | |||
Line 1: | Line 1: | ||
{| border = 1 | {| border = 1 | ||
− | |Time | + | |'''Time''' |
− | |Narration | + | |'''Narration''' |
|- | |- | ||
| 00:01 | | 00:01 | ||
− | | Welcome to the spoken tutorial on Constructors and Destructors in C++. | + | | 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''' Types of '''constructors''' |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
|- | |- | ||
| 00:12 | | 00:12 | ||
− | | '''Destructors.''' | + | |'''Destructors.'''We will do this with the help of some examples. |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
|- | |- | ||
| 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''' |
|- | |- | ||
| 00:28 | | 00:28 | ||
− | | Let us start with an introduction to ''' | + | | Let us start with an introduction to '''Constructor. ''' |
|- | |- | ||
| 00:31 | | 00:31 | ||
− | | A '''constructor''' is a member function. | + | | A '''constructor''' is a member '''function'''. |
|- | |- | ||
Line 58: | Line 50: | ||
|- | |- | ||
| 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 58: | ||
|- | |- | ||
| 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 86: | ||
|- | |- | ||
| 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 114: | Line 106: | ||
|- | |- | ||
| 01:27 | | 01:27 | ||
− | | This is our '''header file as iostream''' | + | | This is our '''header file''' as '''iostream'''. |
|- | |- | ||
| 01:30 | | 01:30 | ||
− | | Here we have used '''std''' | + | | Here we have used '''std namespace.''' |
|- | |- | ||
| 01:33 | | 01:33 | ||
− | | Then we have class '''Addition'''. '''a''' and '''b''' are the integer variables. | + | | 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 126: | ||
|- | |- | ||
| 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 arguements here. | + | | We have passed two '''arguements''' here. |
|- | |- | ||
| 01:54 | | 01:54 | ||
− | | Now we have defined a Destructor. | + | | Now we have defined a '''Destructor.''' |
|- | |- | ||
| 01:57 | | 01:57 | ||
− | | For this we use a '''tilde''' sign followed by the destructors name. | + | | For this we use a '''tilde''' sign followed by the '''destructors''' 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 170: | ||
|- | |- | ||
| 02:21 | | 02:21 | ||
− | | In this we print '''Memory Deallocation.''' | + | | In this, we print''' "Memory Deallocation".''' |
− | + | ||
|- | |- | ||
Line 187: | Line 178: | ||
|- | |- | ||
| 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'''. |
|- | |- | ||
Line 199: | Line 190: | ||
|- | |- | ||
| 02:40 | | 02:40 | ||
− | | This means '''a | + | | 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 211: | Line 202: | ||
|- | |- | ||
| 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 223: | Line 214: | ||
|- | |- | ||
| 03:02 | | 03:02 | ||
− | | Now let us execute the program. | + | | Now let us '''execute''' the program. |
|- | |- | ||
| 03:05 | | 03:05 | ||
− | | 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. |
|- | |- | ||
| 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 259: | Line 250: | ||
|- | |- | ||
| 03:37 | | 03:37 | ||
− | | Come back | + | | Come back to our program. |
|- | |- | ||
Line 267: | Line 258: | ||
|- | |- | ||
| 03:41 | | 03:41 | ||
− | | Note that our | + | | Note that our file name is '''default dot cpp'''. |
|- | |- | ||
| 03:45 | | 03:45 | ||
− | | If a '''constructor''' is not declared in the '''class | + | | If a '''constructor''' is not declared in the '''class ''' |
|- | |- | ||
| 03:48 | | 03:48 | ||
− | | | + | | then the '''compiler''' assumes a '''default constructor''' for the '''class'''. |
|- | |- | ||
Line 283: | Line 274: | ||
|- | |- | ||
| 03:55 | | 03:55 | ||
− | | This is our header file as '''iostream.''' | + | | This is our '''header file''' as '''iostream.''' |
|- | |- | ||
Line 295: | Line 286: | ||
|- | |- | ||
| 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 315: | Line 306: | ||
|- | |- | ||
| 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 323: | Line 314: | ||
|- | |- | ||
| 04:29 | | 04:29 | ||
− | | In this we have defined a variable '''x | + | | 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 339: | Line 330: | ||
|- | |- | ||
| 04:47 | | 04:47 | ||
− | | | + | | The result is stored in variable''' x.''' |
|- | |- | ||
| 04:51 | | 04:51 | ||
− | | And here we print the difference. | + | | And here we '''print''' the difference. |
|- | |- | ||
| 04:54 | | 04:54 | ||
− | | This is our '''return''' | + | | This is our '''return statement''' . |
|- | |- | ||
| 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'''. 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 387: | Line 374: | ||
|- | |- | ||
| 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 399: | Line 386: | ||
|- | |- | ||
| 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 | ||
− | | ''' | + | |'''Constructor.''' eg. '''Addition''' |
|- | |- | ||
| 05:43 | | 05:43 | ||
− | | '''Parameterized Constructor.''' eg. '''Addition obj (3, 4);''' | + | |'''Parameterized Constructor.''' eg. '''Addition obj (3, 4);''' |
|- | |- | ||
| 05:48 | | 05:48 | ||
− | | ''' | + | |'''Destructor.''' 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'''. |
|- | |- | ||
| 06:01 | | 06:01 | ||
− | | And | + | | And create a function '''divide''' which divides two given numbers. |
|- | |- | ||
| 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 448: | Line 434: | ||
|- | |- | ||
| 06:16 | | 06:16 | ||
− | | The Spoken Tutorial Project Team | + | | The Spoken Tutorial Project Team: Conducts workshops using spoken tutorials. |
|- | |- | ||
Line 456: | Line 442: | ||
|- | |- | ||
| 06:25 | | 06:25 | ||
− | | For more details, please write to | + | | For more details, please write to: |
|- | |- | ||
Line 464: | Line 450: | ||
|- | |- | ||
| 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 472: | Line 458: | ||
|- | |- | ||
| 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. |
|} | |} |
Latest revision as of 12:52, 23 March 2017
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 Types of constructors |
00:12 | Destructors.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 Constructor. |
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 destructors 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 as 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 file name 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 | 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. 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 | Constructor. eg. Addition |
05:43 | Parameterized Constructor. eg. Addition obj (3, 4); |
05:48 | Destructor. 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. |