Constructors and Destructors: C++ 6 Sem, A' Div 2018-19 Prof. Mouna M. Naravani
Constructors and Destructors: C++ 6 Sem, A' Div 2018-19 Prof. Mouna M. Naravani
C++
6th Sem, ‘A’ Div
2018-19
Prof. Mouna M. Naravani
A constructor guarantees that an object created by the class will be initialized automatically.
Ex: create an object
integer int1;
➢ Object int1 is created, and also initializes its data members m and n to zero.
➢ There is no need to write any statement to invoke the constructor function.
➢ If a normal member function is defined for zero initialization, we would need to invoke
this function for each of the objects separately.
➢ This would be very inconvenient, if there are a large number of objects.
Default Constructor
➢ Accepts no parameters.
➢ Also called as Zero Constructor.
➢ If no such constructor is defined, then the compiler supplies a default constructor.
Parameterized Constructor
➢ A constructor that takes arguments or parameters are called Parameterized constructors.
➢ We can pass the arguments to constructor function when object are created.
➢ We must pass the initial values as arguments to the constructor function when an object
is declared.
➢ This can be done in two ways:
By calling the constructor explicitly.
By calling the constructor implicitly.
By calling the constructor explicitly
integer int1 = integer(10, 100);
➢ This statement creates an integer object int1 and passes the values 10 and 100 to it.
By calling the constructor implicitly
integer int1(10, 100);
➢ Also called as short hand method, shorter, better and easy to implement.
Copy Constructor
➢ Copy Constructor is used to declare and initialize an object from another object.
➢ Ex:
integer I2 ( I1 );
➢ This would define an object I2 and at the same time initialize it to the values of I1.
➢ Another form:
integer I2 = I1;//it simply assigns the values of I1 to I2, member-by-memeber
➢ The process of initializing through a copy constructor is known as copy initialization.
➢ A copy constructor takes a reference to an object of the same class as itself as an
argument.
Multiple Constructors in a Class 1. Object declarations:
class integer integer I1;
{
int m, n; - invokes default constructor and set both
public: m and n of I1 to 0.
integer() //Default Constructor
{ 2. Object declarations:
m=0; n=0; integer I2(20, 40);
}
integer(int a, int b) - invokes parameterized constructor and
//Parameterized Constructor
{ set both m and n of I2 to 20 and 40
m=a; n=b;
} respectively.
integer(integer & i) 3. Object declarations:
//Copy Constructor
{ integer I3(I2);
m = i.m; n=i.n;
- invokes copy constructor which copies
};
} the values of I2 into I3.
• The actual parameter, when specified, overrides the default value.
Default Constructor Default Argument Constructor
A() { ---- } A(int = 0) { ---- }
➢ The default argument constructor can be called with either one argument or no
arguments.
➢ When called with no arguments, it becomes a default constructor.
➢ When both these forms are used in a class, it causes ambiguity for a statement such
as:
A a;
The ambiguity is whether to call A() or A(int = 0) --- ??
Destructors
➢ Used to destroy the objects that have been created by a constructor.
➢ The destructor is a member function whose name is the same as the class name but is
preceded by a tilde( ~).
➢ Destructor of class sample can be defined as;
~ sample() { }
➢ Destructor never takes any argument nor does it return any value(not even void).
➢ It will be invoked implicitly by the compiler upon exit from the program to clean up
storage that is no longer accessible. (No need to call it explicitly)
➢ Destructors releases memory space for future use.
➢ Destructors destroy the objects in the reverse order of creation.
➢ Destructors cannot be overloaded. (only 1 destructor in a class).
➢ Whenever new is used to allocate memory in the constructors, we should use delete to
free that memory.
➢ This is required because when the pointers to objects go out of scope, a destructor is not
called implicitly.
➢ Constructors.cpp
➢ Destructors.cpp
➢ DestructoroutofScope.cpp
➢ beforeDestructor.cpp
➢ CopyConstructors.cpp
➢ OverloadedConstructors.cpp
➢ ParameterizedConstructors.cpp
References