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

From Script | Spoken-Tutorial
Jump to: navigation, search
 
(One intermediate revision by the same user not shown)
Line 20: Line 20:
  
  
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| In this tutorial we will learn about,
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| In this tutorial we will learn,
  
'''Constructors.'''
+
'''Constructors'''
  
 
'''Types of constructors'''
 
'''Types of constructors'''
Line 28: Line 28:
 
'''Destructors.'''
 
'''Destructors.'''
  
We will do this with the help of example.
+
We will do this with the help of some examples.
  
 
|-
 
|-
Line 43: Line 43:
 
|-
 
|-
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Slide 4
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Slide 4
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us start with the introduction to '''Constructors and Destructors.'''
+
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us start with an introduction to '''Constructors. '''
  
 
A '''constructor''' is a member function.
 
A '''constructor''' is a member function.
Line 51: Line 51:
 
'''Constructors''' cannot return values.
 
'''Constructors''' cannot return values.
  
'''Constructor''' is automatically called when an object is created.
+
It is automatically called when an object is created.
  
 
Types of Constructors are:
 
Types of Constructors are:
Line 58: Line 58:
  
 
'''Copy Constructors.'''
 
'''Copy Constructors.'''
 +
 +
And
  
 
'''Default Constructors.'''
 
'''Default Constructors.'''
 
We will discuss about them in another tutorial.No details about destructors. Pls change the opening statement or provide an additional slide for destructors.
 
  
 
|-
 
|-
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"|  
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"|  
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us move on to''' Destructors'''
+
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"|  
 +
Let us move on to destructors.
  
 
'''Destructors''' are used to deallocate memory.
 
'''Destructors''' are used to deallocate memory.
Line 75: Line 76:
 
|-
 
|-
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"|  
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"|  
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us look at an example of '''Constructors''',
+
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us see an example on '''Constructors and Destructors''',
  
 
I have already typed the code on the editor,
 
I have already typed the code on the editor,
  
So i will open it.
+
I will open it.
  
 
|-
 
|-
Line 85: Line 86:
  
 
'''constructor.cpp'''
 
'''constructor.cpp'''
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| I have saved the file with the name '''constructor.cpp'''
+
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Note that our filename is '''cons-dest.cpp'''
 +
 
 +
In this program we will perform the addition of two numbers using constructor.
  
 
Let me explain the code now.
 
Let me explain the code now.
Line 93: Line 96:
  
 
'''<nowiki>#include <iostream></nowiki>'''
 
'''<nowiki>#include <iostream></nowiki>'''
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| This is our '''header file as iostream.'''
+
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| This is our '''header file as iostream'''
  
 
|-
 
|-
Line 99: Line 102:
  
 
'''using namespace std;'''
 
'''using namespace std;'''
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Here is the '''std''' namespace.
+
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Here we have used '''std''' namespace.
  
 
|-
 
|-
Line 105: Line 108:
  
 
'''class Addition'''
 
'''class Addition'''
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Here we have defined a class '''Addition.'''
+
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Then we have class '''Addition.'''
 
+
 
+
  
  
Line 115: Line 116:
 
'''int a, b ;'''
 
'''int a, b ;'''
 
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| '''a''' and '''b''' are the integer variables.
 
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| '''a''' and '''b''' are the integer variables.
 +
 +
These are the private members of class Addition.
  
 
|-
 
|-
Line 120: Line 123:
  
 
'''public'''
 
'''public'''
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| '''Public''' is the '''access specifier.'''
+
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Here we have the''' Public''' '''specifier.'''
  
It allows the data to be accessible by the other parts of our program.
 
  
 
|-
 
|-
Line 128: Line 130:
  
 
'''Addition(int, int)'''
 
'''Addition(int, int)'''
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| '''Addition''' is the construtor here.
+
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Then we have the construtor '''Addition'''
  
A '''constructor''' has the same name as the class name.
+
The '''constructor''' has the same name as the class name.
  
 
We have passed two arguements here.
 
We have passed two arguements here.
Line 138: Line 140:
  
 
'''int add( )'''
 
'''int add( )'''
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Then we have defined a function '''add.'''
+
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now we have defined a Destructor'''.'''
 
+
  
 +
For this we use a '''tilde''' sign followed by the destructors name.
  
  
Line 147: Line 149:
  
 
'''return(a + b);'''
 
'''return(a + b);'''
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| It returns sum of a and b.  
+
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| This is a public function add.
 +
 
 +
It returns sum of a and b.  
  
 
|-
 
|-
Line 153: Line 157:
  
 
'''Addition :: Addition (int a, int b)'''
 
'''Addition :: Addition (int a, int b)'''
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Here, we specify that function '''Addition''' is not a global function.
+
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Here we access the constructor using the scope resolution operator.
  
It is a member function of the class '''Addition.'''
+
a and b are private members.
  
Here we have passed two arguments.
+
To access the private members we use x and y.
 
+
'''int x''' and c-c++-constructors-&-destructors-c3-eng.odt'''int y.'''
+
 
+
|-
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Highlight
+
  
'''a = x;'''
+
Then we access the destructor.
  
'''b = y;'''
+
In this we print Memory Deallocation.
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now the value of '''x''' will be stored in variable '''a'''.
+
  
And the value of '''y''' will be stored in variable '''b'''.
 
  
 
|-
 
|-
Line 181: Line 178:
  
 
'''Addition obj (3, 4);'''
 
'''Addition obj (3, 4);'''
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| '''obj''' is the object of class '''Addition'''.  
+
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| '''Here we create an object obj''' of class '''Addition'''.  
  
Here we pass two values as '''3''' and '''4'''.
+
Then we pass two arguments as '''3''' and '''4'''.
  
 
'''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'''.
  
Which means a's value will be 3 and b's vaue will be 4.
+
This means '''a's''' value is '''3 '''and '''b's''' value is '''4.'''
 +
 
 +
The constructor that has arguments is called parameterized constructor.
 +
 
 +
Hence this one is the prameterized constructor.
  
 
|-
 
|-
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Highlight
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Highlight
  
'''<nowiki>cout << “Addition of a and b is ” <<obj.add() <<“\n”</nowiki>'''
+
'''<nowiki>cout << “Sum is ” <<obj.add() <<“\n”</nowiki>'''
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Then we print the result.  
+
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Here we call the function '''add '''using''' '''the object''' obj'''.
  
Here we call the function '''add '''through''' '''the object''' obj'''.
+
And we print the sum.  
  
 
|-
 
|-
Line 202: Line 203:
 
'''return 0;'''
 
'''return 0;'''
 
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| This is our '''return''' statement.
 
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| This is our '''return''' statement.
 
|-
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Click on save
 
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now click on '''Save'''.
 
  
 
|-
 
|-
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"|  
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"|  
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us execute the program.
+
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now let us execute the program.
  
 
|-
 
|-
Line 215: Line 212:
  
 
'''Ctrl, Alt and T''' keys simultaneously
 
'''Ctrl, Alt and T''' keys simultaneously
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Open the terminal by pressing '''Ctrl, Alt and T''' keys simultaneously.
+
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Open the terminal window by pressing '''Ctrl, Alt and T''' keys simultaneously on your keyboard.
  
 
|-
 
|-
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Type  
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Type  
  
'''g++ constructor.cpp -o cons'''
+
'''g++ const-dest.cpp -o cons'''
  
 
To execute
 
To execute
Line 227: Line 224:
  
 
'''./cons'''
 
'''./cons'''
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| To compile the program type,
+
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| To compile type,
  
'''g++ constructor.cpp -o cons'''
+
'''g++ space cons-dest.cpp space -o space cons'''
 +
 
 +
'''Press Enter'''
  
 
To execute
 
To execute
Line 236: Line 235:
  
 
'''./cons'''
 
'''./cons'''
 +
 +
'''Press Enter'''
  
 
|-
 
|-
Line 241: Line 242:
  
 
'''Output'''
 
'''Output'''
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Here the output is displayed as  
+
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| The output is displayed as  
  
 
'''Sum is 7.'''
 
'''Sum is 7.'''
  
'''And'''
+
'''And Memory Deallocation'''
 
+
'''Memory Deallocation'''
+
  
 
|-
 
|-
Line 275: Line 274:
 
|-
 
|-
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"|  
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"|  
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Here we have used std namespace.
+
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Here we have used '''std namespace.'''
  
 
|-
 
|-
Line 289: Line 288:
 
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Then we have '''class Subtraction.'''
 
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Then we have '''class Subtraction.'''
  
Then we a and b declared as''' public.'''
+
Then we have a and b declared as''' public.'''
  
 
|-
 
|-
Line 313: Line 312:
  
 
Here we access the default constructor using the''' scope resolution operator.'''
 
Here we access the default constructor using the''' scope resolution operator.'''
 
|-
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"|
 
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"|
 
  
 
|-
 
|-
Line 356: Line 351:
 
|-
 
|-
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| '''On the terminal'''
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| '''On the terminal'''
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us execute the program.
+
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now let us execute the program.
  
 
Come back to our terminal.
 
Come back to our terminal.
Line 368: Line 363:
  
 
'''./def'''
 
'''./def'''
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Type
+
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us compile.
 +
Type
  
'''g++ default.cpp -o def'''
+
'''g++ space default.cpp space -o space def'''
 +
 
 +
'''Press Enter'''
  
 
Type
 
Type
  
 
'''./def'''
 
'''./def'''
 +
 +
'''Press Enter'''
  
 
|-
 
|-
Line 385: Line 385:
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"|  
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| On the editor
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Come back to our program.
+
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"|  
 +
Come back to our program.
  
 
Here you can see that we have passed the arguments within the function.
 
Here you can see that we have passed the arguments within the function.
  
|-
+
And in our previous example we have passed the arguments within the '''Object.'''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"|
+
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| And in our previous program we have passed the arguments within the object
+
 
+
|-
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"|
+
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| And here we have passed the arguments using the object.
+
 
+
  
 +
And here we have passed the arguments using the '''Object.'''
  
 
+
Now let us go back to our slides.
|-
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"|
+
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now let us go back to our program.
+
  
 
|-
 
|-
Line 421: Line 413:
 
'''Destructors'''
 
'''Destructors'''
  
'''eg. '''~Addition
+
eg. ~Addition
  
 
'''Default Constructor.'''
 
'''Default Constructor.'''
  
 
eg. Subtraction
 
eg. Subtraction
 +
  
 
|-
 
|-
Line 433: Line 426:
 
Create a class named Division.
 
Create a class named Division.
  
Create a constructor for the class
+
Create a constructor for the class.
 +
 
 +
And
  
Create a function divide which divides two given numbers.
+
Create a function '''divide''' which divides two given numbers.
  
 
|-
 
|-
Line 441: Line 436:
  
 
About the Spoken Tutorial Project
 
About the Spoken Tutorial Project
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Watch the video avail able at the link shown .
+
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Watch the video available at the link shown below
  
 
It summarises the Spoken Tutorial project.
 
It summarises the Spoken Tutorial project.
Line 473: Line 468:
 
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.
  
More information on this Mission is available at: http://spoken-tutorial.org\NMEICT-Intro.
+
More information on this Mission is available at the link shown below: http://spoken-tutorial.org\NMEICT-Intro.
  
 
|-
 
|-

Latest revision as of 13:29, 3 February 2014

Title of script: Constructors and Destructors

Author: Ashwini R. Patil

Keywords: Constructors, Destructors, Video tutorial


Visual Cue
Narration
Slide 1 Welcome to the spoken tutorial on Constructors and Destructors in C++.
Slide 2


In this tutorial we will learn,

Constructors

Types of constructors

Destructors.

We will do this with the help of some examples.

Slide 3


To record this tutorial, I am using

Ubuntu Operating System version 11.10,

g++ Compiler version 4.6.1.

Slide 4 Let us start with an introduction to Constructors.

A constructor is a member function.

It has the same name as the class name.

Constructors cannot return values.

It is automatically called when an object is created.

Types of Constructors are:

Parameterized Constructors.

Copy Constructors.

And

Default Constructors.

Let us move on to destructors.

Destructors are used to deallocate memory.

They are called when an object is destroyed.

A destructor takes no arguments and has no return types.

Let us see an example on Constructors and Destructors,

I have already typed the code on the editor,

I will open it.

Point the cursor

constructor.cpp

Note that our filename is cons-dest.cpp

In this program we will perform the addition of two numbers using constructor.

Let me explain the code now.

Highlight

#include <iostream>

This is our header file as iostream
Highlight

using namespace std;

Here we have used std namespace.
Highlight

class Addition

Then we have class Addition.


Highlight

int a, b ;

a and b are the integer variables.

These are the private members of class Addition.

Highlight

public

Here we have the Public specifier.


Highlight

Addition(int, int)

Then we have the construtor Addition

The constructor has the same name as the class name.

We have passed two arguements here.

Highlight

int add( )

Now we have defined a Destructor.

For this we use a tilde sign followed by the destructors name.


Highlight

return(a + b);

This is a public function add.

It returns sum of a and b.

Highlight

Addition :: Addition (int a, int b)

Here we access the constructor using the scope resolution operator.

a and b are private members.

To access the private members we use x and y.

Then we access the destructor.

In this we print Memory Deallocation.


Highlight

int main()

This is our main function.
Highlight

Addition obj (3, 4);

Here we create an object obj of class Addition.

Then we pass two arguments as 3 and 4.

3 will be stored in x and 4 will be stored in y.

This means a's value is 3 and b's value is 4.

The constructor that has arguments is called parameterized constructor.

Hence this one is the prameterized constructor.

Highlight

cout << “Sum is ” <<obj.add() <<“\n”

Here we call the function add using the object obj.

And we print the sum.

Highlight

return 0;

This is our return statement.
Now let us execute the program.
Open the terminal

Ctrl, Alt and T keys simultaneously

Open the terminal window by pressing Ctrl, Alt and T keys simultaneously on your keyboard.
Type

g++ const-dest.cpp -o cons

To execute

Type

./cons

To compile type,

g++ space cons-dest.cpp space -o space cons

Press Enter

To execute

Type

./cons

Press Enter

Highlight

Output

The output is displayed as

Sum is 7.

And Memory Deallocation

On the editor Now let us see an example on Default constructors.

Come back ot our program.

I have already typed the code.

Note that our filename is default.cpp

Default Constructor Note that our filename is default.cpp.

If a constructor is not declared in the class,

Then the compiler assumes a default constructor for the class.

Let me explain the code.

This is our header file as iostream.

Here we have used std namespace.
Point cursor to filename


Highlight

class Subtraction


Then we have class Subtraction.

Then we have a and b declared as public.

Highlight

int sub(int a, int b)

return (a-b);

Here we have sub function.

We have passed two arguments int a and int b.

This returns the subtraction of two numbers a and b.

Highlight

Subtraction ();


This is the defalut constructor.

Here we access the default constructor using the scope resolution operator.

Highlight

int main()

int x;

This is our main function.

In this we have defined a variable x.

Highlight

Subtraction s;

Here we create an object s of class subtraction.
Highlight

x=s.sub(8,4);

Then we call the function sub using the object s.

And pass two arguments as 8 and 4.

Then the result is stored in variable x.

Highlight

cout <<"Difference is "<<x<<"\n";

return 0;

And here we print the difference.

This is our return statement.

On the terminal Now let us execute the program.

Come back to our terminal.

Type

g++ default.cpp -o def

Type

./def

Let us compile.

Type

g++ space default.cpp space -o space def

Press Enter

Type

./def

Press Enter

Highlight

Output

The output is return as

Difference is 4

On the editor

Come back to our program.

Here you can see that we have passed the arguments within the function.

And in our previous example we have passed the arguments within the Object.

And here we have passed the arguments using the Object.

Now let us go back to our slides.

Slide 6 Let us summarize,

In this tutorial we learned,

Constructors.

eg. Addition

Parameterized Constructor.

eg. Addition obj (3, 4);

Destructors

eg. ~Addition

Default Constructor.

eg. Subtraction


Slide 7 As an assignment,

Create a class named Division.

Create a constructor for the class.

And

Create a function divide which divides two given numbers.

Slide 8

About the Spoken Tutorial Project

Watch the video available at the link shown below

It summarises the Spoken Tutorial project.

If you do not have good bandwidth, you can download and watch it.

Slide 9

Spoken Tutorial Workshops


The Spoken Tutorial Project Team,

Conducts workshops using spoken tutorials .

Gives certificates to those who pass an online test .

For more details, please write to,

contact@spoken-tutorial.org

Slide Number 10


Acknowledgement

Spoken Tutorial Project is a part of the Talk to a Teacher project.

It is supported by the National Mission on Education through ICT, MHRD, Government of India.

More information on this Mission is available at the link shown below: http://spoken-tutorial.org\NMEICT-Intro.

This is Ashwini Patil from IIT Bombay signing off

Thank You for joining.

Contributors and Content Editors

Ashwini