Advanced-C++/C2/Constructor-And-Destructor/English-timed
From Script | Spoken-Tutorial
Revision as of 10:37, 27 June 2015 by Sandhya.np14 (Talk | contribs)
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. |