SlideShare a Scribd company logo
Name:- Sunipa Bera
Subject: Object Oriented Programing (OOP)
B. Tech CSE(CSF)
Roll no:- 17
PRN No.: 190105181016
Topic:-Constructor and Destructor
Constructor and Destructor
• Constructor
- It is a member function with the same name as its class is called
Constructor and it is used to initialize the objects of that class type
with a legal initial.
• Destructor
- A Destructor is also a member function whose name is the same as the
class name but is preceded by tilde (‘~’). A Destructor , as the name
itself suggests, is used to destroy the objects that have been created by a
constructor.
Special Characteristics of Constructor
• Their functions are invoked automatically when the object is created.
• If a class has a constructor, each object of that class will be initialized before any use is made of the object.
• Constructor functions obey the usual access rules.
• No return type(not even void) can be specified for a constructor.
• They cannot be inherited, though a derived class can call the base class constructor.
• A constructor may not be static.
• Constructor can belong to one of these categories: Default Constructors or Parameterized Constructor or Copy Constructor.
• It is not possible to take the address of a constructor.
• An object of a class with a constructor cannot be a member of a union.
• Member functions may be called from within a constructor.
• A constructor can be used explicitly to create new objects of its class type, using the syntax:
class- name(expression- list)
For example: sample obj1=sample(13,22,42);
Special Characteristics of Destructor
• Destructor functions are invoked automatically when the objects are destroyed.
• You can have only one destructor for a class. In other word, destructors can’t be overloaded.
• If a class has a destructor, each object of that class will be deinitialized before the object goes out of scope. ( local objects
at the end of the block defining them and global and static objects at the end of the program.)
• Destructor functions also, obey the usual access rules as other members do.
• No argument can be provided to a destructor, neither does it return any value.
• They cannot be inherited.
• A destructor may not be static.
• It is not possible to take the address of a destructor
• Member functions may be called from within a destructor.
• An object of a class with a destructor cannot be a member of a union.
Similarities
• Both Constructor and Destructor are associated with objects.
• Both Constructor and Destructor do not return any value.
• Both Constructor and Destructor are called automatically.
Dissimilarities
Basis of comparison Construction Destruction
Purpose It allocates the memory to an object. It deallocates the memory of an object.
Declaration class_name(arguments if any){ }; ~class_name(no arguments){ };
Arguments Constructor accepts arguments Destructor does not accept any arguments.
Calling
Constructor allows an object to initialize some of its
value before, it is used.
Destructor is called automatically, as blocked is
exited or program terminates.
Working
Construction allows an object to initialize some of
its value before, it is used.
Destructor allows an object to executes some
code at the time of its destruction .
Order of Execution Constructor are called in successive order.
Destructor are called in reverse order of
constructor.
In numbers There can be multiple constructors in a class. There is always a single destructor in the class.
Copy Constructor
Copy constructor allows a constructor allows a
constructor to declare and initialize a object from
another object.
No such Concept.
Over loading Constructors can be overloaded. Destructor can not be overloaded.
Default Constructors
• A constructor that accepts no parameter is called the Default Constructor.
• It is automatically invoked when an object.
• Default constructor is the constructor which doesn’t take any argument. It has no parameters.
Program:
// C++ program to illustrate the concept of Constructors
#include <iostream>
using namespace std;
class construct
{
public:
int a, b;
construct() //Default Constructor
{
a = 10;
b = 20;
}
};
int main()
{
construct c; /*Default constructor called automatically when the object is created*/
cout << "a: " << c.a << endl<< "b: " << c.b;
return 1;
}
Parameterized Constructor
• It is possible to pass arguments to constructors.
• Typically, these arguments help initialize an object when it is created.
• 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.
Program:
// CPP program to illustrate parameterized constructors
#include <iostream>
using namespace std;
class Point
{
private:
int x, y;
public:
Point(int x1, int y1) // Parameterized Constructor
{
x = x1;
y = y1;
}
int getX()
{
return x;
}
int getY()
{
return y;
}
};
int main()
{
Point p1(10, 15); // Constructor called
cout << "p1.x = " << p1.getX() << ", p1.y = " << p1.getY();
/*Access values assigned by constructor*/
return 0;
}
Output:
p1.x=10, p1.y = 15
Copy Constructor
• A copy constructor is a member function which initializes an object using another object of the same class.
• A copy constructor has the following general function Syntax:
ClassName (const ClassName &old_obj);
Program:
#include<iostream>
using namespace std;
class Point
{
private:
int x, y;
public:
Point(int x1, int y1)
{
x = x1;
y = y1;
}
Point(const Point &p2) // Copy constructor
{
x = p2.x;
y = p2.y;
}
int getX()
{
return x;
}
int getY()
{
return y;
}
};
int main()
{
Point p1(10, 15); // Normal constructor is called here
Point p2 = p1; // Copy constructor is called here
// Let us access values assigned by constructors
cout << "p1.x = " << p1.getX() << ", p1.y = " << p1.getY();
cout << "np2.x = " << p2.getX() << ", p2.y = " << p2.getY();
return 0;
}
Output:
p1.x = 10, p1.y = 15
p2.x = 10, p2.y = 15
Destructors
• Destructor is a member function which destructs or deletes an object.
• Syntax: ~constructor-name();
Program:
class String
{
private:
char* s;
int size;
public:
String(char*); // constructor
~String(); // destructor
};
String::String(char* c)
{
size = strlen(c);
s = new char[size + 1];
strcpy(s, c);
}
String::~String() { delete[] s; }
Bibliography
- Developerinsider
- Github
- W3school
Note:- Mentioned program in the presentation can be runnable only by using proper header files and logic.

More Related Content

What's hot

[OOP - Lec 19] Static Member Functions
[OOP - Lec 19] Static Member Functions[OOP - Lec 19] Static Member Functions
[OOP - Lec 19] Static Member Functions
Muhammad Hammad Waseem
 
constructors in java ppt
constructors in java pptconstructors in java ppt
constructors in java ppt
kunal kishore
 
Constructor in java
Constructor in javaConstructor in java
Constructor in java
Pavith Gunasekara
 
Constructors and destructors
Constructors and destructorsConstructors and destructors
Constructors and destructors
Prof. Dr. K. Adisesha
 
Constructor and Types of Constructors
Constructor and Types of ConstructorsConstructor and Types of Constructors
Constructor and Types of Constructors
Dhrumil Panchal
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
Vishal Patil
 
[OOP - Lec 01] Introduction to OOP
[OOP - Lec 01] Introduction to OOP[OOP - Lec 01] Introduction to OOP
[OOP - Lec 01] Introduction to OOP
Muhammad Hammad Waseem
 
Inheritance in JAVA PPT
Inheritance  in JAVA PPTInheritance  in JAVA PPT
Inheritance in JAVA PPT
Pooja Jaiswal
 
Unit 9. Structure and Unions
Unit 9. Structure and UnionsUnit 9. Structure and Unions
Unit 9. Structure and Unions
Ashim Lamichhane
 
Constructor and destructor
Constructor  and  destructor Constructor  and  destructor
Constructor and destructor
Shubham Vishwambhar
 
Basic concept of OOP's
Basic concept of OOP'sBasic concept of OOP's
Basic concept of OOP's
Prof. Dr. K. Adisesha
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
Vineeta Garg
 
Inline function in C++
Inline function in C++Inline function in C++
Inline function in C++
Learn By Watch
 
This pointer
This pointerThis pointer
This pointer
Kamal Acharya
 
Constructors and Destructor in C++
Constructors and Destructor in C++Constructors and Destructor in C++
inheritance c++
inheritance c++inheritance c++
inheritance c++
Muraleedhar Sundararajan
 
Constructor and Destructor
Constructor and DestructorConstructor and Destructor
Constructor and DestructorKamal Acharya
 
C++: Constructor, Copy Constructor and Assignment operator
C++: Constructor, Copy Constructor and Assignment operatorC++: Constructor, Copy Constructor and Assignment operator
C++: Constructor, Copy Constructor and Assignment operatorJussi Pohjolainen
 
concept of oops
concept of oopsconcept of oops
concept of oops
prince sharma
 

What's hot (20)

[OOP - Lec 19] Static Member Functions
[OOP - Lec 19] Static Member Functions[OOP - Lec 19] Static Member Functions
[OOP - Lec 19] Static Member Functions
 
constructors in java ppt
constructors in java pptconstructors in java ppt
constructors in java ppt
 
Constructor in java
Constructor in javaConstructor in java
Constructor in java
 
Constructors and destructors
Constructors and destructorsConstructors and destructors
Constructors and destructors
 
Constructor and Types of Constructors
Constructor and Types of ConstructorsConstructor and Types of Constructors
Constructor and Types of Constructors
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
 
[OOP - Lec 01] Introduction to OOP
[OOP - Lec 01] Introduction to OOP[OOP - Lec 01] Introduction to OOP
[OOP - Lec 01] Introduction to OOP
 
Inheritance in JAVA PPT
Inheritance  in JAVA PPTInheritance  in JAVA PPT
Inheritance in JAVA PPT
 
Arrays in Java
Arrays in JavaArrays in Java
Arrays in Java
 
Unit 9. Structure and Unions
Unit 9. Structure and UnionsUnit 9. Structure and Unions
Unit 9. Structure and Unions
 
Constructor and destructor
Constructor  and  destructor Constructor  and  destructor
Constructor and destructor
 
Basic concept of OOP's
Basic concept of OOP'sBasic concept of OOP's
Basic concept of OOP's
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
 
Inline function in C++
Inline function in C++Inline function in C++
Inline function in C++
 
This pointer
This pointerThis pointer
This pointer
 
Constructors and Destructor in C++
Constructors and Destructor in C++Constructors and Destructor in C++
Constructors and Destructor in C++
 
inheritance c++
inheritance c++inheritance c++
inheritance c++
 
Constructor and Destructor
Constructor and DestructorConstructor and Destructor
Constructor and Destructor
 
C++: Constructor, Copy Constructor and Assignment operator
C++: Constructor, Copy Constructor and Assignment operatorC++: Constructor, Copy Constructor and Assignment operator
C++: Constructor, Copy Constructor and Assignment operator
 
concept of oops
concept of oopsconcept of oops
concept of oops
 

Similar to Constructor and Destructor

Constructor & destructor
Constructor & destructorConstructor & destructor
Constructor & destructor
Saharsh Anand
 
Constructor & Destructor
Constructor & DestructorConstructor & Destructor
Constructor & Destructor
KV(AFS) Utarlai, Barmer (Rajasthan)
 
C++ Constructor and Destructors.pptx
C++ Constructor and Destructors.pptxC++ Constructor and Destructors.pptx
C++ Constructor and Destructors.pptx
sasukeman
 
Constructors in C++.pptx
Constructors in C++.pptxConstructors in C++.pptx
Constructors in C++.pptx
Rassjb
 
Constructor and desturctor
Constructor and desturctorConstructor and desturctor
Constructor and desturctorSomnath Kulkarni
 
Constructors and Destructors
Constructors and DestructorsConstructors and Destructors
Constructors and Destructors
murugeswariSenthilku
 
Constructor and destructor in C++
Constructor and destructor in C++Constructor and destructor in C++
Constructor and destructor in C++
Lovely Professional University
 
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
Lovely Professional University
 
Constructors and Destructors
Constructors and DestructorsConstructors and Destructors
Constructors and Destructors
Keyur Vadodariya
 
Constructor and Destructor.pdf
Constructor and Destructor.pdfConstructor and Destructor.pdf
Constructor and Destructor.pdf
MadnessKnight
 
C++
C++C++
C++
C++C++
Constructor and Destructor in c++
Constructor  and Destructor in c++Constructor  and Destructor in c++
Constructor and Destructor in c++
aleenaguen
 
constructor in object oriented program.pptx
constructor in object oriented program.pptxconstructor in object oriented program.pptx
constructor in object oriented program.pptx
urvashipundir04
 
ConsTRUCTION AND DESTRUCTION
ConsTRUCTION AND DESTRUCTIONConsTRUCTION AND DESTRUCTION
ConsTRUCTION AND DESTRUCTION
Shweta Shah
 
Constructors & Destructors
Constructors  & DestructorsConstructors  & Destructors
Constructors & Destructors
Rokonuzzaman Rony
 
Oops
OopsOops
Constructor and destructor
Constructor and destructorConstructor and destructor
Constructor and destructor
rajshreemuthiah
 
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCECONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
Venugopalavarma Raja
 
Constructor and destructor in oop
Constructor and destructor in oop Constructor and destructor in oop
Constructor and destructor in oop
Samad Qazi
 

Similar to Constructor and Destructor (20)

Constructor & destructor
Constructor & destructorConstructor & destructor
Constructor & destructor
 
Constructor & Destructor
Constructor & DestructorConstructor & Destructor
Constructor & Destructor
 
C++ Constructor and Destructors.pptx
C++ Constructor and Destructors.pptxC++ Constructor and Destructors.pptx
C++ Constructor and Destructors.pptx
 
Constructors in C++.pptx
Constructors in C++.pptxConstructors in C++.pptx
Constructors in C++.pptx
 
Constructor and desturctor
Constructor and desturctorConstructor and desturctor
Constructor and desturctor
 
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
 
Constructors and Destructors
Constructors and DestructorsConstructors and Destructors
Constructors and Destructors
 
Constructor and Destructor.pdf
Constructor and Destructor.pdfConstructor and Destructor.pdf
Constructor and Destructor.pdf
 
C++
C++C++
C++
 
C++
C++C++
C++
 
Constructor and Destructor in c++
Constructor  and Destructor in c++Constructor  and Destructor in c++
Constructor and Destructor in c++
 
constructor in object oriented program.pptx
constructor in object oriented program.pptxconstructor in object oriented program.pptx
constructor in object oriented program.pptx
 
ConsTRUCTION AND DESTRUCTION
ConsTRUCTION AND DESTRUCTIONConsTRUCTION AND DESTRUCTION
ConsTRUCTION AND DESTRUCTION
 
Constructors & Destructors
Constructors  & DestructorsConstructors  & Destructors
Constructors & Destructors
 
Oops
OopsOops
Oops
 
Constructor and destructor
Constructor and destructorConstructor and destructor
Constructor and destructor
 
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCECONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
 
Constructor and destructor in oop
Constructor and destructor in oop Constructor and destructor in oop
Constructor and destructor in oop
 

More from Sunipa Bera

hackers.pptx
hackers.pptxhackers.pptx
hackers.pptx
Sunipa Bera
 
Ds stack & queue
Ds   stack & queueDs   stack & queue
Ds stack & queue
Sunipa Bera
 
System tThreats
System tThreatsSystem tThreats
System tThreats
Sunipa Bera
 
Proposition & Logical Operations
Proposition & Logical OperationsProposition & Logical Operations
Proposition & Logical Operations
Sunipa Bera
 
Integral Calculas
Integral CalculasIntegral Calculas
Integral Calculas
Sunipa Bera
 
Basic concept of Engineering Mechanics
Basic concept of Engineering MechanicsBasic concept of Engineering Mechanics
Basic concept of Engineering Mechanics
Sunipa Bera
 
Biogeographical zones of India
Biogeographical zones of IndiaBiogeographical zones of India
Biogeographical zones of India
Sunipa Bera
 
Inflation
InflationInflation
Inflation
Sunipa Bera
 
Operating System & Application Security
Operating System & Application SecurityOperating System & Application Security
Operating System & Application Security
Sunipa Bera
 
Types of Ecosystem
Types of EcosystemTypes of Ecosystem
Types of Ecosystem
Sunipa Bera
 
Elementary transformation
Elementary transformationElementary transformation
Elementary transformation
Sunipa Bera
 
Emotional Intelligence
Emotional IntelligenceEmotional Intelligence
Emotional Intelligence
Sunipa Bera
 
SKYDRIVE
SKYDRIVESKYDRIVE
SKYDRIVE
Sunipa Bera
 
7 cs of effective communication
7 cs of effective communication 7 cs of effective communication
7 cs of effective communication
Sunipa Bera
 
Basics of microprocessor
Basics of microprocessorBasics of microprocessor
Basics of microprocessor
Sunipa Bera
 
Functioning of computer
Functioning of computerFunctioning of computer
Functioning of computer
Sunipa Bera
 
INDIAN ISLAND STUDIES
INDIAN ISLAND STUDIESINDIAN ISLAND STUDIES
INDIAN ISLAND STUDIES
Sunipa Bera
 
GO GREEN - GO ECOFRIENDLY
GO GREEN - GO ECOFRIENDLYGO GREEN - GO ECOFRIENDLY
GO GREEN - GO ECOFRIENDLY
Sunipa Bera
 
INDIA AND HERITAGE
INDIA AND HERITAGEINDIA AND HERITAGE
INDIA AND HERITAGE
Sunipa Bera
 
Happy birthday.rtf
Happy birthday.rtfHappy birthday.rtf
Happy birthday.rtf
Sunipa Bera
 

More from Sunipa Bera (20)

hackers.pptx
hackers.pptxhackers.pptx
hackers.pptx
 
Ds stack & queue
Ds   stack & queueDs   stack & queue
Ds stack & queue
 
System tThreats
System tThreatsSystem tThreats
System tThreats
 
Proposition & Logical Operations
Proposition & Logical OperationsProposition & Logical Operations
Proposition & Logical Operations
 
Integral Calculas
Integral CalculasIntegral Calculas
Integral Calculas
 
Basic concept of Engineering Mechanics
Basic concept of Engineering MechanicsBasic concept of Engineering Mechanics
Basic concept of Engineering Mechanics
 
Biogeographical zones of India
Biogeographical zones of IndiaBiogeographical zones of India
Biogeographical zones of India
 
Inflation
InflationInflation
Inflation
 
Operating System & Application Security
Operating System & Application SecurityOperating System & Application Security
Operating System & Application Security
 
Types of Ecosystem
Types of EcosystemTypes of Ecosystem
Types of Ecosystem
 
Elementary transformation
Elementary transformationElementary transformation
Elementary transformation
 
Emotional Intelligence
Emotional IntelligenceEmotional Intelligence
Emotional Intelligence
 
SKYDRIVE
SKYDRIVESKYDRIVE
SKYDRIVE
 
7 cs of effective communication
7 cs of effective communication 7 cs of effective communication
7 cs of effective communication
 
Basics of microprocessor
Basics of microprocessorBasics of microprocessor
Basics of microprocessor
 
Functioning of computer
Functioning of computerFunctioning of computer
Functioning of computer
 
INDIAN ISLAND STUDIES
INDIAN ISLAND STUDIESINDIAN ISLAND STUDIES
INDIAN ISLAND STUDIES
 
GO GREEN - GO ECOFRIENDLY
GO GREEN - GO ECOFRIENDLYGO GREEN - GO ECOFRIENDLY
GO GREEN - GO ECOFRIENDLY
 
INDIA AND HERITAGE
INDIA AND HERITAGEINDIA AND HERITAGE
INDIA AND HERITAGE
 
Happy birthday.rtf
Happy birthday.rtfHappy birthday.rtf
Happy birthday.rtf
 

Recently uploaded

Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
Jisc
 
The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
kaushalkr1407
 
Introduction to Quality Improvement Essentials
Introduction to Quality Improvement EssentialsIntroduction to Quality Improvement Essentials
Introduction to Quality Improvement Essentials
Excellence Foundation for South Sudan
 
Polish students' mobility in the Czech Republic
Polish students' mobility in the Czech RepublicPolish students' mobility in the Czech Republic
Polish students' mobility in the Czech Republic
Anna Sz.
 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
MIRIAMSALINAS13
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
Celine George
 
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
Nguyen Thanh Tu Collection
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
siemaillard
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
Sandy Millin
 
Cambridge International AS A Level Biology Coursebook - EBook (MaryFosbery J...
Cambridge International AS  A Level Biology Coursebook - EBook (MaryFosbery J...Cambridge International AS  A Level Biology Coursebook - EBook (MaryFosbery J...
Cambridge International AS A Level Biology Coursebook - EBook (MaryFosbery J...
AzmatAli747758
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
Celine George
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
joachimlavalley1
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
MysoreMuleSoftMeetup
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
Jisc
 
Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptx
RaedMohamed3
 
Template Jadual Bertugas Kelas (Boleh Edit)
Template Jadual Bertugas Kelas (Boleh Edit)Template Jadual Bertugas Kelas (Boleh Edit)
Template Jadual Bertugas Kelas (Boleh Edit)
rosedainty
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
Jisc
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
beazzy04
 
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptxMARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
bennyroshan06
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
Vivekanand Anglo Vedic Academy
 

Recently uploaded (20)

Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
 
The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
 
Introduction to Quality Improvement Essentials
Introduction to Quality Improvement EssentialsIntroduction to Quality Improvement Essentials
Introduction to Quality Improvement Essentials
 
Polish students' mobility in the Czech Republic
Polish students' mobility in the Czech RepublicPolish students' mobility in the Czech Republic
Polish students' mobility in the Czech Republic
 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
 
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
 
Cambridge International AS A Level Biology Coursebook - EBook (MaryFosbery J...
Cambridge International AS  A Level Biology Coursebook - EBook (MaryFosbery J...Cambridge International AS  A Level Biology Coursebook - EBook (MaryFosbery J...
Cambridge International AS A Level Biology Coursebook - EBook (MaryFosbery J...
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
 
Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptx
 
Template Jadual Bertugas Kelas (Boleh Edit)
Template Jadual Bertugas Kelas (Boleh Edit)Template Jadual Bertugas Kelas (Boleh Edit)
Template Jadual Bertugas Kelas (Boleh Edit)
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
 
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptxMARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
 

Constructor and Destructor

  • 1. Name:- Sunipa Bera Subject: Object Oriented Programing (OOP) B. Tech CSE(CSF) Roll no:- 17 PRN No.: 190105181016 Topic:-Constructor and Destructor
  • 2. Constructor and Destructor • Constructor - It is a member function with the same name as its class is called Constructor and it is used to initialize the objects of that class type with a legal initial. • Destructor - A Destructor is also a member function whose name is the same as the class name but is preceded by tilde (‘~’). A Destructor , as the name itself suggests, is used to destroy the objects that have been created by a constructor.
  • 3. Special Characteristics of Constructor • Their functions are invoked automatically when the object is created. • If a class has a constructor, each object of that class will be initialized before any use is made of the object. • Constructor functions obey the usual access rules. • No return type(not even void) can be specified for a constructor. • They cannot be inherited, though a derived class can call the base class constructor. • A constructor may not be static. • Constructor can belong to one of these categories: Default Constructors or Parameterized Constructor or Copy Constructor. • It is not possible to take the address of a constructor. • An object of a class with a constructor cannot be a member of a union. • Member functions may be called from within a constructor. • A constructor can be used explicitly to create new objects of its class type, using the syntax: class- name(expression- list) For example: sample obj1=sample(13,22,42);
  • 4. Special Characteristics of Destructor • Destructor functions are invoked automatically when the objects are destroyed. • You can have only one destructor for a class. In other word, destructors can’t be overloaded. • If a class has a destructor, each object of that class will be deinitialized before the object goes out of scope. ( local objects at the end of the block defining them and global and static objects at the end of the program.) • Destructor functions also, obey the usual access rules as other members do. • No argument can be provided to a destructor, neither does it return any value. • They cannot be inherited. • A destructor may not be static. • It is not possible to take the address of a destructor • Member functions may be called from within a destructor. • An object of a class with a destructor cannot be a member of a union.
  • 5. Similarities • Both Constructor and Destructor are associated with objects. • Both Constructor and Destructor do not return any value. • Both Constructor and Destructor are called automatically.
  • 6. Dissimilarities Basis of comparison Construction Destruction Purpose It allocates the memory to an object. It deallocates the memory of an object. Declaration class_name(arguments if any){ }; ~class_name(no arguments){ }; Arguments Constructor accepts arguments Destructor does not accept any arguments. Calling Constructor allows an object to initialize some of its value before, it is used. Destructor is called automatically, as blocked is exited or program terminates. Working Construction allows an object to initialize some of its value before, it is used. Destructor allows an object to executes some code at the time of its destruction . Order of Execution Constructor are called in successive order. Destructor are called in reverse order of constructor. In numbers There can be multiple constructors in a class. There is always a single destructor in the class. Copy Constructor Copy constructor allows a constructor allows a constructor to declare and initialize a object from another object. No such Concept. Over loading Constructors can be overloaded. Destructor can not be overloaded.
  • 7. Default Constructors • A constructor that accepts no parameter is called the Default Constructor. • It is automatically invoked when an object. • Default constructor is the constructor which doesn’t take any argument. It has no parameters. Program: // C++ program to illustrate the concept of Constructors #include <iostream> using namespace std; class construct { public: int a, b; construct() //Default Constructor { a = 10; b = 20; } }; int main() { construct c; /*Default constructor called automatically when the object is created*/ cout << "a: " << c.a << endl<< "b: " << c.b; return 1; }
  • 8. Parameterized Constructor • It is possible to pass arguments to constructors. • Typically, these arguments help initialize an object when it is created. • 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. Program: // CPP program to illustrate parameterized constructors #include <iostream> using namespace std; class Point { private: int x, y; public: Point(int x1, int y1) // Parameterized Constructor { x = x1; y = y1; } int getX() { return x; } int getY() { return y; } }; int main() { Point p1(10, 15); // Constructor called cout << "p1.x = " << p1.getX() << ", p1.y = " << p1.getY(); /*Access values assigned by constructor*/ return 0; } Output: p1.x=10, p1.y = 15
  • 9. Copy Constructor • A copy constructor is a member function which initializes an object using another object of the same class. • A copy constructor has the following general function Syntax: ClassName (const ClassName &old_obj); Program: #include<iostream> using namespace std; class Point { private: int x, y; public: Point(int x1, int y1) { x = x1; y = y1; } Point(const Point &p2) // Copy constructor { x = p2.x; y = p2.y; } int getX() { return x; } int getY() { return y; } }; int main() { Point p1(10, 15); // Normal constructor is called here Point p2 = p1; // Copy constructor is called here // Let us access values assigned by constructors cout << "p1.x = " << p1.getX() << ", p1.y = " << p1.getY(); cout << "np2.x = " << p2.getX() << ", p2.y = " << p2.getY(); return 0; } Output: p1.x = 10, p1.y = 15 p2.x = 10, p2.y = 15
  • 10. Destructors • Destructor is a member function which destructs or deletes an object. • Syntax: ~constructor-name(); Program: class String { private: char* s; int size; public: String(char*); // constructor ~String(); // destructor }; String::String(char* c) { size = strlen(c); s = new char[size + 1]; strcpy(s, c); } String::~String() { delete[] s; }
  • 11. Bibliography - Developerinsider - Github - W3school Note:- Mentioned program in the presentation can be runnable only by using proper header files and logic.