Constructor Overloading in C++
A Constructor is a class member function that is used to initialize the class's objects. Constructors have no return type and are called automatically when an object is formed.
Constructor Characteristics
- Constructors don't have a return type.
- When the object is created, it is immediately called. It is always placed in the class's public scope.
- If no constructor is specified, the default constructor is used, which sets the value of the data member to zero.
- The name of the function Object ( ) { [native code] } is case-sensitive.
- A function Object ( ) { [native code] } is not inherited by default.
Types of Constructor
Default Constructor: It's used to assign a value to data members. When an object is created, the default function Object () { [native code] } is invoked.
Example:
#include < iostream >
#include < bits/tsdc++.h >
#include < stdlib >
using namespace std ;
class construct // create a class construct
{
public:
int x , y ; // initialise two data members
construct ( ) // this is how default constructor is created
{
// We can also assign both values to zero
x = 10 ; // intialise x with some value
y = 20 ; // initialise y with some value
}
} ;
int main ( )
{
construct z ; // creating an object of construct calls defualt constructor
cout << "x:" << z.x << endl ;
<< "y:" << z.y ; // print x and y
return 1 ;
}
OUTPUT:
X: 10
Y:20
……..
Process executed in 0.11 seconds
Press any key to continue.
Explanation:
In the program above in C ++, we created a builder class and announced the default builder. In the main work we started the builder thing by z.
Parameterized Constructor is non-parameterized function Object() { [native code] } has function Object() { [native code] } arguments, and the value supplied in the argument is initialised to its data members. Parameterized constructors are utilised in function Object() { [native code] } overloading.
Example:
#include < iostream >
#include < bits/stdc++.h >
#include < stdlib >
using namespace std ;
class Point // create point class
{
private :
int a , b ; // the two data members of class Point
public :
Point ( int a1 , int b1 ) // create paramterised Constructor and initialise data member
{
a = a1 ; // a1 is now intialised to a
b = b1 ; // a1 is now intialised to b
}
intgetA ( )
{
return a ; // to get the value of a
}
intgetB ( )
{
return b ; // to get the value of b
}
} ;
int main ( )
{
Point pt1 ( 10 , 15 ) ; // created object for paramterised constructor
cout << "pt1.a = " << pt1.getA ( ) << " , pt1.b = " << pt1.getB ( ) ; // print a and b
return 0 ;
}
OUTPUT:
Pt1.a = 10 ,pt1.b = 15
……………………………..
Process executed in 0.11 seconds
Press any key to continue.
Explanation:
Make a class point out of the two data elements a and b. A parameterized constructor is formed using the parameters a1 and b1, and the values of a and b are assigned using a1 and b1. We build a parameterized constructor using the values in the main function (10, 15). The values of the data members are obtained using the getter functions.
Constructor Overloading
Similarly to the idea of function overloading, constructor overloading is used. Constructor overloading occurs when we overload a function Object with more than one purpose. The conditions for overloading a function Object are to change the number or type of parameters.
Example:
// C++ program to demonstrate constructor overloading
#include <iostream>
using namespace std;
class People { // create people class
private:
int Age; // data member
public:
// 1. Constructor with no arguments
People ()
{
Age = 10; // when object is created the age will be 20
}
// 2. Constructor with an argument
People (int a)
{ // when parameterised Constructor is called with a value the
// age passed will be initialised
Age = a;
}
intgetage()
{ // getter to return the age
return Age;
}
};
int main()
{
People person1 , person2 ( 20 ) ; // called the object of people class in differnt way
cout<< "Person1 Age = " << person1.getage() <<endl;
cout<< "Person2 Age = " << person2.getage() <<endl;
return 0;
}
OUTPUT:
Person1 Age = 10
Person2 Age = 20
………………………..
Process executed in 0.11 seconds
Press any key to continue.
Explanation:
In the preceding program, we established a People class with one data member (Age). We've overloaded the second by passing it one argument and parameterzing it. As a result, when the object person1 age is printed, it returns the default value of 20, however person2 age returns 45 since it was supplied by the object to the parameterized constructor.