Advanced-C++/C2/Constructor-And-Destructor/English
Title of script: Constructors and Destructors
Author: Ashwini R. Patil
Keywords: Constructors, Destructors, Video tutorial
|
|
---|---|
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
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
|
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. |