C++ Constructors And Destructors

C++ constructors are special member functions that initialize objects when it is created. In other words, as soon as the object starts living the constructor assigns initial values to the objects. There is another special function called destructor, which does opposite of what constructor does – destroy objects.

Syntax for Declaring a Constructor

  1. A constructor should have same name as that of class.
  2. A constructor has not return type, not even void.
  3. Mostly it declared as Public or Protected, rarely it’s declared as Private.
  4. Constructor are not declared with special keywords such as volatile, const, static, virtual.
class A { 
  int m; 
public: A () {
  m = 0 ;
}  
// Constructor that initialize m to 0.
};

The constructor above has class name A, so the constructor name is also A. It is declared under public visibility. It has no return value. There are many types of constructors which you will learn in this article. The following is a list of constructors used in C++.

  1. Default Constructor.
  2. Parameterized Constructor.
  3. Constructor with Default Arguments.
  4. Copy Constructor.
  5. Dynamic Constructor.

Default Constructor

There is no need to call a constructor like member functions. It gets invoked the moment you create an object. The compiler have this constructor and it is called the default constructor. It does not take any kind of values.

You can create a constructor that takes no arguments and it will be called a default constructor.

Example of a Default Constructor

The ‘Do-Nothing’ constructor is an example of default constructor.

class dummy { 
   int m; 
public: 
   dummy () { 
      } 
};

In the above example, constructor dummy does nothing, however, it is called a default constructor. When you create an object of class dummy.

dummy obj; // object created and constructor will be invoked.

The default constructor gets invoked automatically. This is known as automatic initialization of objects.

Parameterized Constructor

The default constructor set the default values which is not required all the time. A user-defined value is desired sometimes, in that case, you can use a parameterized constructor. A parameterized constructor will accept certain arguments and assign those to the private member of the class.

Syntax for Parameterized Constructor

The syntax for parameterized constructor is given below.

class dummy { 
   int m, n; 
  public: 
   dummy(int x, int y){ 
      m = x; 
      n = y); 
  // parameterized constructor 
 }; 
  //During object creation dummy 
     obj(10, 223);
  // shortcut way called implicit 
     declaration 
     dummy obj = dummy(33, 445); 
  // explicit declaration
Constructor with Parameters
Constructor with Parameters

In the above example, when the object of class dummy is created, and supplied with values 10 and 243 for constructor. The value is assigned to x and y parameter of the constructor and later assigned to private members of class – m and n.

Constructors with Default Values

Parameterized constructors are great way to initialize with user-defined values, now in certain parameterized constructors, you can set a default parameter value.

For example,




class A { 
   int m, n; 
public: 
   A (int x, int y = 100) { 
      m = x; 
      n = y; 
   } 
};

While creating an object for class A, you can enter only one argument instead of two.

A obj (234); // ignore the second value because it is y = 100

Suppose you want to override the default value, C++ constructors, allow you to do that also.

A obj (234, 587); // Override default value y = 100, now y = 587
Constructor with Default Values
Constructor with Default Values

Copy Constructor

Constructors can accept all kinds of parameters except objects of their own class. But there is a way to pass objects of same class as parameter, instead of passing the object itself, pass a reference to the object.

The syntax for copy constructor is as follows.


class Set { 
   int x; 
  public: 
   Set (Set) { 
      // this declaration is wrong, cannot pass an object directly. 
    }
 };

Now, consider the same example with modification.

class Set { 
  int x; 
public: 
  Set (int y) { 
  // constructor with parameters 
  int x = y;
 } 
  Set (&Set) { 
  // this declaration is wrong, cannot pass an object directly. 
  } 
};

By using the reference operator, a reference to the object is passed. This type of constructor is called a copy constructor.

For example,

Set A (100);
Set B (A); 
// will invoke the copy constructor and initialize B with values of A. Set C; 
C = A; 
// In this all values of A is assigned to object C, but not using copy constructor.

Dynamic Constructors

You can allocate memory to objects while creating them using constructors. This saves memory if you allocate object memory dynamically. The new keyword allocates memory and delete keyword free the memory when the object is destroyed.

For example, let demo be a class, we are going to create an object of demo dynamically. You need a pointer of type demo to this task.

 demo *ptr = new demo; 

The pointer (*ptr) has memory equal to any demo object. You can get values of data member using (*ptr) easily.

 (*ptr).x = 100;

 Where (*ptr) is just like any demo class object and x is a data member of class demo.

Destructors

The destructors do exactly opposite of what constructor do, they destroy the objects and free memory after object is terminated. Some well-known properties of destructors are

  1. They have same class name, similar to constructors.
  2. But, before the name it has a tilde (~).
  3. Never take argument or return anything.
  4. Clear objects and return free memory for future use.

References

  • Balagurusamy. 2008. Object Oriented Programming With C++. Tata McGraw-Hill Education.
  • Kanetkar, Yashavant. 20 November 2002. Let us C. Bpb Publications

Please support us by disabling your adblocker or whitelist this site from your adblocker. Thanks!

turn of adblocker imag