Javatpoint Logo
Javatpoint Logo

Constructor overloading in C++

Constructor is a member function of a class that is used to initialize the objects of the class. Constructors do not have any return type and are automatically called when the object is created.

Characteristics of constructors

  • The name of the constructor is the same as the class name
  • No return type is there for constructors
  • Called automatically when the object is created
  • Always placed in the public scope of the class
  • If a constructor is not created, the default constructor is created automatically and initializes the data member as zero
  • Declaration of constructor name is case-sensitive
  • A constructor is not implicitly inherited

Types of constructors

There are three types of constructors -

  • Default constructor - A default constructor is one that does not have function parameters. It is used to initialize data members with a value. The default constructor is called when the object is created.

Code

Output

a:10
b:20 
  • Parameterized constructor - A non-parameterized constructor does have the constructor arguments and the value passed in the argument is initialized to its data members. Parameterized constructors are used in constructor overloading.

Code

Output

p1.x = 10, p1.y = 15   

Explanation

Create a class point with two data members x and y. A parameterized constructor is created with the points x1 and y1 as parameters and the value of x and y are assigned using x1 and y1. In the main function, we create a parameterized constructor with the values (10, 15). Using the getter functions, we get the values of the data members.

  • Copy constructor - Copy constructor initializes an object using another object of the same class.

Syntax

class_name(constclassname&old_object).

Code

Output

p1.x = 10, p1.y = 15
p2.x = 10, p2.y = 15 

Constructor overloading in C++

As there is a concept of function overloading, similarly constructor overloading is applied. When we overload a constructor more than a purpose it is called constructor overloading.

The declaration is the same as the class name but as they are constructors, there is no return type.

The criteria to overload a constructor is to differ the number of arguments or the type of arguments.

Code

Output

Person1 Age = 20
Person2 Age = 45    

Explanation

In the above program, we have created a class Person with one data member(age). There are two constructors in the class that are overloaded. We have overloaded the second constructor by providing one argument and making it parameterized.

So, in the main function when the object person1 is created, it calls the non-parameterized constructor and when person2 is created, it calls the parameterized constructor and performs the required operation of initializing the age. Hence, when object person1 age is printed it gives 20 that was set by default and person2 age gives 45 as it was passed by the object to the parameterized constructor.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA