Difference between revisions of "Advanced-C++/C2/Constructor-And-Destructor/English"
Line 28: | Line 28: | ||
'''Destructors.''' | '''Destructors.''' | ||
− | We will do this with the help of | + | We will do this with the help of some examples. |
|- | |- | ||
Line 43: | Line 43: | ||
|- | |- | ||
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Slide 4 | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Slide 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;"| Let us start with the introduction to '''Constructors | + | | 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 start with the introduction to '''Constructors. ''' |
A '''constructor''' is a member function. | A '''constructor''' is a member function. | ||
Line 59: | Line 59: | ||
'''Copy Constructors.''' | '''Copy Constructors.''' | ||
− | '''Default Constructors.''' | + | '''Default Constructors.'''No details about destructors. Pls change the opening statement or provide an additional slide for destructors. |
− | + | ||
− | + | ||
|- | |- | ||
| 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;"| | + | | 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''' |
'''Destructors''' are used to deallocate memory. | '''Destructors''' are used to deallocate memory. | ||
Line 75: | Line 73: | ||
|- | |- | ||
| 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;"| Let us | + | | 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 see an example of '''Constructors and Destructors''', |
I have already typed the code on the editor, | I have already typed the code on the editor, | ||
− | + | I will open it. | |
|- | |- | ||
Line 85: | Line 83: | ||
'''constructor.cpp''' | '''constructor.cpp''' | ||
− | | 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="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 '''cons-dest.cpp''' |
+ | |||
+ | In this program we will perform the addition of two numbers using constructor. | ||
Let me explain the code now. | Let me explain the code now. | ||
Line 93: | Line 93: | ||
'''<nowiki>#include <iostream></nowiki>''' | '''<nowiki>#include <iostream></nowiki>''' | ||
− | | 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 '''header file as iostream | + | | 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 '''header file as iostream''' |
|- | |- | ||
Line 99: | Line 99: | ||
'''using namespace std;''' | '''using namespace std;''' | ||
− | | 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. |
|- | |- | ||
Line 105: | Line 105: | ||
'''class Addition''' | '''class Addition''' | ||
− | | 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="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 '''Addition.''' |
Line 115: | Line 115: | ||
'''int a, b ;''' | '''int 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;"| '''a''' and '''b''' are the integer variables. | | 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;"| '''a''' and '''b''' are the integer variables. | ||
+ | |||
+ | These are the private members of class Addition. | ||
|- | |- | ||
Line 120: | Line 122: | ||
'''public''' | '''public''' | ||
− | | 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;"| '''Public''' | + | | 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 the''' Public''' '''specifier.''' |
+ | |||
+ | |||
− | |||
|- | |- | ||
Line 128: | Line 131: | ||
'''Addition(int, int)''' | '''Addition(int, int)''' | ||
− | | 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;"| '''Addition''' | + | | 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 the construtor '''Addition''' |
− | + | The '''constructor''' has the same name as the class name. | |
We have passed two arguements here. | We have passed two arguements here. | ||
Line 138: | Line 141: | ||
'''int add( )''' | '''int add( )''' | ||
− | | 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="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 we have defined a Destructor'''.''' |
+ | |||
+ | For this we use a '''tilde''' sign followed by the destructors name. | ||
Line 147: | Line 152: | ||
'''return(a + 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;"| It returns sum of a and 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;"| This is a public function add. |
+ | |||
+ | It returns sum of a and b. | ||
|- | |- | ||
Line 153: | Line 160: | ||
'''Addition :: Addition (int a, int b)''' | '''Addition :: Addition (int a, int 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 | + | | 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 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. | |
− | + | ||
− | |||
− | |||
− | |||
− | |||
|- | |- | ||
Line 181: | Line 183: | ||
'''Addition obj (3, 4);''' | '''Addition obj (3, 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;"| '''obj''' | + | | 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 object obj''' of class '''Addition'''. |
− | + | Then we pass two values as '''3''' and '''4'''. | |
'''3''' will be stored in '''x''' and '''4''' will be stored in '''y'''. | '''3''' will be stored in '''x''' and '''4''' will be stored in '''y'''. | ||
− | + | This means '''a's''' value will be '''3 '''and '''b's''' vaue will be '''4.''' | |
+ | |||
+ | The constructor that has arguments is called parameterized constructor. | ||
+ | |||
+ | Hence this one is a prameterized constructor | ||
|- | |- | ||
| 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 | ||
− | '''<nowiki>cout << | + | '''<nowiki>cout << “Sum is ” <<obj.add() <<“\n”</nowiki>''' |
− | | 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="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 call the function '''add '''using''' '''the object''' obj'''. |
− | + | And we print the sum. | |
|- | |- | ||
Line 202: | Line 208: | ||
'''return 0;''' | '''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;"| This is our '''return''' statement. | | 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 '''return''' statement. | ||
− | |||
− | |||
− | |||
− | |||
|- | |- | ||
| 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;"| | + | | 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 execute the program. |
|- | |- | ||
Line 215: | Line 217: | ||
'''Ctrl, Alt and T''' keys simultaneously | '''Ctrl, Alt and T''' keys simultaneously | ||
− | | 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;"| Open the terminal by pressing '''Ctrl, Alt and T''' keys simultaneously. | + | | 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;"| Open the terminal by pressing '''Ctrl, Alt and T''' keys simultaneously on your keyboard. |
|- | |- | ||
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Type | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Type | ||
− | '''g++ | + | '''g++ const-dest.cpp -o cons''' |
To execute | To execute | ||
Line 229: | Line 231: | ||
| 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 compile the program type, | | 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 compile the program type, | ||
− | '''g++ | + | '''g++ cons-dest.cpp -o cons''' |
To execute | To execute | ||
Line 241: | Line 243: | ||
'''Output''' | '''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;"| | + | | 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 displayed as |
'''Sum is 7.''' | '''Sum is 7.''' | ||
− | '''And | + | '''And Memory Deallocation''' |
− | + | ||
− | + | ||
|- | |- | ||
Line 275: | Line 275: | ||
|- | |- | ||
| 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 we have used std namespace. | + | | 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.''' |
|- | |- | ||
Line 313: | Line 313: | ||
Here we access the default constructor using the''' scope resolution operator.''' | Here we access the default constructor using the''' scope resolution operator.''' | ||
− | |||
− | |||
− | |||
− | |||
|- | |- | ||
Line 356: | Line 352: | ||
|- | |- | ||
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| '''On the terminal''' | | 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;"| | + | | 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 execute the program. |
Come back to our terminal. | Come back to our terminal. | ||
Line 383: | Line 379: | ||
'''Difference is 4''' | '''Difference is 4''' | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
|- | |- | ||
Line 414: | Line 389: | ||
eg. Addition | eg. Addition | ||
+ | |||
+ | '''Default Constructor.''' | ||
+ | |||
+ | eg. Subtraction | ||
'''Parameterized Constructor.''' | '''Parameterized Constructor.''' | ||
Line 421: | Line 400: | ||
'''Destructors''' | '''Destructors''' | ||
− | + | eg. ~Addition | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
|- | |- | ||
Line 433: | Line 408: | ||
Create a class named Division. | Create a class named Division. | ||
− | Create a constructor for the class | + | Create a constructor for the class. |
Create a function divide which divides two given numbers. | Create a function divide which divides two given numbers. | ||
Line 441: | Line 416: | ||
About the Spoken Tutorial Project | About the Spoken Tutorial Project | ||
− | | 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;"| Watch the video | + | | 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;"| Watch the video available at the link shown. |
It summarises the Spoken Tutorial project. | It summarises the Spoken Tutorial project. |
Revision as of 13:18, 27 September 2013
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 about,
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 the introduction to Constructors.
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.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 see an example of 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 object obj of class Addition.
Then we pass two values as 3 and 4. 3 will be stored in x and 4 will be stored in y. This means a's value will be 3 and b's vaue will be 4. The constructor that has arguments is called parameterized constructor. Hence this one is a 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 by pressing Ctrl, Alt and T keys simultaneously on your keyboard. |
Type
g++ const-dest.cpp -o cons To execute Type ./cons |
To compile the program type,
g++ cons-dest.cpp -o cons To execute Type ./cons |
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 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 |
Type
g++ default.cpp -o def Type ./def |
Highlight
Output |
The output is return as
Difference is 4 |
Slide 6 | Let us summarize,
In this tutorial we learned, Constructors. eg. Addition Default Constructor. eg. Subtraction Parameterized Constructor. eg. Addition obj (3, 4); Destructors eg. ~Addition |
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 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
|
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. |