0% found this document useful (0 votes)
12 views5 pages

Constructorssss

Uploaded by

radhikaj500
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views5 pages

Constructorssss

Uploaded by

radhikaj500
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Constructor

A constructor is a special member function of a class that is automatically invoked whenever an object
of that class is created. Its primary purpose is to initialize the data members of the object.

Key Features of Constructors:

1. Same Name as the Class: A constructor must have the same name as the class it belongs to.
2. No Return Type: It doesn't have a return type, not even void.
3. Automatically Invoked: It's automatically called when an object of the class is created.
4. Initialization: It's used to initialize the member variables of the object.
5. Overloading: You can have multiple constructors with different parameter lists, allowing for
flexible object initialization.

Types of Constructors:

Default Constructor:

1. A constructor without any parameters.


2. It's automatically provided by the compiler if no other constructor is defined.
3. It initializes members to their default values (0 for numbers, null for pointers, etc.).

Parameterized Constructor:

1. A constructor with parameters.


2. It allows you to initialize member variables with specific values during object
creation.

Copy Constructor:

1. A constructor that takes an object of the same class as an argument.


2. It's used to create a copy of an existing object.

Why Use Constructors?

 Initialization: Ensures proper initialization of object members.


 Error Prevention: Prevents objects from being created in an invalid state.
 Resource Management: Can be used to allocate and initialize resources during object
creation.
 Flexibility: Allows for different ways to create objects with various initializations.

Programs:

Zero Argument Constructor

#include<iostream.h>

class Box
{
public:
int length;
int breadth;
int height;

// Default constructor
Box() {
length = 0;
breadth = 0;
height = 0;
}
};

void main()
{
Box box1;
cout << "Box 1 Details:" << endl;
cout << "Length: " << box1.length << endl;
cout << "Breadth: " << box1.breadth << endl;
cout << "Height: " << box1.height << endl;
}

Parameterized Constructor
#include<iostream.h>
#include<conio.h>

class Box
{
public:
int length;
int breadth;
int height;

// Default constructor
Box()
{
length = 0;
breadth = 0;
height = 0;
}
// Parameterized constructor
Box(int l, int b, int h)
{
length = l;
breadth = b;
height = h;
}
};

void main()
{
clrscr();
Box box1;
cout << "Box 1 Details:" << endl;
cout << "Length: " << box1.length << endl;
cout << "Breadth: " << box1.breadth << endl;
cout << "Height: " << box1.height << endl;

Box box2(10, 20, 30);


cout << "Box 2 Details:" << endl;
cout << "Length: " << box2.length << endl;
cout << "Breadth: " << box2.breadth << endl;
cout << "Height: " << box2.height << endl;
getch();
}
Copy Constructor
#include<iostream.h>
#include<conio.h>

class Box
{
public:
int length;
int breadth;
int height;

// Default constructor
Box()
{
length = 0;
breadth = 0;
height = 0;
}
// Parameterized constructor
Box(int l, int b, int h)
{
length = l;
breadth = b;
height = h;
}
// Copy constructor
Box(Box& obj)
{
length = obj.length;
breadth = obj.breadth;
height = obj.height;
}

};

void main()
{
clrscr();
Box box1;
cout << "Box 1 Details:" << endl;
cout << "Length: " << box1.length << endl;
cout << "Breadth: " << box1.breadth << endl;
cout << "Height: " << box1.height << endl;

Box box2(10, 20, 30);


cout << "Box 2 Details:" << endl;
cout << "Length: " << box2.length << endl;
cout << "Breadth: " << box2.breadth << endl;
cout << "Height: " << box2.height << endl;

Box box3 = box2; // Copy constructor is called here


cout << "Box 3 Details:" << endl;
cout << "Length: " << box3.length << endl;
cout << "Breadth: " << box3.breadth << endl;
cout << "Height: " << box3.height << endl;
getch();
}

You might also like