OBJECT ORIENTED
PROGRAMMING
Chapter 6: Constructor and
Destructor in C++
Er. Sudeeip Bhandarri
Constructors in C++
● A constructor is a special method that is automatically called when an
object of a class is created.
● It is used to construct and initialize all the data members inside the
objects and so called as constructor. Every time an instance of a class is
created, the constructor method is called.
● A constructor is a class member function used to initialize the objects of
the class. It is treated as a special member function because it has the
same name as the class name.
Why Constructor??
Constructor Rules
● The constructor has the same name as the class.
● It has no return type (not even void).
● It is usually declared public.
● It is automatically called when an object is created.
Without
Constructor With
constructor
Real-Life Analogy
● With Constructor:
Imagine buying a new phone. As soon as you turn it on for the first
time, it automatically sets language, date/time, and network
setup - just like a constructor.
● Without Constructor:
Now, imagine you get the phone, but you have to manually install
all basic settings every time. That's like creating an object without a
constructor.
Constructor with
Parameters
Inside Class Outside Class
Constructor Defined
outside the class
Why Constructors Are Useful??
● Constructors run by themselves when you create an object. They set things up so
everything is ready right away.
● Think of it like this: When you order a pizza (object), the constructor is the chef who
adds the sauce, cheese, and toppings before it gets to you - you don't have to do it
yourself!
● Cleaner Code
● No risk of forgetting initialization
● Automatic Setup
Types of Constructors in C++
Constructors in C++ are classified based on how they accept and assign values during
object creation.
Constructors can be classified based on the situations they are being used in. There are 3
types of constructors in C++:
1. Default Constructor
2. Parameterized Constructor
3. Copy Constructor
1. Default Constructor
● A default constructor is automatically generated by the compiler if the
programmer does not define one.
● This constructor doesn't take any argument as it is parameter less and initializes
object members using default values.
● It is also called a zero-argument constructor. However, the default constructor is
not generated by the compiler if the programmer has explicitly defined a
constructor.
● Takes no parameters.
● Automatically assigns default values to the object.
The Person() constructor has no parameters – that's
why it’s called a default constructor.
When Person p1; is created, it automatically sets
name = "Ram" and age = 25.
Why to use this
What is Default
default constructor
Constructor??
Concept??
When to use
default
Constructor??
When is a Default Constructor Used?
● When you want to initialize default values for your object.
● When you create an object without passing any arguments.
● Helpful in arrays of objects, where objects are initialized automatically.
● Used in inheritance, object arrays, and object creation before input
assignment.
Why to use this
What is
Parameterized
Parameterized
Constructor
Constructor??
Concept??
When to use
Parameterized
Constructor??
Without With
What is a Parameterized Constructor?
A parameterized constructor is a constructor that accepts arguments to
initialize an object’s data members at the time of object creation.
Syntax:
ClassName(type param1, type param2, ...) {
// Use parameters to initialize data members
}
Why Use a Parameterized Constructor?
● To initialize an object with specific values right when it is
created.
● Saves time : no need to set each member individually using setter
functions.
● Makes the code cleaner, shorter, and more meaningful.
When to Use a Parameterized Constructor?
You want to create objects with custom values during creation.
You’re working with classes that require important initial data, such as:
● Student name and marks
● Bank account number and balance
● Employee ID and salary
You want to avoid using multiple setter functions after object creation.
2. Parameterized Constructor
● Parameterized constructor allow us to pass arguments to constructors.
● Typically, these arguments help initialize an object's members. To create a
parameterized constructor, simply add parameters to it the way you would to
any other function.
● When you define the constructor’s body, use the parameters to initialize the
object's members.
● Accepts arguments to initialize object properties.
● Allows custom object creation with specific values.
The constructor Person(string n, int a) takes
arguments.
When we create objects like Person p1("Sita",
30), the constructor assigns values directly.
Why These Are Real Examples:
● You initialize objects with real-world values directly when creating
them.
● You avoid using multiple setter functions.
● You use constructors to ensure that no object is created without
required values.
Why to use this
What is Copy
Copy Constructor
Constructor??
Concept??
When to use
Copy
Constructor??
What is a Copy Constructor?
A copy constructor is a special constructor that creates a new object as a copy of an
existing object.
Syntax:t
ClassName(const ClassName &obj)
// copy obj's data to this object
It takes a reference to another object of the same class as its argument.
Why Use a Copy Constructor?
● To duplicate an object’s values into a new object.
● To create a deep copy (especially when dealing with dynamically
allocated memory).
● Ensures that one object can be safely copied into another without
unintended side effects.
● Used when passing objects by value or returning objects from functions.
When Is the Copy Constructor Used?
3. Copy Constructor
● A copy constructor is a member function that initializes an object using another
object of the same class.
● Copy constructor takes a reference to an object of the same class as an
argument.
● C++ provides a particular type of Constructor that takes an object as an
argument and is used to copy the values of data members of one object to
another. In this case, the copy constructor is used to declare and initialize an
object from another object.
● Creates a new object as a copy of an existing object.
Syntax: ClassName(const
ClassName &oldObj)
Constructor Overloading in C++
In C++, We can have more than one constructor in a class with same name, as long as
each has a different list of arguments.This concept is known as Constructor
Overloading and is quite similar to function overloading.
● Overloaded constructors essentially have the same name (exact name of the
class) and different by number and type of arguments.
● A constructor is called depending upon the number and type of arguments
passed.
● While creating the object, arguments must be passed to let compiler know,
which constructor needs to be called.
Destructor in C++
● Destructor is an instance member function that is invoked
automatically whenever an object is going to be destroyed.
● Meaning, a destructor is the last function that is going to be called
before an object is destroyed.
● The name of the destructor is the same as the name of the class, except
that it has a tilde (~) before its name. It is a good practice to declare the
destructor after the Constructor has finished using it.
Characteristics of a Destructor
● Destructor has the same name as their class name preceded by a tilde (~) symbol.
● It is not possible to define more than one destructor.
● The destructor is only one way to destroy the object created by the constructor.
Hence, destructor cannot be overloaded.
● It cannot be declared static or const.
● Destructor neither requires any argument nor returns any value.
● It is automatically called when an object goes out of scope.
● Destructor release memory space occupied by the objects created by the
constructor.
● In destructor, objects are destroyed in the reverse of an object creation.
When is the destructor called?
A destructor function is called automatically when the object goes out of
scope or is deleted. Following are the cases where destructor is called:
1. Destructor is called when the function ends.
2. Destructor is called when the program ends.
3. Destructor is called when a block containing local variables ends.
4. Destructor is called when a delete operator is called.