SlideShare a Scribd company logo
1 of 24
Download to read offline
Constructors & Destructors
Prof. K. Adisesha
Learning Outcomes
 Introduction
 Defining Constructors
 Characteristics of Constructor
 Types of constructor
 Constructor Overloading
 Destructors
 Characteristics of destructor
2
Introduction
Introduction
 It is sometimes convenient if an object can initialize itself
when it is first created, without the need to make a
separate call to member functions.
 Automatic initialization is carried out using special
member functions called constructors.
 Automatic de-allocating of memory when an object of
that class is destroyed is carried out using special member
functions called destructor.
3
Constructors
Constructors:
 A Constructor is a special member function that is
called automatically when an object is created.
 The purpose of a constructor is to mainly initialize the
member variables of a class.
 The general syntax of a the constructor in C++ is:
class MyClass // The class
{ public: // Access specifier
MyClass() // Constructor
{ cout << "Hello World!";
}
}; 4
Constructors
Characteristics of Constructor:
 The name of the constructor is the same as the name of the
class.
 A Constructor, even though it is a function, has no return type
function nor a void function.
 The constructor should be declared in the public section.
 Constructors are executed automatically.
 They are executed when a class object is created.
 A class can have more than one constructor.
 It is not possible to refer to the address of the constructors.
 The constructors make implicit calls to the operator new and
delete when memory allocation is required.
5
Constructors
Need for a Constructor:
 Constructors are named as constructors because they are
getting called when an object is constructed.
 The use of a constructor can be cleverly done especially
in those problems where it is necessary to initialize certain
data members compulsorily.
 Instead of having separate member functions for
initializing we can perform those operations inside the
constructor itself.
6
Constructors
Types of constructor:
 Constructors are normally classified as follows:
 Default Constructors.
 Parameterized Constructors.
 Copy Constructors.
7
Constructors
Default Constructors:
 A default constructor is a special member function which is
invoked by the C++ compiler without any argument for
initializing the object of a class.
 It is also called as zero argument constructors.
 Some of the features of the default constructors are:
 A default constructor function initializes the data member with no
argument.
 It can be explicitly written in the public section of the class.
 In case, default constructor is not defined in a program, the C++
compiler automatically generates it in a program.
 The purpose of the default constructor is to construct a default object of
the class type.
8
Constructors
Default Constructors:
 The general format of default constructor is as follows.
9
Syntax:
class Class_Name
{
public:
Class_Name( )
{
…….
}
};
Example:
class Number
{
public:
Number( )
{
n = 0;
}
};
Constructors
Disadvantages of default constructors are:
 When many objects of the same class are created, all
objects are initialized to same set of values by default
constructors.
 It is not possible to initialize different objects with
different initial values using default constructors.
10
Constructors
Parameterized Constructors:
 A constructor that takes one or more arguments is called
parameterized constructor.
 Using this constructor, it is possible to initialize different
objects with different values.
 Parameterized constructors are also invoked automatically,
whenever objects with arguments are created.
 Some of the features of the parameterized constructors are:
 The parameterized constructors can be overloaded.
 For an object created with one argument, constructor with only one
argument is invoked and executed.
 The parameterized constructor can have default arguments and default
values.
11
Constructors
Parameterized Constructors:
 The general format of Parameterized constructor is as follows.
12
Syntax:
class Class_Name
{
public:
Class_Name(arg1, arg2)
{
…….
}
};
Example:
class Number
{
public:
Number(int a, int b )
{ if (a > b)
big = a;
else
big = b; }
};
Constructors
Copy Constructors:
 Copy constructor is a parameterized constructor using one
object can be copied to another object.
 Copy Constructors are used in the following situations:
 To initialize an object with the values of already existing
objects.
 When objects must be returned as function values.
 To state objects as by value parameters of a function.
 Copy Constructor can accept a single argument of reference to
same class type.
 The argument must be passed as a constant reference type.
13
Constructors
Copy Constructors:
 The general format of copy constructor is as follows.
14
Syntax:
class Class_Name
{
public:
Class_Name( Class_Name &ptr )
{
…….
}
};
Example:
class Number
{
public:
Number(int n)
{ a = n; }
Number(Number &X)
{ a = X.a;
cout<<”Copy Constructor
invoked”;
} };
Constructors
Invoking Constructors
 A Constructor is automatically invoked by C++ compiler
with an object declaration.
 The constructor can be invoked through the following
methods.
 Implicit Call
 Explicit Call
 Initialization with “ = ” Assignment operator
15
Invoking Constructors
Implicit Call:
 An Implicit call means the declaration of the object is followed
by argument list enclosed in parenthesis.
 Syntax:
Class_name Object(Arg1, Arg2,…)
 Example:
num obj1(10, 20); //Implicit Call
num obj2(40, 50); //Implicit Call
16
Invoking Constructors
Explicit Call:
 In explicit call, declaration of an object is followed by
assignment operator, constructor name and argument list
enclosed in parenthesis.
 Syntax:
Class_name Object=Constructor_name(Arg1, Arg2,…)
 Example:
num obj1 = num(10, 20); //Explicit Call
num obj2 = num(40, 50); //Explicit Call
17
Invoking Constructors
Initialization with “ = ” Assignment operator:
 This method is used for the constructor with exactly one
argument.
 In this method declaration is followed by assignment operator
and value to be initialized..
 Syntax:
Class_name Object=Arg1
 Example:
num obj1 = 100
num obj2 = 200
18
Invoking Constructors
#include<iostream.h>
#include<conio.h>
class num
{
private:
int a, b;
public:
num ( int m) // Constructor
{ a = m; b=m; }
num (int m, int n) // Constructor
{
a = m;
b = n;
}
void display( )
{
cout<<” a = “ << a <<” b = “ << b;
}
};
void main( )
{
num obj1(10, 20); //Implicit Call
num obj2=num(30, 40); //Explicit Call
Num obj3=50; //Assignment operator
obj1.display( );
obj2.display( );
Obj3.display( );
}
19
Program to initialize objects using Invoking Constructors
Constructor
Constructor Overloading:
 A class has two or more constructor functions with the same
name but different signatures are called Constructors
Overloading.
 Depending upon the type of argument, the constructors will be
invoked automatically by the compiler to initialize the objects.
 Example: class num
{
public:
num ( ); // Constructor with no agruments
num ( int m); // Constructor with one agrument
num (int m, int n); // Constructor with two agruments
}; 20
Destructors
Destructors:
 A destructor is special member function that is executed
when an object of that class is destroyed.
 Destroying an object means, de-allocating all the
resources such as memory that was allocated for the
object by the constructor.
 It will have like constructor, the name same as that of the
class but preceded by a tilde (~).
21
Destructors
Destructors:
 The general format of destructor is as follows.
22
Syntax:
class Class_Name
{
public:
Class_Name( );
~ Class_Name( );
};
Example:
class Counter
{
public:
Counter( ) //Constructor
{
n = 0;
}
~Counter ( ) //Destructor
{ }
};
Destructors
Characteristics of destructor:
 The destructor name must have the same name as the
class preceded by a tilde (~).
 The destructor cannot take arguments therefore cannot be
overloaded.
 The destructor has no return type.
 There can be only one destructor in each class.
 In should have public access in the class declaration.
 The destructor cannot be inherited.
23
Discussion
Questions:
 What is Constructor? Give the rules for writing a constructor
function.
 What is default constructor? Write a program to illustrate it.
 Explain parameterized constructor with syntax and example.
 Mention the different methods by which constructor are
invoked. Explain anyone with an illustrative example.
 Explain the features of copy constructor.
 Explain destructor with syntax and example.
24

More Related Content

What's hot

String Handling in c++
String Handling in c++String Handling in c++
String Handling in c++Fahim Adil
 
Keywords, identifiers ,datatypes in C++
Keywords, identifiers ,datatypes in C++Keywords, identifiers ,datatypes in C++
Keywords, identifiers ,datatypes in C++Ankur Pandey
 
INLINE FUNCTION IN C++
INLINE FUNCTION IN C++INLINE FUNCTION IN C++
INLINE FUNCTION IN C++Vraj Patel
 
Object oriented programming c++
Object oriented programming c++Object oriented programming c++
Object oriented programming c++Ankur Pandey
 
C the basic concepts
C the basic conceptsC the basic concepts
C the basic conceptsAbhinav Vatsa
 
Variables in C and C++ Language
Variables in C and C++ LanguageVariables in C and C++ Language
Variables in C and C++ LanguageWay2itech
 
C++ Programming Language
C++ Programming Language C++ Programming Language
C++ Programming Language Mohamed Loey
 
Functions in c language
Functions in c language Functions in c language
Functions in c language tanmaymodi4
 
Lecture 02: Preliminaries of Data structure
Lecture 02: Preliminaries of Data structureLecture 02: Preliminaries of Data structure
Lecture 02: Preliminaries of Data structureNurjahan Nipa
 
Polymorphism in c++(ppt)
Polymorphism in c++(ppt)Polymorphism in c++(ppt)
Polymorphism in c++(ppt)Sanjit Shaw
 
Multiple inheritance possible in Java
Multiple inheritance possible in JavaMultiple inheritance possible in Java
Multiple inheritance possible in JavaKurapati Vishwak
 
data types in C programming
data types in C programmingdata types in C programming
data types in C programmingHarshita Yadav
 

What's hot (20)

Functions in c++
Functions in c++Functions in c++
Functions in c++
 
Strings in c++
Strings in c++Strings in c++
Strings in c++
 
String Handling in c++
String Handling in c++String Handling in c++
String Handling in c++
 
Constructors in C++
Constructors in C++Constructors in C++
Constructors in C++
 
Unit 2. Elements of C
Unit 2. Elements of CUnit 2. Elements of C
Unit 2. Elements of C
 
Keywords, identifiers ,datatypes in C++
Keywords, identifiers ,datatypes in C++Keywords, identifiers ,datatypes in C++
Keywords, identifiers ,datatypes in C++
 
INLINE FUNCTION IN C++
INLINE FUNCTION IN C++INLINE FUNCTION IN C++
INLINE FUNCTION IN C++
 
Object oriented programming c++
Object oriented programming c++Object oriented programming c++
Object oriented programming c++
 
Array in c#
Array in c#Array in c#
Array in c#
 
Introduction to c programming
Introduction to c programmingIntroduction to c programming
Introduction to c programming
 
C the basic concepts
C the basic conceptsC the basic concepts
C the basic concepts
 
Class and object
Class and objectClass and object
Class and object
 
Variables in C and C++ Language
Variables in C and C++ LanguageVariables in C and C++ Language
Variables in C and C++ Language
 
C++ Programming Language
C++ Programming Language C++ Programming Language
C++ Programming Language
 
Functions in c language
Functions in c language Functions in c language
Functions in c language
 
Lecture 02: Preliminaries of Data structure
Lecture 02: Preliminaries of Data structureLecture 02: Preliminaries of Data structure
Lecture 02: Preliminaries of Data structure
 
Polymorphism in c++(ppt)
Polymorphism in c++(ppt)Polymorphism in c++(ppt)
Polymorphism in c++(ppt)
 
Multiple inheritance possible in Java
Multiple inheritance possible in JavaMultiple inheritance possible in Java
Multiple inheritance possible in Java
 
data types in C programming
data types in C programmingdata types in C programming
data types in C programming
 
C introduction by thooyavan
C introduction by  thooyavanC introduction by  thooyavan
C introduction by thooyavan
 

Similar to Constructors & Destructors: Initialization and Memory Management in C

Constructor and desturctor
Constructor and desturctorConstructor and desturctor
Constructor and desturctorSomnath Kulkarni
 
Constructors in C++.pptx
Constructors in C++.pptxConstructors in C++.pptx
Constructors in C++.pptxRassjb
 
Constructors and destructors in C++
Constructors and destructors in  C++Constructors and destructors in  C++
Constructors and destructors in C++RAJ KUMAR
 
Constructors & Destructors [Compatibility Mode].pdf
Constructors & Destructors [Compatibility Mode].pdfConstructors & Destructors [Compatibility Mode].pdf
Constructors & Destructors [Compatibility Mode].pdfLadallaRajKumar
 
Constructor and Destructor in c++
Constructor  and Destructor in c++Constructor  and Destructor in c++
Constructor and Destructor in c++aleenaguen
 
chapter-9-constructors.pdf
chapter-9-constructors.pdfchapter-9-constructors.pdf
chapter-9-constructors.pdfstudy material
 
Constructor and Destructor
Constructor and DestructorConstructor and Destructor
Constructor and DestructorSunipa Bera
 
constructor in object oriented program.pptx
constructor in object oriented program.pptxconstructor in object oriented program.pptx
constructor in object oriented program.pptxurvashipundir04
 
Constructor and destructor
Constructor and destructorConstructor and destructor
Constructor and destructorrajshreemuthiah
 
ConsTRUCTION AND DESTRUCTION
ConsTRUCTION AND DESTRUCTIONConsTRUCTION AND DESTRUCTION
ConsTRUCTION AND DESTRUCTIONShweta Shah
 
Constructor and destructor in oop
Constructor and destructor in oop Constructor and destructor in oop
Constructor and destructor in oop Samad Qazi
 
Constructor&amp; destructor
Constructor&amp; destructorConstructor&amp; destructor
Constructor&amp; destructorchauhankapil
 
C++ Constructor and Destructors.pptx
C++ Constructor and Destructors.pptxC++ Constructor and Destructors.pptx
C++ Constructor and Destructors.pptxsasukeman
 

Similar to Constructors & Destructors: Initialization and Memory Management in C (20)

Constructor and desturctor
Constructor and desturctorConstructor and desturctor
Constructor and desturctor
 
Constructors in C++.pptx
Constructors in C++.pptxConstructors in C++.pptx
Constructors in C++.pptx
 
Constructors & Destructors
Constructors  & DestructorsConstructors  & Destructors
Constructors & Destructors
 
Constructors and destructors in C++
Constructors and destructors in  C++Constructors and destructors in  C++
Constructors and destructors in C++
 
Constructors & Destructors [Compatibility Mode].pdf
Constructors & Destructors [Compatibility Mode].pdfConstructors & Destructors [Compatibility Mode].pdf
Constructors & Destructors [Compatibility Mode].pdf
 
Constructor and Destructor in c++
Constructor  and Destructor in c++Constructor  and Destructor in c++
Constructor and Destructor in c++
 
chapter-9-constructors.pdf
chapter-9-constructors.pdfchapter-9-constructors.pdf
chapter-9-constructors.pdf
 
Constructor and Destructor
Constructor and DestructorConstructor and Destructor
Constructor and Destructor
 
constructor in object oriented program.pptx
constructor in object oriented program.pptxconstructor in object oriented program.pptx
constructor in object oriented program.pptx
 
Constructor,destructors cpp
Constructor,destructors cppConstructor,destructors cpp
Constructor,destructors cpp
 
Constructor and destructor
Constructor and destructorConstructor and destructor
Constructor and destructor
 
ConsTRUCTION AND DESTRUCTION
ConsTRUCTION AND DESTRUCTIONConsTRUCTION AND DESTRUCTION
ConsTRUCTION AND DESTRUCTION
 
Constructor and destructor in oop
Constructor and destructor in oop Constructor and destructor in oop
Constructor and destructor in oop
 
Constructor&amp; destructor
Constructor&amp; destructorConstructor&amp; destructor
Constructor&amp; destructor
 
Constructors and Destructors
Constructors and DestructorsConstructors and Destructors
Constructors and Destructors
 
Constructor and destructor in C++
Constructor and destructor in C++Constructor and destructor in C++
Constructor and destructor in C++
 
Constructors and destructors in C++ part 2
Constructors and destructors in C++ part 2Constructors and destructors in C++ part 2
Constructors and destructors in C++ part 2
 
C++
C++C++
C++
 
C++
C++C++
C++
 
C++ Constructor and Destructors.pptx
C++ Constructor and Destructors.pptxC++ Constructor and Destructors.pptx
C++ Constructor and Destructors.pptx
 

More from Prof. Dr. K. Adisesha

Software Engineering notes by K. Adisesha.pdf
Software Engineering notes by K. Adisesha.pdfSoftware Engineering notes by K. Adisesha.pdf
Software Engineering notes by K. Adisesha.pdfProf. Dr. K. Adisesha
 
Software Engineering-Unit 1 by Adisesha.pdf
Software Engineering-Unit 1 by Adisesha.pdfSoftware Engineering-Unit 1 by Adisesha.pdf
Software Engineering-Unit 1 by Adisesha.pdfProf. Dr. K. Adisesha
 
Software Engineering-Unit 2 "Requirement Engineering" by Adi.pdf
Software Engineering-Unit 2 "Requirement Engineering" by Adi.pdfSoftware Engineering-Unit 2 "Requirement Engineering" by Adi.pdf
Software Engineering-Unit 2 "Requirement Engineering" by Adi.pdfProf. Dr. K. Adisesha
 
Software Engineering-Unit 3 "System Modelling" by Adi.pdf
Software Engineering-Unit 3 "System Modelling" by Adi.pdfSoftware Engineering-Unit 3 "System Modelling" by Adi.pdf
Software Engineering-Unit 3 "System Modelling" by Adi.pdfProf. Dr. K. Adisesha
 
Software Engineering-Unit 4 "Architectural Design" by Adi.pdf
Software Engineering-Unit 4 "Architectural Design" by Adi.pdfSoftware Engineering-Unit 4 "Architectural Design" by Adi.pdf
Software Engineering-Unit 4 "Architectural Design" by Adi.pdfProf. Dr. K. Adisesha
 
Software Engineering-Unit 5 "Software Testing"by Adi.pdf
Software Engineering-Unit 5 "Software Testing"by Adi.pdfSoftware Engineering-Unit 5 "Software Testing"by Adi.pdf
Software Engineering-Unit 5 "Software Testing"by Adi.pdfProf. Dr. K. Adisesha
 
Computer Networks Notes by -Dr. K. Adisesha
Computer Networks Notes by -Dr. K. AdiseshaComputer Networks Notes by -Dr. K. Adisesha
Computer Networks Notes by -Dr. K. AdiseshaProf. Dr. K. Adisesha
 
CCN Unit-1&2 Data Communication &Networking by K. Adiaesha
CCN Unit-1&2 Data Communication &Networking by K. AdiaeshaCCN Unit-1&2 Data Communication &Networking by K. Adiaesha
CCN Unit-1&2 Data Communication &Networking by K. AdiaeshaProf. Dr. K. Adisesha
 
CCN Unit-3 Data Link Layer by Dr. K. Adisesha
CCN Unit-3 Data Link Layer by Dr. K. AdiseshaCCN Unit-3 Data Link Layer by Dr. K. Adisesha
CCN Unit-3 Data Link Layer by Dr. K. AdiseshaProf. Dr. K. Adisesha
 
CCN Unit-4 Network Layer by Dr. K. Adisesha
CCN Unit-4 Network Layer by Dr. K. AdiseshaCCN Unit-4 Network Layer by Dr. K. Adisesha
CCN Unit-4 Network Layer by Dr. K. AdiseshaProf. Dr. K. Adisesha
 
CCN Unit-5 Transport & Application Layer by Adi.pdf
CCN Unit-5 Transport & Application Layer by Adi.pdfCCN Unit-5 Transport & Application Layer by Adi.pdf
CCN Unit-5 Transport & Application Layer by Adi.pdfProf. Dr. K. Adisesha
 

More from Prof. Dr. K. Adisesha (20)

Software Engineering notes by K. Adisesha.pdf
Software Engineering notes by K. Adisesha.pdfSoftware Engineering notes by K. Adisesha.pdf
Software Engineering notes by K. Adisesha.pdf
 
Software Engineering-Unit 1 by Adisesha.pdf
Software Engineering-Unit 1 by Adisesha.pdfSoftware Engineering-Unit 1 by Adisesha.pdf
Software Engineering-Unit 1 by Adisesha.pdf
 
Software Engineering-Unit 2 "Requirement Engineering" by Adi.pdf
Software Engineering-Unit 2 "Requirement Engineering" by Adi.pdfSoftware Engineering-Unit 2 "Requirement Engineering" by Adi.pdf
Software Engineering-Unit 2 "Requirement Engineering" by Adi.pdf
 
Software Engineering-Unit 3 "System Modelling" by Adi.pdf
Software Engineering-Unit 3 "System Modelling" by Adi.pdfSoftware Engineering-Unit 3 "System Modelling" by Adi.pdf
Software Engineering-Unit 3 "System Modelling" by Adi.pdf
 
Software Engineering-Unit 4 "Architectural Design" by Adi.pdf
Software Engineering-Unit 4 "Architectural Design" by Adi.pdfSoftware Engineering-Unit 4 "Architectural Design" by Adi.pdf
Software Engineering-Unit 4 "Architectural Design" by Adi.pdf
 
Software Engineering-Unit 5 "Software Testing"by Adi.pdf
Software Engineering-Unit 5 "Software Testing"by Adi.pdfSoftware Engineering-Unit 5 "Software Testing"by Adi.pdf
Software Engineering-Unit 5 "Software Testing"by Adi.pdf
 
Computer Networks Notes by -Dr. K. Adisesha
Computer Networks Notes by -Dr. K. AdiseshaComputer Networks Notes by -Dr. K. Adisesha
Computer Networks Notes by -Dr. K. Adisesha
 
CCN Unit-1&2 Data Communication &Networking by K. Adiaesha
CCN Unit-1&2 Data Communication &Networking by K. AdiaeshaCCN Unit-1&2 Data Communication &Networking by K. Adiaesha
CCN Unit-1&2 Data Communication &Networking by K. Adiaesha
 
CCN Unit-3 Data Link Layer by Dr. K. Adisesha
CCN Unit-3 Data Link Layer by Dr. K. AdiseshaCCN Unit-3 Data Link Layer by Dr. K. Adisesha
CCN Unit-3 Data Link Layer by Dr. K. Adisesha
 
CCN Unit-4 Network Layer by Dr. K. Adisesha
CCN Unit-4 Network Layer by Dr. K. AdiseshaCCN Unit-4 Network Layer by Dr. K. Adisesha
CCN Unit-4 Network Layer by Dr. K. Adisesha
 
CCN Unit-5 Transport & Application Layer by Adi.pdf
CCN Unit-5 Transport & Application Layer by Adi.pdfCCN Unit-5 Transport & Application Layer by Adi.pdf
CCN Unit-5 Transport & Application Layer by Adi.pdf
 
Introduction to Computers.pdf
Introduction to Computers.pdfIntroduction to Computers.pdf
Introduction to Computers.pdf
 
R_Programming.pdf
R_Programming.pdfR_Programming.pdf
R_Programming.pdf
 
Scholarship.pdf
Scholarship.pdfScholarship.pdf
Scholarship.pdf
 
Operating System-2 by Adi.pdf
Operating System-2 by Adi.pdfOperating System-2 by Adi.pdf
Operating System-2 by Adi.pdf
 
Operating System-1 by Adi.pdf
Operating System-1 by Adi.pdfOperating System-1 by Adi.pdf
Operating System-1 by Adi.pdf
 
Operating System-adi.pdf
Operating System-adi.pdfOperating System-adi.pdf
Operating System-adi.pdf
 
Data_structure using C-Adi.pdf
Data_structure using C-Adi.pdfData_structure using C-Adi.pdf
Data_structure using C-Adi.pdf
 
JAVA PPT -2 BY ADI.pdf
JAVA PPT -2 BY ADI.pdfJAVA PPT -2 BY ADI.pdf
JAVA PPT -2 BY ADI.pdf
 
JAVA PPT -5 BY ADI.pdf
JAVA PPT -5 BY ADI.pdfJAVA PPT -5 BY ADI.pdf
JAVA PPT -5 BY ADI.pdf
 

Recently uploaded

Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Celine George
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...JhezDiaz1
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaVirag Sontakke
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementmkooblal
 
Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...jaredbarbolino94
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Jisc
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Celine George
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfUjwalaBharambe
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
CELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxCELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxJiesonDelaCerna
 
Meghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentMeghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentInMediaRes1
 

Recently uploaded (20)

Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of India
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of management
 
Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17
 
OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
CELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxCELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptx
 
Meghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentMeghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media Component
 

Constructors & Destructors: Initialization and Memory Management in C

  • 2. Learning Outcomes  Introduction  Defining Constructors  Characteristics of Constructor  Types of constructor  Constructor Overloading  Destructors  Characteristics of destructor 2
  • 3. Introduction Introduction  It is sometimes convenient if an object can initialize itself when it is first created, without the need to make a separate call to member functions.  Automatic initialization is carried out using special member functions called constructors.  Automatic de-allocating of memory when an object of that class is destroyed is carried out using special member functions called destructor. 3
  • 4. Constructors Constructors:  A Constructor is a special member function that is called automatically when an object is created.  The purpose of a constructor is to mainly initialize the member variables of a class.  The general syntax of a the constructor in C++ is: class MyClass // The class { public: // Access specifier MyClass() // Constructor { cout << "Hello World!"; } }; 4
  • 5. Constructors Characteristics of Constructor:  The name of the constructor is the same as the name of the class.  A Constructor, even though it is a function, has no return type function nor a void function.  The constructor should be declared in the public section.  Constructors are executed automatically.  They are executed when a class object is created.  A class can have more than one constructor.  It is not possible to refer to the address of the constructors.  The constructors make implicit calls to the operator new and delete when memory allocation is required. 5
  • 6. Constructors Need for a Constructor:  Constructors are named as constructors because they are getting called when an object is constructed.  The use of a constructor can be cleverly done especially in those problems where it is necessary to initialize certain data members compulsorily.  Instead of having separate member functions for initializing we can perform those operations inside the constructor itself. 6
  • 7. Constructors Types of constructor:  Constructors are normally classified as follows:  Default Constructors.  Parameterized Constructors.  Copy Constructors. 7
  • 8. Constructors Default Constructors:  A default constructor is a special member function which is invoked by the C++ compiler without any argument for initializing the object of a class.  It is also called as zero argument constructors.  Some of the features of the default constructors are:  A default constructor function initializes the data member with no argument.  It can be explicitly written in the public section of the class.  In case, default constructor is not defined in a program, the C++ compiler automatically generates it in a program.  The purpose of the default constructor is to construct a default object of the class type. 8
  • 9. Constructors Default Constructors:  The general format of default constructor is as follows. 9 Syntax: class Class_Name { public: Class_Name( ) { ……. } }; Example: class Number { public: Number( ) { n = 0; } };
  • 10. Constructors Disadvantages of default constructors are:  When many objects of the same class are created, all objects are initialized to same set of values by default constructors.  It is not possible to initialize different objects with different initial values using default constructors. 10
  • 11. Constructors Parameterized Constructors:  A constructor that takes one or more arguments is called parameterized constructor.  Using this constructor, it is possible to initialize different objects with different values.  Parameterized constructors are also invoked automatically, whenever objects with arguments are created.  Some of the features of the parameterized constructors are:  The parameterized constructors can be overloaded.  For an object created with one argument, constructor with only one argument is invoked and executed.  The parameterized constructor can have default arguments and default values. 11
  • 12. Constructors Parameterized Constructors:  The general format of Parameterized constructor is as follows. 12 Syntax: class Class_Name { public: Class_Name(arg1, arg2) { ……. } }; Example: class Number { public: Number(int a, int b ) { if (a > b) big = a; else big = b; } };
  • 13. Constructors Copy Constructors:  Copy constructor is a parameterized constructor using one object can be copied to another object.  Copy Constructors are used in the following situations:  To initialize an object with the values of already existing objects.  When objects must be returned as function values.  To state objects as by value parameters of a function.  Copy Constructor can accept a single argument of reference to same class type.  The argument must be passed as a constant reference type. 13
  • 14. Constructors Copy Constructors:  The general format of copy constructor is as follows. 14 Syntax: class Class_Name { public: Class_Name( Class_Name &ptr ) { ……. } }; Example: class Number { public: Number(int n) { a = n; } Number(Number &X) { a = X.a; cout<<”Copy Constructor invoked”; } };
  • 15. Constructors Invoking Constructors  A Constructor is automatically invoked by C++ compiler with an object declaration.  The constructor can be invoked through the following methods.  Implicit Call  Explicit Call  Initialization with “ = ” Assignment operator 15
  • 16. Invoking Constructors Implicit Call:  An Implicit call means the declaration of the object is followed by argument list enclosed in parenthesis.  Syntax: Class_name Object(Arg1, Arg2,…)  Example: num obj1(10, 20); //Implicit Call num obj2(40, 50); //Implicit Call 16
  • 17. Invoking Constructors Explicit Call:  In explicit call, declaration of an object is followed by assignment operator, constructor name and argument list enclosed in parenthesis.  Syntax: Class_name Object=Constructor_name(Arg1, Arg2,…)  Example: num obj1 = num(10, 20); //Explicit Call num obj2 = num(40, 50); //Explicit Call 17
  • 18. Invoking Constructors Initialization with “ = ” Assignment operator:  This method is used for the constructor with exactly one argument.  In this method declaration is followed by assignment operator and value to be initialized..  Syntax: Class_name Object=Arg1  Example: num obj1 = 100 num obj2 = 200 18
  • 19. Invoking Constructors #include<iostream.h> #include<conio.h> class num { private: int a, b; public: num ( int m) // Constructor { a = m; b=m; } num (int m, int n) // Constructor { a = m; b = n; } void display( ) { cout<<” a = “ << a <<” b = “ << b; } }; void main( ) { num obj1(10, 20); //Implicit Call num obj2=num(30, 40); //Explicit Call Num obj3=50; //Assignment operator obj1.display( ); obj2.display( ); Obj3.display( ); } 19 Program to initialize objects using Invoking Constructors
  • 20. Constructor Constructor Overloading:  A class has two or more constructor functions with the same name but different signatures are called Constructors Overloading.  Depending upon the type of argument, the constructors will be invoked automatically by the compiler to initialize the objects.  Example: class num { public: num ( ); // Constructor with no agruments num ( int m); // Constructor with one agrument num (int m, int n); // Constructor with two agruments }; 20
  • 21. Destructors Destructors:  A destructor is special member function that is executed when an object of that class is destroyed.  Destroying an object means, de-allocating all the resources such as memory that was allocated for the object by the constructor.  It will have like constructor, the name same as that of the class but preceded by a tilde (~). 21
  • 22. Destructors Destructors:  The general format of destructor is as follows. 22 Syntax: class Class_Name { public: Class_Name( ); ~ Class_Name( ); }; Example: class Counter { public: Counter( ) //Constructor { n = 0; } ~Counter ( ) //Destructor { } };
  • 23. Destructors Characteristics of destructor:  The destructor name must have the same name as the class preceded by a tilde (~).  The destructor cannot take arguments therefore cannot be overloaded.  The destructor has no return type.  There can be only one destructor in each class.  In should have public access in the class declaration.  The destructor cannot be inherited. 23
  • 24. Discussion Questions:  What is Constructor? Give the rules for writing a constructor function.  What is default constructor? Write a program to illustrate it.  Explain parameterized constructor with syntax and example.  Mention the different methods by which constructor are invoked. Explain anyone with an illustrative example.  Explain the features of copy constructor.  Explain destructor with syntax and example. 24