Difference between revisions of "Advanced-C++/C2/Constructor-And-Destructor/English"
(Created page with ''''Title of script''': Constructors and Destructors '''Author: '''Ashwini R. Patil '''Keywords: Constructors, Destructors, Video tutorial''' {| style="border-spacing:0;" ! <…') |
|||
Line 12: | Line 12: | ||
|- | |- | ||
− | | 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;"| SPreferable to have the title of the slide like Introduction/prerequisite etc...lide 1 |
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Welcome to the spoken tutorial on Constructors and Destructors in C++. | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Welcome to the spoken tutorial on Constructors and Destructors in C++. | ||
Line 23: | Line 23: | ||
'''Constructors.''' | '''Constructors.''' | ||
+ | |||
+ | '''Types of constructors''' | ||
'''Destructors.''' | '''Destructors.''' | ||
Line 35: | Line 37: | ||
| 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 record this tutorial, I am using | | 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 record this tutorial, I am using | ||
− | '''Ubuntu Operating System''' version 11. | + | '''Ubuntu Operating System''' version 11.10, |
'''g++''' '''Compiler''' version 4.6.1. | '''g++''' '''Compiler''' version 4.6.1. | ||
Line 63: | Line 65: | ||
|- | |- | ||
| 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 157: | Line 159: | ||
Here we have passed two arguments. | Here we have passed two arguments. | ||
− | '''int x''' and '''int y.''' | + | '''int x''' and c-c++-constructors-&-destructors-c3-eng.odt'''int y.''' |
|- | |- | ||
Line 258: | Line 260: | ||
|- | |- | ||
− | | 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;"| '''Default Constructor''' |
+ | | 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;"| 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. | ||
|- | |- | ||
| 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;"| This is our | + | | 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 me explain the code. |
+ | |||
+ | This is our header file as iostream. | ||
|- | |- | ||
| 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;"| Here | + | | 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 used std namespace. |
|- | |- | ||
− | | 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;"| '''Point cursor to filename''' |
+ | |||
+ | |||
+ | Highlight | ||
+ | |||
+ | '''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.''' | |
− | and | + | Then we a and b declared as''' public.''' |
|- | |- | ||
− | | 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;"| Highlight |
− | + | ||
− | + | '''int sub(int a, int b) ''' | |
+ | |||
+ | '''return (a-b);''' | ||
+ | | 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 '''sub''' function. | ||
+ | |||
+ | We have passed two '''arguments int a '''and''' int b.''' | ||
+ | |||
+ | This returns the subtraction of two numbers '''a''' and '''b.''' | ||
|- | |- | ||
− | | 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;"| Highlight |
− | ''' | + | '''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;"| This is the '''defalut constructor.''' |
− | + | Here we access the default constructor using the''' scope resolution operator.''' | |
− | + | |- | |
+ | | 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;"| | ||
− | + | |- | |
+ | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Highlight | ||
− | '''. | + | '''int main()''' |
+ | |||
+ | '''int x;''' | ||
+ | | 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;"| This is our '''main''' function. | ||
+ | |||
+ | In this we have defined a variable '''x.''' | ||
|- | |- | ||
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Highlight | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Highlight | ||
− | ''' | + | '''Subtraction s;''' |
− | | 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 | + | | 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''' '''s of class subtraction.''' |
− | ''' | + | |- |
+ | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Highlight | ||
+ | |||
+ | '''x=s.sub(8,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;"| Then we call the function '''sub '''using the''' object s.''' | ||
− | ''' | + | And pass two''' arguments '''as''' 8 '''and''' 4.''' |
− | This | + | Then the result is stored in variable''' x.''' |
+ | |||
+ | |- | ||
+ | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Highlight | ||
+ | |||
+ | '''<nowiki>cout <<"Difference is "<<x<<"\n";</nowiki>''' | ||
+ | |||
+ | '''return 0;''' | ||
+ | | 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;"| And here we print the difference. | ||
+ | |||
+ | This is our return statement. | ||
+ | |||
+ | |- | ||
+ | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| '''On the terminal''' | ||
+ | | 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 execute the program. | ||
+ | |||
+ | Come back to our terminal. | ||
+ | |||
+ | |- | ||
+ | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Type | ||
+ | |||
+ | '''g++ default.cpp -o def''' | ||
+ | |||
+ | Type | ||
+ | |||
+ | '''./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 | ||
+ | |||
+ | '''g++ default.cpp -o def''' | ||
+ | |||
+ | Type | ||
+ | |||
+ | '''./def''' | ||
+ | |||
+ | |- | ||
+ | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Highlight | ||
+ | |||
+ | '''Output''' | ||
+ | | 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;"| The output is return as | ||
+ | |||
+ | '''Difference is 4''' | ||
+ | |||
+ | |- | ||
+ | | 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;"| Come back to our program. | ||
+ | |||
+ | Here you can see that we have passed the arguments within the function. | ||
+ | |||
+ | |- | ||
+ | | 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;"| And in our previous program we have passed the arguments within the object | ||
+ | |||
+ | |- | ||
+ | | 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;"| And here we have passed the arguments using the object. | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |- | ||
+ | | 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;"| Now let us go back to our program. | ||
|- | |- | ||
Line 327: | Line 411: | ||
In this tutorial we learned, | In this tutorial we learned, | ||
− | '''Constructors''' | + | '''Constructors.''' |
+ | |||
+ | eg. Addition | ||
+ | |||
+ | '''Parameterized Constructor.''' | ||
− | eg. | + | eg. Addition obj (3, 4); |
'''Destructors''' | '''Destructors''' | ||
− | eg. '''~Addition''' | + | '''eg. '''~Addition |
+ | |||
+ | '''Default Constructor.''' | ||
+ | |||
+ | eg. Subtraction | ||
|- | |- | ||
Line 339: | Line 431: | ||
| 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;"| As an assignment, | | 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;"| As an assignment, | ||
− | + | Create a class named Division. | |
+ | |||
+ | Create a constructor for the class | ||
+ | |||
+ | Create a function divide which divides two given numbers. | ||
|- | |- |
Revision as of 13:24, 29 July 2013
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. |