Types Of Constructor In C++
Types Of Constructor In C++
Types Of Constructor In C++ :
Constructors :
The constructor is a special type of member function that initialises an object automatically when it is created.
The Compiler identifies a given member function is a constructor by its name and the return type.
Types Of Constructor In C++ |
Constructor's the same name as the class and it does not have any return type.
And Also, the constructor is always public.
class A
{
private:
int x;
float y;
public:
A(): x(5), y(5.5)
{
// Body of constructor
}
... .. ...
};
int main()
{
A t1;
... .. ...
}
In Above program we can see that a constructor is defined without a return type and the same name as the class.
May You Like To Read This Related Articles:
What Is Operating System? Knowledge Of OS!!
Operating System As A User Interface And As A Resource Manager - Technopedia!
Services Of Operating System -
Technopedia!!!
Basic Concepts Of OOPC!!
CONCEPT OF DBMS!!
Types Of Operating System - Technopedia!!!
Learn How constructor works?
In the above code, A() is a constructor.
We can also initialise the data members inside the constructor's body as below.
A()
{
x = 5;
y = 5.5;
}
Learn Use of Constructor in C++
Let's Suppose you are working on 50's of Person objects and the default value of a data member age is 0. Initialising all objects manually will be a very tough task.
Instead of this you can define a constructor that initialises age to 0.
And Then, all you have to do is create a Person object and the constructor will automatically initialise the age.
Example :
Write code for Calculate the area of a rectangle and display it.#include <iostream>
using namespace std;
class A
{
private:
int length;
int breadth;
public:
// Constructor
A(): length(5), breadth(2){ }
void GetLength()
{
cout << "Enter length and breadth respectively: ";
cin >> length >> breadth;
}
int AreaCalculation() { return (length * breadth); }
void DisplayArea(int temp)
{
cout << "Area: " << temp;
}
};
int main()
{
A A1, A2;
int temp;
A1.GetLength();
temp = A1.AreaCalculation();
A1.DisplayArea(temp);
cout << endl << "Default Area when value is not taken from user" << endl;
temp = A2.AreaCalculation();
A2.DisplayArea(temp);
return 0;
}
In above program, class Area is created to handle area related functionalities. It has two data members length and breadth.
The constructor is defined which initialises length to 5 and breadth to 2.
And We also have three additional member functions GetLength(), AreaCalculation() and DisplayArea() to get length from the user, calculate the area and display the area respectively.
When the objects A1 and A2 are created, the length and breadth of both objects are initialized to 5 and 2 respectively, because of the constructor.
And the member function GetLength() is invoked which takes the value of length and breadth from the user for object A1. This changes the length and breadth of the object A1.
Then, the area for the object A1 is calculated and stored in variable temp by calling AreaCalculation() function and finally, it is displayed.
For object A2, no data is asked from the user. So, the length and breadth remains 5 and 2 respectively.
Then, the area for A2 is calculated and displayed which is 10.
Output :
Enter length and breadth respectively: 67
Area: 42
Default Area when value is not taken from user
Area: 10
Constructor Overloading
Constructor can be overloaded in a similar way as function overloading.
Overloaded constructors have the same name (name of the class) but different number of arguments.
Depending upon the number and type of arguments passed, specific constructor is called.
Since, there are multiple constructors present, argument to the constructor should also be passed while creating an object.
Example 2: Constructor overloading
Write the Code to demonstrate the working of overloaded constructors
#include <iostream>
using namespace std;
class Area
{
private:
int length;
int breadth;
public:
// Constructor with no arguments
Area(): length(5), breadth(2) { }
// Constructor with two arguments
Area(int l, int b): length(l), breadth(b){ }
void GetLength()
{
cout << "Enter length and breadth respectively: ";
cin >> length >> breadth;
}
int AreaCalculation() { return length * breadth; }
void DisplayArea(int temp)
{
cout << "Area: " << temp << endl;
}
};
int main()
{
Area A1, A2(2, 1);
int temp;
cout << "Default Area when no argument is passed." << endl;
temp = A1.AreaCalculation();
A1.DisplayArea(temp);
cout << "Area when (2,1) is passed as argument." << endl;
temp = A2.AreaCalculation();
A2.DisplayArea(temp);
return 0;
}
For object A1, no argument is passed while creating the object.
Thus, the constructor with no argument is invoked which initialises length to 5 and breadth to 2. Hence, area of the object A1 will be 10.
For object A2, 2 and 1 are passed as arguments while creating the object.
Thus, the constructor with two arguments is invoked which initialises length to l (2 in this case) and breadth to b (1 in this case). Hence, area of the object A2 will be 2.
Output :
Default Area when no argument is passed.
Area: 10
Area when (2,1) is passed as argument.
Area: 2
Recommend Articles For More Information :
Introduction Of C++!!.
Operators In C++
Function Overloading In C++
Data Types in C++
Types Of Constructor In C++
Classes and Objects In C++
What Is Inheritance In C++?
Types Of Inheritance?
Basic Concepts Of OOPC!!
CONCEPT OF DBMS!!
Basic Program Of C Language For Print Hello!!
Default Copy Constructor
An object can be initialized with another object of same type. This is same as copying the contents of a class to another class.
In the above program, if you want to initialise an object A3 so that it contains same values as A2, this can be performed as:
int main()
{
Area A1, A2(2, 1);
// Copies the content of A2 to A3
Area A3(A2);
OR,
Area A3 = A2;
}
Types Of Constructor
There are three Types of Constructors in C++
- Default Constructor
- Parameterized Constructor
- Copy COnstructor
Default Constructors
Syntax :
class_name(param1, param2, ...)
{
// constructor define
}
Example:
class C
{
public:
int side;
C()
{
side = 10;
}
};
int main()
{
C c1;
cout << c1.side;
}
10
Look, In this case, as soon as the object is created the constructor is called which initializes its data members.
The default constructor is so important for initialization of object members, that even if we do not define a constructor explicitly, the compiler will provide a default constructor implicitly.
class C
{
public:
int side;
};
int main()
{
C c1;
cout << c1.side;
}
0 or any value
here, default constructor provided by the compiler will be called which will initialize the object data members to default value, that will be 0 or any integer value in this case.
Parameterized Constructors
Parameterized constructors are with parameter.By Using this Constructor you can provide different values to data members of different objects, by passing the values as argument.
Example:
class C{
public:
int side;
C(int x)
{
side=x;
}
};
int main()
{
C c1(10);
C c2(20);
C c3(30);
cout << c1.side;
cout << c2.side;
cout << c3.side;
}
Output :
10
20
30
With the help of parameterized constructor here, we have initialized 3 objects with user defined values.
We can use any number of parameters in a constructor.
Copy Constructors
Copy Constructors are special type of Constructors which is takes an object as argument, and is used to copy values of data members of one object into another object.
For More Knowledge Check This Articles For Better Understanding :
Machine Learning Technology in Software Engineering
Trending Pieces Of Technology You Should Know About!!!
CONCEPT OF DBMS!!
Phases Of Compiler In System Programming!!!!
C Program For Addition, Subtraction, Multiplication, Division Of Two Numbers!!
Difference between a monolithic and micro kernel?
Difference Between The Procedure Oriented Language and Object Oriented Language.
What Is Inheritance In C++? Types Of Inheritance?
Comments
Post a Comment