Constructor and Destructors
Constructor and Destructors
DESTRUCTORS
INTRODUCTION
• For setting the values for private member we have used putdata( ) and setvalue( )
• These functions cannot be used to initialize the member variables at the time of creation of their objects, which does not
conform with the philosophy of C++ language.
• In C++ we aim to create user-defined data types such as class that behave very similar to the built-in types. This means
that we should be able to initialize a class type variable(object) when declared, much the same way as initialization of an
ordinary variable.
CONSTRUCTORS
• Constructor in C++ is a special method that is invoked
automatically at the time an object of a class is created. It is used to
initialize the data members of new objects generally.
• The constructor in C++ has the same name as the class or structure.
It constructs the values i.e. provides data for the object which is
why it is known as a constructor.
• Constructors can also take parameters (just like regular functions),
which can be useful for setting initial values for attributes.
METHODS OF DECLARING AN CONSTRUCTOR
TYPES OF CONSTRUCTORS
When to Use:
•When you need to initialize objects with default values.
•When you don’t want to manually initialize every object.
•When working with arrays of objects (since an array of
objects requires a constructor with no arguments).
When to Use:
•When each object needs to be initialized with different
values at the time of creation.
•When you want to enforce that objects always start
with meaningful data.
•When user input or pre-defined values need to be set
during object creation.
Copy Constructor
When to Use:
•When creating a duplicate object with the same
values as an existing object.
•When passing objects by value to functions.
•When returning objects from functions.
•When an object needs to be duplicated before
modification, preserving the original state.
Real-World Flow of Constructor Usage
1.Default Constructor: Used when creating objects with default
values.
2.Parameterized Constructor: Used when specific initial values
are required.
3.Copy Constructor: Used when an existing object needs to be
duplicated.
A software company is developing a User Management System
where each user has a username, email, and ID. The system should:
1. Allow new users to be created with default values if no details
are provided.
•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
• 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.
• A destructor in C++ is a special member function that is
automatically called when an object goes out of scope or is explicitly
deleted. It is used to clean up resources such as dynamically
allocated memory, file handles, database connections, or network
resources.
Destructor Defined Inside The Class
Destructor Defined Outside The Class
Dynamic Constructor
• When allocation of memory is done dynamically using dynamic memory
allocator new in a constructor, it is known as dynamic constructor. By
using this, we can dynamically initialize the objects.
• This allows you to create objects at runtime, rather than at compile time.
Why Use Dynamic Constructors?
•Flexibility: Dynamic constructors provide flexibility in object
creation, allowing you to create objects based on runtime conditions or
user input.
•Memory Management: You have more control over memory
allocation and deallocation, which can be crucial for performance-
critical applications.
•Polymorphism: Dynamic constructors are essential for creating
objects of derived classes in polymorphic scenarios.
Creating Dynamic Objects
To create a dynamic object, you use the new operator followed by the class name. This allocates
memory on the heap for the object and returns a pointer to it.
The data members and member functions of base class comes automatically in derived class
based on the access specifier but the definition of these members exists in base class only. So
when we create an object of derived class, all of the members of derived class must be
initialized but the inherited members in derived class can only be initialized by the base class’s
constructor as the definition of these members exists in base class only. This is why the
constructor of base class is called first to initialize all the inherited members.
Single Inheritance Simple Class (no inheritance):
Invocation of constructors and Destructors – [C++]
• The sequence of invocation of constructors and destructors is important to know, as it
depends on the type of inheritance being used.
• Constructor and destructor invocation (invoked)in single inheritance
In single inheritance, firstly, parent class constructors are called and then the child class
constructors are called.
Destructor and Destructor invocation (invoked)in single inheritance
Single inheritance: Invocation of constructors and
Destructors
In single inheritance, firstly, Child class destructors are called and then the parent class
destructors are called.
Multiple inheritance:
Invocation of constructors
and Destructors
Multilevel inheritance:
Invocation of constructors and
Destructors
Dynamic Memory
Allocation
The mechanism by which storage/memory/cells can be allocated to variables during the
run time is called Dynamic Memory Allocation
Dynamic memory allocation is the process of assigning the memory space during the execution time or the run time.
Here,
•data_type is the type of data that we want to store in the array.
•* pointer_variableName declares a pointer variable.
•new is a keyword used for dynamic memory allocation.
•array_size is the size of the array we want to allocate.