0% found this document useful (0 votes)
8 views58 pages

Constructor and Destructors

Uploaded by

priyanshu110101
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views58 pages

Constructor and Destructors

Uploaded by

priyanshu110101
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 58

CONSTRUCTOR AND

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.

2.Allow users to be registered with specific details at the time of


object creation.

3.Support creating a duplicate user profile based on an existing


user.
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
• 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.

Deleting Dynamic Objects


When you're finished with a dynamic object, you must delete it using the delete operator to free
the memory allocated for it. Failure to do so can lead to memory leaks.
Array of Dynamic Objects
You can also create arrays of
dynamic objects using the
new operator.
create arrays of dynamic objects using the
new operator
User Management System
APPLICATION
ORDER OF INVOCATION
• Whenever we create an object of a class, the default constructor of that class is invoked
automatically to initialize the members of the class.
• If we inherit a class from another class and create an object of the derived class, it is clear that
the default constructor of the derived class will be invoked but before that the default
constructor of all of the base classes will be invoke, i.e the order of invocation is that the base
class’s default constructor will be invoked first and then the derived class’s default constructor
will be invoked.
Why the base class’s constructor is called on creating an object of
derived class?

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.

You might also like