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

From Script | Spoken-Tutorial
Jump to: navigation, search
 
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 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. '''
+
| 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 59: Line 59:
 
'''Copy Constructors.'''
 
'''Copy Constructors.'''
  
'''Default Constructors.'''No details about destructors. Pls change the opening statement or provide an additional slide for destructors.
+
And
 +
 
 +
'''Default Constructors.'''
  
 
|-
 
|-
 
| 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;"| '''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 73: 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 see an example of '''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 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,
Line 106: Line 109:
 
'''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;"| Then we have 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 123: Line 124:
 
'''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;"| Here we have the''' Public''' '''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.'''
 
 
  
  
Line 144: Line 143:
  
 
For this we use a '''tilde''' sign followed by the destructors name.
 
For this we use a '''tilde''' sign followed by the destructors name.
 
 
  
  
Line 169: Line 166:
  
 
In this we print Memory Deallocation.
 
In this we print Memory Deallocation.
 
 
  
  
Line 183: 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;"| '''Here we create object obj''' 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'''.  
  
Then 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'''.
  
This 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.
 
The constructor that has arguments is called parameterized constructor.
  
Hence this one is a prameterized constructor
+
Hence this one is the prameterized constructor.
  
 
|-
 
|-
Line 217: 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 on your keyboard.
+
| 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.
  
 
|-
 
|-
Line 229: 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++ cons-dest.cpp -o cons'''
+
'''g++ space cons-dest.cpp space -o space cons'''
 +
 
 +
'''Press Enter'''
  
 
To execute
 
To execute
Line 238: Line 235:
  
 
'''./cons'''
 
'''./cons'''
 +
 +
'''Press Enter'''
  
 
|-
 
|-
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 364: 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 379: Line 383:
  
 
'''Difference is 4'''
 
'''Difference is 4'''
 +
 +
|-
 +
| 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.
 +
 +
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.
  
 
|-
 
|-
Line 389: Line 406:
  
 
eg. Addition
 
eg. Addition
 
'''Default Constructor.'''
 
 
eg. Subtraction
 
  
 
'''Parameterized Constructor.'''
 
'''Parameterized Constructor.'''
Line 401: Line 414:
  
 
eg. ~Addition
 
eg. ~Addition
 +
 +
'''Default Constructor.'''
 +
 +
eg. Subtraction
 +
  
 
|-
 
|-
Line 410: Line 428:
 
Create a constructor for the class.
 
Create a constructor for the class.
  
Create a function divide which divides two given numbers.
+
And
 +
 
 +
Create a function '''divide''' which divides two given numbers.
  
 
|-
 
|-
Line 416: 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 available 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 448: 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