Constructor in C++
0 3651
As we do earlier, when there is any need to initialize the value in the class, we create and call a function. We can also initialize the value with the help of constructor which is known as the special member function because its name is identical with the class name and we can do coding similar like the normal function. But the constructors have no return type.
Syntax
class coding
{
public:
coding() // constructor invoked
{
..... // code
}
The constructor can be defined both inside and outside like normal member functions with the help of scope resolution operator.
Following example illustrate defining constructor outside the class.
class coding
{
int a;
public:
coding();
};
coding::coding() // constructor definition
{
a=2;
}
Objective of Constructor
The main objective of the constructor is to initialize the value to the data member as the object is created.
Constructors in C++ have some features that are given below:
* The constructor is a special member function.
* We cannot Specify return type for the constructor.
* A constructor always returns an object of the same class. This concept will clear the doubt of the student that if the constructor is also a function then why it does not return value.
* We can have as many constructors as we want in the class but they must differ in their signature.
Category of Constructor
The constructor can be divided into three categories i.e.
1. Default Constructor
2. Parameterized Constructor
3. Copy Constructor
Default Constructor
The constructor that does not contain any argument or parameter is known as Default constructor.
Following example illustrating Default constructor
// Cpp program illustrating Constructors
#include <iostream>
using namespace std;
class construct1
{
public:
int g, h;
// Default Constructor
construct1()
{
g = 15;
h = 35;
}
};
int main()
{
// As the object is created
// Default constructor is automatically called
construct1 j;
cout << "g: "<< j.g << endl << "h: "<< j.h;
return 1;
}
Output
g: 15
h: 35
When we create an object, the Default constructor is automatically created which initialize all data members to garbage values. That means that whenever we create an object of the class, data members are initialized to some garbage value.
When we do not want it to have garbage values or if we want while creating objects, certain values to the data members then there is a need to write our own constructors that will be further discussed in detail in the parameterized constructor.
Parameterised Constructor
When there is a need to initialize data members to some values, we can build parameterized constructor. We can have as many constructors as we want in the class but they must differ in their signature. Name of the parameterized constructor is also identical to the class name but there is a requirement of parameters as shown below in the example
For e.g.
construct1(int g, int h) // parameterized constructor
{
g = 15;
h = 35;
}
cpp program explaining parameterized constructor
#include<iostream>
using namespace std;
class coding
{
private:
int g, h;
public:
// Parameterized Constructor
coding(int g1, int h1)
{
g = g1;
h = h1;
}
int getG()
{
return g;
}
int getH()
{
return h;
}
};
int main()
{
// Constructor called
coding c1(15, 20);
// Access values assigned by constructor
cout << "c1.g = " << c1.getG() << ", c1.h = " << c1.getH();
return 0;
}
Output
c1.g = 15, c1.h = 20
Copy Constructor
Whenever there is a requirement of creating a copy of the existing object, the copy constructor is preferred. They can accept a single argument of reference to the same class type.
#include<iostream>
using namespace std;
class coding
{
private:
int g, h; //data members
public:
coding(int g1, int h1)
{
g = g1;
h = h1;
}
/* Copy constructor */
coding(const coding &add)
{
g = add.g;
h = add.h;
}
void display()
{
cout<<g<<" "<<h<<endl;
}
};
int main()
{
coding j1(11, 13); // Normal constructor
coding j2 = j1; // Copy constructor
cout<<"Normal constructor : ";
j1.display();
cout<<"Copy constructor : ";
j2.display();
return 0;
}
Output
Normal constructor : 11 13
Copy constructor : 11 13
Constructor Overloading
The concept of function overloading shows similarity with the copy constructor. In copy constructor more than one constructor is used that's why it is known as constructor overloading.
A Simple C++ program illustrating constructor overloading
#include <iostream>
using namespace std;
class built
{
public:
float rate;
// Constructor with no parameters
built()
{
rate = 0;
}
// Constructor with two parameters
built(int g, int h)
{
rate = g * h;
}
void disp()
{
cout<< rate<< endl;
}
};
int main()
{
// Constructor Overloading
built j;
built j2( 15, 25);
j.disp();
j2.disp();
return 1;
}
Output
0
375
Share:
Comments
Waiting for your comments