Is a special member function whose name is same as class name and preceded by?

While programming, the objects which hold certain data members and member functions have to be initialized before operating on them. This is usually done by member functions that initialize data members to predefined values. But there is a special member function in which the object initializes itself automatically when it is first constructed. This special member function is called a constructor.

Substantially, a constructor defines a value to data members of the class. A constructor is a special member function that is used to initialize objects of a class instantaneously when it is constructed.

Using and Declaring a Constructor

  • A constructor is a member function that has the same name as that of the class.
  • It is defined as any other member functions (both inside or outside) of the class.
  • Since a constructor just defines the value to a data member, there’s no return type to it.
  • They are called automatically when the objects are created.
  • They should be declared in the public section of the class.
  • Declaration Syntax: Consider the class name and constructor name to be demo. Let a and b be two integer variables.

class demo
	{
		private:
			int a,b;
		public:
			demo()
				{
				a=1;
				b=2;
				}
	};

C++ program to demonstrate the use of a constructor

Consider the program to find the area of a circle. The name of the class will be demo. The two member functions will be declared, one for input and one for output. Since the name of the constructor should be the same as that of the class, the name of the constructor will also be demo. When the constructor demo is declared, the initialization of the class objects is done automatically.

#include 
#include
using namespace std;

class demo
{
  private:
     double radius, pi;
   public:
     void input(double r);
     double output();
     demo();
};

demo::demo(void)    //constructor definition outside the class
{
   pi=3.142;
}
void demo::input(double r)
{
  radius=r;
}
double demo::output(void)
{
  return (pi*radius*radius);
}

int main()
{
  demo d1;

  d1.input(5.5);
  cout<<"Area of the circle is : "<<d1.output()<<"sq.units"<<"\n";
  return 0;
  getch();
}

The output of the above program is:

Area of the circle is : 95.0455sq.units

The above program can also be written by defining the constructor inside the class. The code segment for defining the constructor inside the class is as follows:

class demo
{
   private:
      double radius, pi;
    public:
      void input(double r);
      double output();
      demo()        //constructor definition inside the class
            {
                pi=3.142;
            }
};

Types of Constructors

Default Constructor

A constructor which does not have any arguments is called a Default Constructor, or a ‘Zero Argument Constructor’. In a default constructor, every object in the class is initialized to the same set of values. It is not possible to initialize different objects with different initial values. This is one of the disadvantages of a default constructor.

Syntax: Consider a class with the name world. The default constructor for this class would be:

world :: world()   //default constructor without any arguments

Note: Any constructor with arguments is not a default constructor.

Parameterized constructor

To avoid the infeasibility of default constructor to accept arguments, we use Parameterized Constructor, which is a constructor that can accept one or more arguments. It works the same way as a default constructor, but the difference is that it can hold arguments. They are also called automatically once the objects are created. Another attribute of parameterized constructors is that they can be overloaded.

Syntax: Consider a class with the name world. The parameterized constructor for this class would be:

world :: world(int a, int b)     //parameterized constructor with arguments

Copy Constructor

Copy Constructor is a type of parameterized constructor in which the properties of one object can be copied to another object. It is used to initialize an object with the values of already existing objects. A copy constructor is invoked when an existing object is passed as a parameter.

Note: Copy constructors cannot be invoked explicitly.

Syntax: Consider a class with the name world. The copy constructor for this class would be:

world :: world(world &ptr)        //copy constructor

Where,

#include 
#include
using namespace std;

class demo
{
  private:
     double radius, pi;
   public:
     void input(double r);
     double output();
     demo();
};

demo::demo(void)    //constructor definition outside the class
{
   pi=3.142;
}
void demo::input(double r)
{
  radius=r;
}
double demo::output(void)
{
  return (pi*radius*radius);
}

int main()
{
  demo d1;

  d1.input(5.5);
  cout<<"Area of the circle is : "<<d1.output()<<"sq.units"<<"\n";
  return 0;
  getch();
}
6 is the pointer to the class object.

Invoking (Calling) Constructors

Explicit Call

Explicit call is a method of invoking constructors in which the declaration of an object is done by using assignment operator(=), constructor name followed by argument list.

Syntax:

constructor_name object_name=constructor_name(argument list);
C++ program to demonstrate the use of explicit call

Consider the program to calculate the area of the circle. The name of the class and constructor will be demo. The constructor will be parameterized by taking one argument. Here, d1 acts as the object which transfers the value 5.5 explicitly to the constructor.

#include 
#include
using namespace std;

class demo
{
	private:
		double radius, area;
	public:
		demo(double r)
	{
	radius=r;
	}
	void output()
	{
	area=3.142*radius*radius;
	cout<<"The area of the circle is: "<<area<<"sq.units"<<"\n";
	}
};

int main()
{
	demo d1=demo(5.5);
	d1.output();
	return 0;
	getch();
}

The output of the above program will be:

The area of the circle is: 95.0455sq.units

Implicit Call

Implicit call is a method of invoking constructors in which the declaration of an object is done and is then followed by an argument list.

Syntax:

#include 
#include
using namespace std;

class demo
{
  private:
     double radius, pi;
   public:
     void input(double r);
     double output();
     demo();
};

demo::demo(void)    //constructor definition outside the class
{
   pi=3.142;
}
void demo::input(double r)
{
  radius=r;
}
double demo::output(void)
{
  return (pi*radius*radius);
}

int main()
{
  demo d1;

  d1.input(5.5);
  cout<<"Area of the circle is : "<<d1.output()<<"sq.units"<<"\n";
  return 0;
  getch();
}
0
C++ program to demonstrate the use of implicit call

Consider the program to calculate the area of the circle. The name of the class and constructor will be demo. The constructor will be parameterized by taking one argument. Here, d1 acts as the object which transfers the value 5.5 implicitly to the constructor.

#include 
#include
using namespace std;

class demo
{
  private:
     double radius, pi;
   public:
     void input(double r);
     double output();
     demo();
};

demo::demo(void)    //constructor definition outside the class
{
   pi=3.142;
}
void demo::input(double r)
{
  radius=r;
}
double demo::output(void)
{
  return (pi*radius*radius);
}

int main()
{
  demo d1;

  d1.input(5.5);
  cout<<"Area of the circle is : "<<d1.output()<<"sq.units"<<"\n";
  return 0;
  getch();
}
1

The output of the above program will be:

The area of the circle is: 95.0455sq.units

Note: For different values, different objects have to be initialized in the main function.

Constructor Overloading

We know that constructors are used for initializing data members of the class. They can also be used for initializing specific input values to the data members. But this cannot be done by using default constructors, since they do not take any arguments. So, for this to happen the default constructors have to be overloaded, i.e. they should be initialized with additional arguments. The object initialization has to be done by keeping the number of arguments in mind. The compiler then decides which constructor to invoke depending on the argument list.

Look at this simple segment. Consider the class name and constructor name to be demo.

#include 
#include
using namespace std;

class demo
{
  private:
     double radius, pi;
   public:
     void input(double r);
     double output();
     demo();
};

demo::demo(void)    //constructor definition outside the class
{
   pi=3.142;
}
void demo::input(double r)
{
  radius=r;
}
double demo::output(void)
{
  return (pi*radius*radius);
}

int main()
{
  demo d1;

  d1.input(5.5);
  cout<<"Area of the circle is : "<<d1.output()<<"sq.units"<<"\n";
  return 0;
  getch();
}
3

Consider d1 and d2 to be the objects. When the object d1 is created, default constructor is invoked, and when the object d2 is created with some arguments, parameterized constructor is invoked.

#include 
#include
using namespace std;

class demo
{
  private:
     double radius, pi;
   public:
     void input(double r);
     double output();
     demo();
};

demo::demo(void)    //constructor definition outside the class
{
   pi=3.142;
}
void demo::input(double r)
{
  radius=r;
}
double demo::output(void)
{
  return (pi*radius*radius);
}

int main()
{
  demo d1;

  d1.input(5.5);
  cout<<"Area of the circle is : "<<d1.output()<<"sq.units"<<"\n";
  return 0;
  getch();
}
4

Destructors

As mentioned in the introduction, constructor is a special member function which is instantaneously called whenever an object is created. Likewise, a destructor is a type of function which is instantaneously called whenever an object is destroyed. By doing so, it deallocates the values initialized to a variable and also its memory. They have no return value. They do not take any arguments and so they cannot be overloaded. It has the same name as the class (like a constructor), but is preceded by tilde mark (symbolized as ~). Look at the following code snippet to see how a destructor works.

Is a special member function whose name is same as class name and preceded by tilde?

Destructor is also a special member function like constructor. Destructor destroys the class objects created by constructor. Destructor has the same name as their class name preceded by a tilde (~) symbol.

Which of the following member function with the same name as the class name that is used to initialize the object variable by passing parameter values?

Constructor is a special method that is invoked automatically at the time of object creation. It is used to initialize the data members of new objects generally. Constructor in C++ has the same name as class or structure. Hence the correct answer is Constructor.

Is a special method that has the same name as the class and is invoked automatically whenever an object of the class is instantiated?

Whenever an object is created, the constructor is called automatically. A constructor is like an instance method that usually has the same name as the class, and can be used to set the values of the members of an object, either to default or to user-defined values.

Are special member functions whose task is to initialize the objects of its class?

Constructors: A constructor is a special member function of a class whose purpose is usually to initialize the members of an object.