Class Constructors and destructors in C++
In this C++ tutorial you will learn about Class Constructors and destructors in C++ viz., Constructors, What is the use of Constructor, General Syntax of Constructor, Destructors, What is the use of Destructors and General Syntax of Destructors.
What is the use of Constructor
The main use of constructors is to initialize objects. The function of initialization is automatically carried out by the use of a special member function called a constructor.
General Syntax of Constructor
A constructor is a special member function that takes the same name as the class name. The syntax generally is as given below:
The default constructor for a class X has the formX::X()
In the above example, the arguments are optional.
The constructor is automatically named when an object is created. A constructor is named whenever an object is defined or dynamically allocated using the “new” operator.
There are several forms in which a constructor can take its shape namely:
This constructor has no arguments in it. The default Constructor is also called as the no argument constructor.
Exforsys :: Exforsys()
This constructor takes one argument, also called one argument constructor. The main use of copy constructor is to initialize the objects while in creation, also used to copy an object. The copy constructor allows the programmer to create a new object from an existing one by initialization.
For example to invoke a copy constructor the programmer writes:
Both the above formats can be sued to invoke a copy constructor.
using namespace std;
cout << " Example of Copy Constructor";
In the above the copy constructor takes one argument an object of type Exforsys which is passed by reference. The output of the above program is
Some important points about constructors:
- A constructor takes the same name as the class name.
- The programmer cannot declare a constructor as virtual or static, nor can the programmer declare a constructor as const, volatile, or const volatile.
- No return type is specified for a constructor.
- The constructor must be defined in the public. The constructor must be a public member.
- Overloading of constructors is possible. This will be explained in later sections of this tutorial.
What is the use of Destructors
Destructors are also special member functions used in C++ programming language. Destructors have the opposite function of a constructor. The main use of destructors is to release dynamic allocated memory. Destructors are used to free memory, release resources and to perform other clean up. Destructors are automatically named when an object is destroyed. Like constructors, destructors also take the same name as that of the class name.
General Syntax of Destructors
The above is the general syntax of a destructor. In the above, the symbol tilda ~ represents a destructor which precedes the name of the class.
Some important points about destructors:
- Destructors take the same name as the class name.
- Like the constructor, the destructor must also be defined in the public. The destructor must be a public member.
- The Destructor does not take any argument which means that destructors cannot be overloaded.
- No return type is specified for destructors.