Advanced-C++/C2/Constructor-And-Destructor/English
Title of script: Constructors and Destructors
Author: Ashwini R. Patil
Keywords: Constructors, Destructors, Video tutorial
|
|
---|---|
SPreferable to have the title of the slide like Introduction/prerequisite etc...lide 1 | Welcome to the spoken tutorial on Constructors and Destructors in C++. |
Slide 2
|
In this tutorial we will learn about,
Constructors. Types of 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.10, 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. |
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 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 c-c++-constructors-&-destructors-c3-eng.odtint 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 |
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 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 | Let us execute the program.
Come back to our terminal. |
Type
g++ default.cpp -o def Type ./def |
Type
g++ default.cpp -o def Type ./def |
Highlight
Output |
The output is return as
Difference is 4 |
Come back to our program.
Here you can see that we have passed the arguments within the function. | |
And in our previous program 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 program. | |
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 Create a function divide which divides two given numbers. |
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
|
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. |