Loading
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Amit MohantyRank #112 · 20K reputationPosted Oct 12, 2022, 3:55 AM EDT
There are 3 types of Constructors.
1. Default Constructor
A constructor with no arguments (or parameters) in the definition is a default constructor. Usually, these constructors use to initialize data members (variables) with real values.
Note: If no constructor is explicitly declared, the compiler automatically creates a default constructor with no data member (variables) or initialization.
Syntax of Default Constructor –
class CLASS_NAME
{
………
public :
CLASS_NAME() //Default constructor
{
. . . . . .
}
//other member functions
};
Example:
#include using namespace std; //Class Name: Default_construct class Default_construct { public: int a, b; // Default Constructor Default_construct() { a = 100; b = 200; } }; int main() { Default_construct con; //Object created cout << "Value of a: " << con.a; cout<< "Value of b: " << con.b; return 0; }2. Parameterized Constructor
Unlike, Default constructor, It contains parameters (or arguments) in the constructor definition and declaration. More than one argument can also pass through a parameterized constructor.
Moreover, these types of constructors use for overloading to differentiate between multiple constructors.
Syntax –
class class_Name
{
public:
class_Name(datatype variable) //Parameterized constructor
{
………………..
}
};
Example:
#include using namespace std; // class name: Rectangle class Rectangle { private: double length; double breadth; public: // parameterized constructor Rectangle(double l, double b) { length = l; breadth = b; } double calculateArea() { return length * breadth; } }; int main() { // create objects to call constructors Rectangle obj1(10,6); Rectangle obj2(13,8); cout << "Area of Rectangle 1: " << obj1.calculateArea(); cout << "Area of Rectangle 2: " << obj2.calculateArea(); return 0; }3. Copy Constructor
A copy constructor is a member function that initializes an object using another object of the same class. It helps to copy data from one object to another.
Example –
#include using namespace std; // class name: Rectangle class Rectangle { private: double length; double breadth; public: // parameterized constructor Rectangle(double l, double b) { length = l; breadth = b; } // copy constructor with a Rectangle object as parameter copies data of the obj parameter Rectangle(Rectangle &obj) { length = obj.length; breadth = obj.breadth; } double calculateArea() { return length * breadth; } }; int main() { // create objects to call constructors Rectangle obj1(10,6); Rectangle obj2 = obj1; //copy the content using object //print areas of rectangles cout << "Area of Rectangle 1: " << obj1.calculateArea(); cout << "Area of Rectangle 2: " << obj2.calculateArea(); return 0; }