Advanced-C++/C2/Constructor-And-Destructor/English

From Script | Spoken-Tutorial
Revision as of 12:29, 6 May 2013 by Ashwini (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

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 about,

Constructors.

Destructors.

We will do this with the help of example.

Slide 3


To record this tutorial, I am using

Ubuntu Operating System version 11.04,

g++ Compiler version 4.6.1.

Slide 4 Let us start with the introduction to Constructors and Destructors.

A constructor is a member function.

It has the same name as the class name.

Constructors cannot return values.

Constructor is automatically called when an object is created.

Types of Constructors are:

Parameterized Constructors.

Copy 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.

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 look at an example of Constructors,

I have already typed the code on the editor,

So i will open it.

Point the cursor

constructor.cpp

I have saved the file with the name constructor.cpp

Let me explain the code now.

Highlight

#include <iostream>

This is our header file as iostream.
Highlight

using namespace std;

Here is the std namespace.
Highlight

class Addition

Here we have defined a class Addition.



Highlight

int a, b ;

a and b are the integer variables.
Highlight

public

Public is the access specifier.

It allows the data to be accessible by the other parts of our program.

Highlight

Addition(int, int)

Addition is the construtor here.

A constructor has the same name as the class name.

We have passed two arguements here.

Highlight

int add( )

Then we have defined a function add.



Highlight

return(a + b);

It returns sum of a and b.
Highlight

Addition :: Addition (int a, int b)

Here, we specify that function Addition is not a global function.

It is a member function of the class Addition.

Here we have passed two arguments.

int x and int y.

Highlight

a = x;

b = y;

Now the value of x will be stored in variable a.

And the value of y will be stored in variable b.

Highlight

int main()

This is our main function.
Highlight

Addition obj (3, 4);

obj is the object of class Addition.

Here we pass two values as 3 and 4.

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.

Highlight

cout << “Addition of a and b is ” <<obj.add() <<“\n”

Then we print the result.

Here we call the function add through the object obj.

Highlight

return 0;

This is our return statement.
Click on save Now click on Save.
Let us execute the program.
Open the terminal

Ctrl, Alt and T keys simultaneously

Open the terminal by pressing Ctrl, Alt and T keys simultaneously.
Type

g++ constructor.cpp -o cons

To execute

Type

./cons

To compile the program type,

g++ constructor.cpp -o cons

To execute

Type

./cons

Highlight

Output

Here 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

Go to File menu

Click on Save as option

If a constructor is not declared in the class then the compiler assumes a default constructor for the class.
This is our header file as iostream.
Here is the std namespace.
Type:

Addition::~Addition();

Here type Addition::~Addition()

Opening curly bracket {

then type cout << "Memory Deallocation\n";

and closing surly bracket }

On the terminal Let us execute.

Come back to the terminal.

Type

g++ destructor.cpp -o dest

Type

./dest

Compile and execute as before.

g++ destructor.cpp -o dest,

and press Enter.

To execute,

Type,

./dest

Highlight

output

Here the output is displayed as,

Addition of a and b is 7.

Memory Deallocation.

This brings us to the end of this tutorial.

Slide 6 Let us summarize,

In this tutorial we learned,

Constructors

eg. Addition

Destructors

eg. ~Addition

Slide 7 As an assignment,

Write a program to calculate the area of a triangle using constructor.

Slide 8

About the Spoken Tutorial Project

Watch the video avail able at the link shown .

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: http://spoken-tutorial.org\NMEICT-Intro.

This is Ashwini Patil from IIT Bombay signing off

Thank You for joining.

Contributors and Content Editors

Ashwini