CHAPTHER- 5 | Constructors and Destructors
Q. What is a constructor?
Ans.: A constructor is a special member function of a class that is automatically
called when an object of that class is created. The constructor’s main purpose is to
initialize values to the object’s data members and preparing it for use. It has the
same name as the class and has no return type (not even void). It is typically defined
n the class declaration and implemented outside the class.
Q. How to declare and define a constructor?
Ans.: A constructor can be defined in two ways, either inside the class definition or
outside the class definition.
Inside the class definition: Declare the constructor using the same name as
the class. Constructor do not have a return type not even ‘void’. Multiple constructor
can be declared with different parameter list (i.e., constructor can also be overload).
For example:
Outside the class definition: Declare the constructor outside the class using
the scope resolution operator ‘::’ to associate it with the class. For example:
Page |1
Q. What is default constructor?
Ans.: A default constructor is a special constructor that is automatically generated
by the compiler when no explicit constructor is defined in a class. It is called default
constructor because it does not take any argument. For example:
In this program, the class A does not have an explicit constructor defined, so
the compiler automatically generates a default constructor us. Whenever the object
of that class is created default constructor is automatically called.
Q. What is parameterized constructor?
Ans.: A parameterized constructor is a constructor that accepts one or more
parameter/arguments. It allows us to initialize the object’s data members with
custom values provided during object creation. Unlike default constructors, a
parameterized constructor requires arguments to be passed. For example:
Page |2
INVOCATION OF CONSTRUCTOR
Q. How can constructor be invoked?
Ans.: Constructor invocation refers to the process of calling a constructor to create
and initialize an object. When a constructor is invoked, memory is allocated for the
object and its data members are initialized according to the constructor’s
implementation. The constructor can be invoked primarily in two ways-
a. By calling the constructor implicitly (implicit Invocation)
b. By calling the constructor explicitly (Explicit Invocation)
Implicit Invocation: By implicit call or invocation means when and object is
declared without explicitly calling a constructor the default constructor (if available)
is implicitly invoked. For example:
Explicit Invocation: By explicit call or invocation means constructor can be
explicitly invokes by using the class name followed by parentheses containing the
constructor arguments. This allows us to choose a specific constructor and provide
custom initialization values. For example:
Q. What is copy constructor?
Ans.: A copy constructor is special constructor that creates a new object by making a
copy of existing object. It allows for the creation of a new object with the same
attributes values as another object of the same class. The syntax for a copy
constructor is as follows-
The above syntax words just like assigning value of one variable to another variable.
For example-
Page |3
Q. What is a copy Constructor called?
Ans.: A copy constructor is invoked when a new object is created as a copy of an
existing object. It copies the attributes values from the source object to the newly
created. There are two other situations that may occur when a copy constructor is
invoked. There are –
a. When an object is passed by value.
b. When a function returns and objects.
Q. What is constructor overloading?
Ans.: Just like any other function a class can also have multiple constructors with
different parameter lists. Thus, multiple constructors having different parameters in
a class are known as constructor overloading. Each constructor can initialize the
object in different ways when creating objects of that class. For example:
Page |4
Q. What is a Destructor?
Ans.: A destructor is a special member function of a class that is used to destroy the
objects that have been created by constructor. A destructor destroys the values of
the object being destroyed. The destructor is named with the same name as the
class, preceded by a little (~) sign. It does not take any arguments and does not have
a return type. For example:
Page |5
Page |6