Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

Constructor

A constructor is a special member function whose task is to initialize the


objects of its class. It is special because its name is the same as the class
name. The constructor is invoked whenever an object of its associated
class is created. It is called constructor because it constructs the values to
data members of the class.
A constructor has the same name as the class and no return value. A
constructor can have any number of parameters and a class may have any number
of overloaded constructors. Constructors may have any accessibility, public,
protected or private. If you don't define any constructors, the compiler will
generate a default constructor that takes no parameters; you can override this
behavior by declaring a default constructor as deleted.

Characteristics of a constructor (ACEM note)


1. They should be declared in the public section.
2. They are invoked automatically when the objects are created.
3. They do not have return types, not even void and therefore they cannot return
the value.
4. They cannot be inherited, though a derived class can call the base class
constructor.
5. Like other C-H- functions, they can have default arguments.
6. Constructors cannot be virtual.
7. We cannot refer to their addresses.
8. An object with a constructor (or destructor) cannot be used as a member of a
union.
9. They make implicit calls to the operators new and delete when memory
allocation is required.
Characteristics of a constructor (Mam Slide)
•Constructor has the same name as that of class it belongs.
• Constructor is executed when a object is declared.
• Every class has an implicit constructor when not defined
• Constructor have no return type, not even void.
• Job of the constructor is to initialize objects and allocate appropriate memory to
the object.
• Though constructor are executed implicitly, it can be executed explicitly.
• Constructor can be overloaded.

Syntax of defining a constructor function in a class:


class A
{
public:
int x;
// constructor
A()
{
// object initialization
}
};

While defining a constructor you must remember that the name of constructor will
be same as the name of the class, and constructors will never have a return type.
Constructors can be defined either inside the class definition or outside
class definition using class name and scope resolution :: operator.
class A
{
public:
int i;
A(); // constructor declared
};

// constructor definition
A::A()
{
i = 1;
}

Example of constructor
#include<iostream>
using namespace std;
class constructor
{
int a, b;
public:
constructor()
{
a=3;
b=2;
}
void display()
{
cout<<"a= "<<a<<endl;
cout<<"b= "<<b<<endl;
}
};
main()
{
constructor obj;
obj.display();
}

Output:
a= 3
b= 2

Another Example
How constructor works? Class Note (handwritten)
Use of constructor
How constructor are different from a normal member function?
Parameterized Constructor

End Semester Questions


1. List out four features of destructor of a class? How is it different with
constructor?
2. Differentiate between default constructor and parameterized constructor
with suitable example.
3. Compare and contrast constructor with destructor and write a C++ program
to illustrate them.
4. What is class and object in C++? Discuss copy constructor in details. Illustrate
the use of default constructor, parameterized constructor, copy constructor
and destructor with suitable programming example.
5. What is a copy constructor and when is it called? Illustrate with example.
6. Define constructor, copy constructor, and destructor. What is the
significance of each of them in C++?

You might also like