C-and-C++/C2/Scope-Of-Variables/English

From Script | Spoken-Tutorial
Revision as of 12:05, 3 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,

Constructors.

Destructors.

We will do this with the help of example.

We will also see some common errors and their solutions.

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.

Let us see an example on Constructors,

I have already typed the code on the editor,

So i will open it

Point the cursor

constructor.c

I have saved the file with the name constructor.c

Let me explain the code now.

Highlight

#include <iostream>

This is our header file.
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

It is the access specifier.

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

Highlight

Rectangle(int, int)

Addition is the construtor here.

A constructor has the same name as the class name.

We passed two arguements here.

Highlight

int area( )

Then we have defined a function add.



Highlight

return(a + b);

It returns sum of a and b.
Highlight

Rectangle :: Rectangle (int a, int b)

This is a scope operator.

We must use this operator.

It specifies 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 a and 4 will be stored in b.

Which means 3 will be the width and 4 will be the height.

Highlight

cout << “Area of the rectangle is ” <<rect.area() <<“\n”

Then we print the result.

Here we call the function area.

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

Please open the terminal by pressing Ctrl, Alt and T keys simultaneously
Type

g++ constructor.c -o cons

To execute

Type

./cons

To compile the program type,

g++ constructor.c -o cons

To execute

Type

./cons

Highlight

Output

Here the output is displayed as

Area of rectangle is 12.

On the editor NOW LET US SEE HOW THE DESTRUCTOR WORKS.

Come back to our program

Go to File menu

Click on Save as option

Type

.cpp

Now click on File menu.

click on Save as option.

Save the file with the name destructor.cpp.

Now let me change a few things here
Delete

<stdio.h>

Type

<iostream>

First we will change the header file as

iostream.

Type

using namespace std;

We will include the using statement here.
The function declaration in C++ is same as in C,

So no need to change anything here.

Type

cout <<

Now replace the printf statement with cout statement

As we use cout<< function to print a line in C++.

Delete

%d\n

We don't need the format specifier and '\n' here,

So delete it.

Type

<<

Now type two opening angle brackets here

Then type sum two opening angle brackets again,

and within the double quotes backslash n.

Delete the closing bracket.

On the terminal Let us compile.

Come back to the terminal.

Type

g++ function.cpp -o fun1

Type

./fun1

Type,

g++ function.cpp -o fun1,

and press Enter.

To execute,

Type,

./fun1

Highlight

output

Here the output is displayed as,

Sum of a and b is 9.

Errors


Type

int a = 2;

Now we will see some common errors which we can come across.

Suppose here I will declare variable a again,

Type

int a = 2;

On the terminal Click on save.

Come back to the terminal.

Highlight


Error

Let us compile,

We see an error that declaration of int a shadows a parameter.

a is a global variable.

It has a global scope.

We cannot declare the variable twice as it is already declared globally

We can only declare variable a as a local variable now.

On the editor Come back to the program.

Let us fix the error.

Delete

int a = 2;

Remove this declaration.
Click on save Click on save.
On the terminal Let us execute.

Come back to our terminal.

Let us compile and execute as before.
Yes it is working.

Now come back to our slides.

Slide 7 As an assignment,

Write a program to print the difference of two numbers.

Slide 8

About the Spoken Tutorial Project

Watch the video available 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