Constructors and Destructors in C++

Submitted by: 
Language: 

Constructors and Destructors in C++

In this tutorial you will learn:
1. What are Constructors?
2. What are Destructors?
3. To use constructors and destructor in programs
4. Basic C++ syntax

What are constructors?
Whenever an object of a certain class is made, the data member of the objects are be initialized to some value and this purpose is achieved by using special function called constructors. A member function that is called automatically when an object is created.Constructors assign the values at the time when the object is made, if we don’t use the constructors a garbage value is assigned to the data members which is not a good programming practice. There are three type of constructors:
• Implicit Constructor(default)
• User defined Constructor
• Copy Constructor
Implicit (no argument) constructor is automatically called if the constructor is not provided by the programmer. It is made by compiler itself and arbitrary values are assigned to the data members.
User defined constructor(also called explicit) has to be defined by the programmer. In C++ user defined Constructor is defined as
NameOfTheClass()// can be provided argument according to wish of the programmer
{
//values are assigned to the data members
}
It must be noted here that the constructors have no return type and the name of constructor is also the name same as the class.

Copy constructor basically copies the data members of one object into the corresponding data members of the other object and is used in main of the program as
NameOfClass object1(object2)
In this case the values which are already assigned to the data members of the object1(through implicit or explicit constructor) are assigned to the object2. Copy constructor is used to initialize an object with another (existing) object of the same type (class).
What are Destructors?
In C++ the objects are not automatically destroyed as in JAVA so for this purpose we use garbage collector which automatically destroys the objects and cleans up the system memory when the program is closed. Destructors can be called as follow:
Tilde(~) + nameOfClass()
e.g
~myClass()
{// some information
}
Destructors can be used to display some message when the object is destroyed. Destructor can never be called, it is called automatically by the compiler.
strong>Using Structures

Basic Step:
Open Dev C++ then File > new > source file and start writing the code below.

  1. #include<iostream>
  2. #include<conio.h>
  3. using namespace std;

These are the basic header files which are required for C++ programming.

  1. class Distance
  2. { private:
  3. int dist; //distance in x direction
  4.  
  5. public:
  6. Distance(int a) //explicit constructor
  7. {
  8. dist=a;
  9. }
  10.  
  11. int showDist()
  12. {
  13. return dist;
  14. }
  15. ~Distance()
  16. {
  17. cout<<"\n Object Destroyed\n";
  18. }
  19. };

In this program we made a class named Distance, this class has only one data member ‘distance’ , a constructor and a function ‘showDist’. As told before the constructor name is that of the name of class. In body of constructor we initialize the only data member ‘dist’.

  1. void main() {
  2. Distance d1(11); // ctor
  3. Distance d2(d1); // default copy ctor
  4. Distance d3=d1; //default copy ctor
  5. cout<<"\n d1 = "<<d1.showDist();
  6. cout<<"\n d2 = "<<d2.showDist();
  7. cout<<"\n d3 = "<<d3.showDist();
  8. cout<<"\n-------------------------Destructor--------------------------\n";
  9. getch();
  10. }

In the main of the program, the object named ’d1’ is initialized using explicit constructor which is present in class of the program. In the next lines the value of its data member of d1 are copied into two objects of the class distance ‘d2’ and ‘d2’ using default copy constructor which is demonstrated through two methods.
Destructor is called at the end of the program as shown in the output.
Execute > compile
then Execute > run
Output
output


Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3>
  • You may insert videos with [video:URL]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.