Difference between revisions of "Advanced-C++/C2/Constructor-And-Destructor/English-timed"

From Script | Spoken-Tutorial
Jump to: navigation, search
 
(4 intermediate revisions by 2 users not shown)
Line 14: Line 14:
 
|-
 
|-
 
| 00:09
 
| 00:09
|* '''Constructors'''
+
|'''Constructors''' Types of '''constructors'''
 
+
|-
+
| 00:10
+
|* Types of '''constructors'''
+
  
 
|-
 
|-
 
| 00:12
 
| 00:12
|* '''Destructors.'''
+
|'''Destructors.'''We will do this with the help of some examples.
 
+
|-
+
| 00:13
+
| We will do this with the help of some examples.
+
  
 
|-
 
|-
Line 34: Line 26:
 
|-
 
|-
 
| 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 '''Constructors. '''
+
| 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 66: Line 58:
 
|-
 
|-
 
| 00:46
 
| 00:46
|* '''Parameterized Constructors.'''
+
|'''Parameterized Constructors'''
  
 
|-
 
|-
 
| 00:49
 
| 00:49
|* '''Copy Constructors.'''and
+
|'''Copy Constructors''' and
  
 
|-
 
|-
 
| 00:50
 
| 00:50
|* '''Default Constructors.'''
+
|'''Default Constructors.'''
  
 
|-
 
|-
Line 114: Line 106:
 
|-
 
|-
 
| 01:27
 
| 01:27
| This is our '''header file''' as '''iostream'''
+
| This is our '''header file''' as '''iostream'''.
  
 
|-
 
|-
Line 122: Line 114:
 
|-
 
|-
 
| 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'''.
  
 
|-
 
|-
Line 142: Line 134:
 
|-
 
|-
 
| 01:52
 
| 01:52
| We have passed two '''arguement'''s here.
+
| We have passed two '''arguements''' here.
  
 
|-
 
|-
Line 150: Line 142:
 
|-
 
|-
 
| 01:57
 
| 01:57
| For this we use a '''tilde''' sign followed by the '''destructor's''' name.
+
| For this we use a '''tilde''' sign followed by the '''destructors''' name.
  
 
|-
 
|-
Line 158: Line 150:
 
|-
 
|-
 
| 02:05
 
| 02:05
| It returns sum of 'a' and 'b'.  
+
| It returns sum of '''a''' and '''b'''.  
  
 
|-
 
|-
Line 166: Line 158:
 
|-
 
|-
 
| 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 194: Line 186:
 
|-
 
|-
 
| 02:36
 
| 02:36
| '''3''' will be stored in 'x' and '''4''' will be stored in 'y'.
+
| '''3''' will be stored in '''x''' and '''4''' will be stored in '''y'''.
  
 
|-
 
|-
 
| 02:40
 
| 02:40
| This means 'a's value is '''3 '''and 'b's value is '''4.'''
+
| This means '''a'''s value is '''3 '''and 'b's value is '''4.'''
  
 
|-
 
|-
Line 226: Line 218:
 
|-
 
|-
 
| 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.
  
 
|-
 
|-
Line 266: Line 258:
 
|-
 
|-
 
| 03:41
 
| 03:41
| Note that our filename is '''default dot cpp'''
+
| 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 '''
  
 
|-
 
|-
Line 294: Line 286:
 
|-
 
|-
 
| 04:04
 
| 04:04
| Then we have 'a' and 'b' declared as''' public.'''
+
| Then we have '''a''' and '''b''' declared as''' public.'''
  
 
|-
 
|-
Line 306: Line 298:
 
|-
 
|-
 
| 04:15
 
| 04:15
| This returns the subtraction of two numbers 'a' and 'b.'
+
| This returns the subtraction of two numbers '''a''' and '''b.'''
  
 
|-
 
|-
Line 314: 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 322: Line 314:
 
|-
 
|-
 
| 04:29
 
| 04:29
| In this we have defined a variable 'x.'
+
| In this, we have defined a variable '''x'''.
  
 
|-
 
|-
Line 338: Line 330:
 
|-
 
|-
 
| 04:47
 
| 04:47
| Then the result is stored in variable' x.'
+
| The result is stored in variable''' x.'''
  
 
|-
 
|-
Line 346: Line 338:
 
|-
 
|-
 
| 04:54
 
| 04:54
| This is our '''return''' statement.
+
| This is our '''return statement''' .
  
 
|-
 
|-
Line 362: Line 354:
 
|-
 
|-
 
| 05:09
 
| 05:09
| '''Press Enter'''.
+
| '''Press Enter'''. Type: '''dot slash def'''
 
+
|-
+
| 05:10
+
| Type: '''dot slash def'''
+
  
 
|-
 
|-
Line 398: Line 386:
 
|-
 
|-
 
| 05:34
 
| 05:34
| Now let us go back to our '''slide'''s.
+
| Now, let us go back to our '''slide'''s.
  
 
|-
 
|-
Line 406: Line 394:
 
|-
 
|-
 
| 05:41
 
| 05:41
|* '''Constructors.''' eg. '''Addition'''
+
|'''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
|* '''Destructors.''' eg.''' ~Addition'''
+
|'''Destructor.''' eg.''' ~Addition'''
  
 
|-
 
|-
 
| 05:52
 
| 05:52
|* '''Default Constructor.''' eg. '''Subtraction'''
+
|'''Default Constructor.''' eg. '''Subtraction'''
  
 
|-
 
|-
Line 430: Line 418:
 
|-
 
|-
 
| 06:01
 
| 06:01
| And Create a function '''divide''' which divides two given numbers.
+
| And create a function '''divide''' which divides two given numbers.
  
 
|-
 
|-
Line 446: Line 434:
 
|-
 
|-
 
| 06:16
 
| 06:16
| The Spoken Tutorial Project Team, Conducts workshops using spoken tutorials. .
+
| The Spoken Tutorial Project Team: Conducts workshops using spoken tutorials.  
  
 
|-
 
|-

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.

Contributors and Content Editors

Gaurav, PoojaMoolya, Pratik kamble, Sandhya.np14