SlideShare a Scribd company logo
Sarvajanik College Of
Engineering And Technology
Subject Name :- Object Oriented
Programming With C++
Subject Code :- 2140705
Branch :- Computer Engineering Eve. (COE)
1
Group Members And Topic
⮚ 170420107559 – Uttam Thummar
⮚ 170420107560 – Dhriti Uttamchandani
⮚ 170420107561 – Keyur Vadodariya
⮚ 170420107562 – Aditya Vaghela
⮚Topic:- Constructors & Destructors
⮚ Guided By :- Prof. Dipali Kasat
Prof. Jaydeep Barad
2
What Does Constructor Mean?
⮚ A constructor is a special method of a class or structure in object oriented
programming that initializes an object of that type.
⮚ A constructor is an instance method that usually has the same name an the
class.
⮚ A constructor can be used to set the values of the member of an object ,
either to default or to user-defined values.
Need For Constructor
⮚ It is very common for some part of an object to require initialization
before it can be used.
⮚ Suppose you are working on 100's of objects and the default value
of a particular data member is needed to be zero.
⮚ Initializing all objects manually will be very tedious job.
⮚ Instead, you can define a constructor function which initializes that
data member to zero.
⮚ Then all you have to do is declare object and constructor will
initialize object automatically.
Syntax Of Constructor
class classname
{
…
public:
classname( Arguments )
{
//Statements
}
};
Characteristics Of Constructor.
⮚ They should be declared in the public section.
⮚ They are invoked automatically when the object are created.
⮚ They do not have return type not even void and there for they cannot
return any values.
⮚ They cannot be inherited, a derived class can call the base class
constructor.
⮚ They make implicit calls to the operator new and delete when memory
allocation is required.
Two Ways For Calling a Constructor
⮚ Implicit call
Class circle
{
float radius;
Public:
circle()
{
radius = 0;
}
circle (float r)
{
radius = r;
}
};
Circle ob (7.6);
⮚ Explicit call
Circle ob;
Ob = circle(7.6);
Static Initialization Of Object
Class demo int main ( )
{ {
int pp; demo (2,3);
public ( ): return 0;
demo (int x , int y); }
{
a=x;
b=y;
}
};
Dynamic initialization of object
Class demo int main()
{ {
int a,b; int a,b;
public: cout<<“enter 2 no.”;
demo(int x,int y) cin>>a>>b;
{ demo obj (a,b);
a=x; }
b=y;
}
};
Types Of Constructors
⮚ There are basically three main types of constructors.
1. Default Constructor
▪ One which is Implicitly called, when an object is created.
2. Parameterized Constructor
▪ One which has arguments.
3. Copy Constructor
▪ One which copies one object to another.
10
Default Constructor
⮚ It is a constructor which is
implicitly called at time of
object creation.
⮚ It is automatically declared
and defined by the compiler
if not by the user.
⮚ This constructor has no
arguments and may or may
not have the body
statements.
⮚ So it is also called Non-Arg.
Constructor.
⮚Syntax :-
▪ By User
Classname(no arguments)
{
//Statements
}
▪ By Compiler
Classname(no arguments)
{
//Empty Body
}
11
Parameterized Constructor
⮚ Sometimes it becomes necessary
to initialize the various data
elements of an objects with
different values when they are
created.
⮚ This is achieved by passing
arguments to the constructor
function when the objects are
created.
⮚ The constructors that can take
arguments are called
parameterized constructors.
⮚Syntax :-
Classname(arguments)
{
//Statements
}
12
Copy Constructor
⮚ It is basically a constructor which
copies one object to another of the
same class.
⮚ The object which is to be copied is
passed as an argument o the
constructor.
⮚ The object in which the data is to
be copied is the object used to
calling constructor.
⮚Syntax :-
classname(classname &object)
{
var = object.var ;
}
13
Constructor Overloading
⮚ C++ permits the use of more than one Constructors in a class in a
single program.
⮚ Such a situation is called Constructor Overloading.
⮚Rules For Constructor Overloading :-
⮚ Constructors should have different no.’s of arguments.
⮚ Constructors should have different types of arguments.
14
Example Of Constructor Overloading
⮚ #include <iostream>
using namespace std;
class circle
{ int r;
public:
//Default Cons.
circle()
{
r = 0;
}
//Parameterized Cons.
circle(int x)
{ r = x;
}
//Copy Cons.
circle(circle &x)
{ r = x.r;
}
void display()
{ cout <<
r;
}
};
int main()
{ circle r1;
r1.display();
r1 = circle(20);
circle r2(r1);
r1.display();
r2.display();
}
⮚ Output :-
0
20
20
15
Dynamic Constructor
•The constructors can also be used to allocate memory while
creating objects.
•This will enable the system to allocate the right amount of memory
for each object when the objects are not of the same size.
•Allocation of memory to objects at the time of their construction is
known as dynamic construction of objects.
•The memory is created with the help of the new operator.
16
Destructors
● A destructor is used to destroy the objects that have been
created by a constructor.
● Like constructor, the destructor is a member function whose
name is the same as the class name but is preceded by a tilde .
eg: ~ integer ( ) { }
● Whenever new is used to allocate memory in the constructor,
we should use delete to free that memory.
● There can only be 1 destructor in a class.
17
Rules Of Destructors
● Destructors cannot be overloaded.
● Destructors take no arguments.
● Destructors don't return any value.
● Destructure cannot be inherited.
A destructor function is called automatically when the object goes out of
scope:
● Function ends
● Program ends
● A block containing local variables ends
● A delete operator is called
18
When is a Destructor called?
Example of New and Delete Operators
19
#include <iostream>
using namespace std;
class Test
{
private:
int n;
float *a;
public:
Test()
{
cout << "Enter n: ";
cin >> n;
a = new float[n];
cout << "Enter array
elements." << endl;
for (int i = 0; i < n; i++)
{
cout << i + 1<<”
element:”;
cin >> *(a + i);
}
}
~Test()
{ delete[] a; }
void Display()
{
cout << "nArray
Elements:" << endl;
for (int i = 0; i < n; i++)
cout << *(a + i) <<”t”;
}
};
int main() {
Test s;
s.Display();
}
⮚ Output :-
Enter n:3
Enter array
elements:
Enter 1 element:2
Enter 2 element:4
Enter 3 element:6
Array elements:
2 4 6
Limitations Of Constructors
⮚ They should only be declared in public section.
⮚ They don’t have any return type, not even void, so they do not return
any value.
⮚ They cannot be Inherited. However a Derived Class can call a Base
Class Constructor.
⮚ Name of the constructor must be same as that of class.
⮚ Constructors may not be Static as well as Virtual.
⮚ We cannot fetch the address of the Constructor Function.
20
Thank You21

More Related Content

What's hot

oops concept in java | object oriented programming in java
oops concept in java | object oriented programming in javaoops concept in java | object oriented programming in java
oops concept in java | object oriented programming in java
CPD INDIA
 
java Features
java Featuresjava Features
java Features
Jadavsejal
 
Templates in C++
Templates in C++Templates in C++
Templates in C++Tech_MX
 
Python: Basic Inheritance
Python: Basic InheritancePython: Basic Inheritance
Python: Basic Inheritance
Damian T. Gordon
 
Abstract data types
Abstract data typesAbstract data types
Abstract data types
Poojith Chowdhary
 
Array in Java
Array in JavaArray in Java
Array in Java
Ali shah
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
Ramish Suleman
 
Object Oriented Programming Concepts
Object Oriented Programming ConceptsObject Oriented Programming Concepts
Object Oriented Programming Conceptsthinkphp
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
Vineeta Garg
 
C++ OOPS Concept
C++ OOPS ConceptC++ OOPS Concept
C++ OOPS Concept
Boopathi K
 
Oop c++class(final).ppt
Oop c++class(final).pptOop c++class(final).ppt
Oop c++class(final).pptAlok Kumar
 
Structure in c
Structure in cStructure in c
Structure in c
Prabhu Govind
 
Java(Polymorphism)
Java(Polymorphism)Java(Polymorphism)
Java(Polymorphism)
harsh kothari
 
[OOP - Lec 04,05] Basic Building Blocks of OOP
[OOP - Lec 04,05] Basic Building Blocks of OOP[OOP - Lec 04,05] Basic Building Blocks of OOP
[OOP - Lec 04,05] Basic Building Blocks of OOP
Muhammad Hammad Waseem
 
Java threads
Java threadsJava threads
Java threads
Prabhakaran V M
 
Inheritance in JAVA PPT
Inheritance  in JAVA PPTInheritance  in JAVA PPT
Inheritance in JAVA PPT
Pooja Jaiswal
 
Function overloading
Function overloadingFunction overloading
Function overloading
Selvin Josy Bai Somu
 
Principles and advantages of oop ppt
Principles and advantages of oop pptPrinciples and advantages of oop ppt
Principles and advantages of oop ppt
daxesh chauhan
 
Pointers in C Programming
Pointers in C ProgrammingPointers in C Programming
Pointers in C Programming
Jasleen Kaur (Chandigarh University)
 

What's hot (20)

oops concept in java | object oriented programming in java
oops concept in java | object oriented programming in javaoops concept in java | object oriented programming in java
oops concept in java | object oriented programming in java
 
java Features
java Featuresjava Features
java Features
 
Templates in C++
Templates in C++Templates in C++
Templates in C++
 
Python: Basic Inheritance
Python: Basic InheritancePython: Basic Inheritance
Python: Basic Inheritance
 
Abstract data types
Abstract data typesAbstract data types
Abstract data types
 
Array in Java
Array in JavaArray in Java
Array in Java
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
 
Constructors & destructors
Constructors & destructorsConstructors & destructors
Constructors & destructors
 
Object Oriented Programming Concepts
Object Oriented Programming ConceptsObject Oriented Programming Concepts
Object Oriented Programming Concepts
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
 
C++ OOPS Concept
C++ OOPS ConceptC++ OOPS Concept
C++ OOPS Concept
 
Oop c++class(final).ppt
Oop c++class(final).pptOop c++class(final).ppt
Oop c++class(final).ppt
 
Structure in c
Structure in cStructure in c
Structure in c
 
Java(Polymorphism)
Java(Polymorphism)Java(Polymorphism)
Java(Polymorphism)
 
[OOP - Lec 04,05] Basic Building Blocks of OOP
[OOP - Lec 04,05] Basic Building Blocks of OOP[OOP - Lec 04,05] Basic Building Blocks of OOP
[OOP - Lec 04,05] Basic Building Blocks of OOP
 
Java threads
Java threadsJava threads
Java threads
 
Inheritance in JAVA PPT
Inheritance  in JAVA PPTInheritance  in JAVA PPT
Inheritance in JAVA PPT
 
Function overloading
Function overloadingFunction overloading
Function overloading
 
Principles and advantages of oop ppt
Principles and advantages of oop pptPrinciples and advantages of oop ppt
Principles and advantages of oop ppt
 
Pointers in C Programming
Pointers in C ProgrammingPointers in C Programming
Pointers in C Programming
 

Similar to Constructors and Destructors

Constructors in C++.pptx
Constructors in C++.pptxConstructors in C++.pptx
Constructors in C++.pptx
Rassjb
 
Lecture 3, c++(complete reference,herbet sheidt)chapter-13
Lecture 3, c++(complete reference,herbet sheidt)chapter-13Lecture 3, c++(complete reference,herbet sheidt)chapter-13
Lecture 3, c++(complete reference,herbet sheidt)chapter-13
Abu Saleh
 
Constructors & Destructors [Compatibility Mode].pdf
Constructors & Destructors [Compatibility Mode].pdfConstructors & Destructors [Compatibility Mode].pdf
Constructors & Destructors [Compatibility Mode].pdf
LadallaRajKumar
 
Constructors and destructors in C++
Constructors and destructors in  C++Constructors and destructors in  C++
Constructors and destructors in C++
RAJ KUMAR
 
CONSTRUCTORS, DESTRUCTORS AND OPERATOR OVERLOADING.pptx
CONSTRUCTORS, DESTRUCTORS AND OPERATOR OVERLOADING.pptxCONSTRUCTORS, DESTRUCTORS AND OPERATOR OVERLOADING.pptx
CONSTRUCTORS, DESTRUCTORS AND OPERATOR OVERLOADING.pptx
DeepasCSE
 
chapter-9-constructors.pdf
chapter-9-constructors.pdfchapter-9-constructors.pdf
chapter-9-constructors.pdf
study material
 
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
Constructor and DestructorConstructor and Destructor
Constructor and Destructor
Sunipa Bera
 
Object Oriented Design and Programming Unit-02
Object Oriented Design and Programming Unit-02Object Oriented Design and Programming Unit-02
Object Oriented Design and Programming Unit-02
sivakumarmcs
 
constructors and destructors
constructors and destructorsconstructors and destructors
constructors and destructors
Akshaya Parida
 
Learning C++ - Class 4
Learning C++ - Class 4Learning C++ - Class 4
Learning C++ - Class 4
Ali Aminian
 
Oops lecture 1
Oops lecture 1Oops lecture 1
Oops lecture 1
rehan16091997
 
Classes
ClassesClasses
Classes
Swarup Boro
 
Lecture 4.2 c++(comlete reference book)
Lecture 4.2 c++(comlete reference book)Lecture 4.2 c++(comlete reference book)
Lecture 4.2 c++(comlete reference book)
Abu Saleh
 
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
 
Unit 1 Part - 3 constructor Overloading Static.ppt
Unit 1 Part - 3  constructor Overloading Static.pptUnit 1 Part - 3  constructor Overloading Static.ppt
Unit 1 Part - 3 constructor Overloading Static.ppt
DeepVala5
 
C#2
C#2C#2

Similar to Constructors and Destructors (20)

Constructors in C++.pptx
Constructors in C++.pptxConstructors in C++.pptx
Constructors in C++.pptx
 
Lecture 3, c++(complete reference,herbet sheidt)chapter-13
Lecture 3, c++(complete reference,herbet sheidt)chapter-13Lecture 3, c++(complete reference,herbet sheidt)chapter-13
Lecture 3, c++(complete reference,herbet sheidt)chapter-13
 
Constructors & Destructors [Compatibility Mode].pdf
Constructors & Destructors [Compatibility Mode].pdfConstructors & Destructors [Compatibility Mode].pdf
Constructors & Destructors [Compatibility Mode].pdf
 
Constructors and destructors in C++
Constructors and destructors in  C++Constructors and destructors in  C++
Constructors and destructors in C++
 
CONSTRUCTORS, DESTRUCTORS AND OPERATOR OVERLOADING.pptx
CONSTRUCTORS, DESTRUCTORS AND OPERATOR OVERLOADING.pptxCONSTRUCTORS, DESTRUCTORS AND OPERATOR OVERLOADING.pptx
CONSTRUCTORS, DESTRUCTORS AND OPERATOR OVERLOADING.pptx
 
chapter-9-constructors.pdf
chapter-9-constructors.pdfchapter-9-constructors.pdf
chapter-9-constructors.pdf
 
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
Constructor and DestructorConstructor and Destructor
Constructor and Destructor
 
Object Oriented Design and Programming Unit-02
Object Oriented Design and Programming Unit-02Object Oriented Design and Programming Unit-02
Object Oriented Design and Programming Unit-02
 
constructors and destructors
constructors and destructorsconstructors and destructors
constructors and destructors
 
Learning C++ - Class 4
Learning C++ - Class 4Learning C++ - Class 4
Learning C++ - Class 4
 
Constructor,destructors cpp
Constructor,destructors cppConstructor,destructors cpp
Constructor,destructors cpp
 
Oops lecture 1
Oops lecture 1Oops lecture 1
Oops lecture 1
 
Classes
ClassesClasses
Classes
 
Lecture 4.2 c++(comlete reference book)
Lecture 4.2 c++(comlete reference book)Lecture 4.2 c++(comlete reference book)
Lecture 4.2 c++(comlete reference book)
 
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
 
Unit 1 Part - 3 constructor Overloading Static.ppt
Unit 1 Part - 3  constructor Overloading Static.pptUnit 1 Part - 3  constructor Overloading Static.ppt
Unit 1 Part - 3 constructor Overloading Static.ppt
 
Oops concept
Oops conceptOops concept
Oops concept
 
C#2
C#2C#2
C#2
 

More from Keyur Vadodariya

Transport Layer Services : Multiplexing And Demultiplexing
Transport Layer Services : Multiplexing And DemultiplexingTransport Layer Services : Multiplexing And Demultiplexing
Transport Layer Services : Multiplexing And Demultiplexing
Keyur Vadodariya
 
I/O Management
I/O ManagementI/O Management
I/O Management
Keyur Vadodariya
 
Signed Addition And Subtraction
Signed Addition And SubtractionSigned Addition And Subtraction
Signed Addition And Subtraction
Keyur Vadodariya
 
Ocean Acidification
Ocean AcidificationOcean Acidification
Ocean Acidification
Keyur Vadodariya
 
Polar Curves
Polar CurvesPolar Curves
Polar Curves
Keyur Vadodariya
 
Laser And It's Application
Laser And It's ApplicationLaser And It's Application
Laser And It's Application
Keyur Vadodariya
 
Air Compressors
Air CompressorsAir Compressors
Air Compressors
Keyur Vadodariya
 

More from Keyur Vadodariya (7)

Transport Layer Services : Multiplexing And Demultiplexing
Transport Layer Services : Multiplexing And DemultiplexingTransport Layer Services : Multiplexing And Demultiplexing
Transport Layer Services : Multiplexing And Demultiplexing
 
I/O Management
I/O ManagementI/O Management
I/O Management
 
Signed Addition And Subtraction
Signed Addition And SubtractionSigned Addition And Subtraction
Signed Addition And Subtraction
 
Ocean Acidification
Ocean AcidificationOcean Acidification
Ocean Acidification
 
Polar Curves
Polar CurvesPolar Curves
Polar Curves
 
Laser And It's Application
Laser And It's ApplicationLaser And It's Application
Laser And It's Application
 
Air Compressors
Air CompressorsAir Compressors
Air Compressors
 

Recently uploaded

Quality defects in TMT Bars, Possible causes and Potential Solutions.
Quality defects in TMT Bars, Possible causes and Potential Solutions.Quality defects in TMT Bars, Possible causes and Potential Solutions.
Quality defects in TMT Bars, Possible causes and Potential Solutions.
PrashantGoswami42
 
weather web application report.pdf
weather web application report.pdfweather web application report.pdf
weather web application report.pdf
Pratik Pawar
 
ethical hacking-mobile hacking methods.ppt
ethical hacking-mobile hacking methods.pptethical hacking-mobile hacking methods.ppt
ethical hacking-mobile hacking methods.ppt
Jayaprasanna4
 
addressing modes in computer architecture
addressing modes  in computer architectureaddressing modes  in computer architecture
addressing modes in computer architecture
ShahidSultan24
 
road safety engineering r s e unit 3.pdf
road safety engineering  r s e unit 3.pdfroad safety engineering  r s e unit 3.pdf
road safety engineering r s e unit 3.pdf
VENKATESHvenky89705
 
Event Management System Vb Net Project Report.pdf
Event Management System Vb Net  Project Report.pdfEvent Management System Vb Net  Project Report.pdf
Event Management System Vb Net Project Report.pdf
Kamal Acharya
 
Final project report on grocery store management system..pdf
Final project report on grocery store management system..pdfFinal project report on grocery store management system..pdf
Final project report on grocery store management system..pdf
Kamal Acharya
 
DESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docxDESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docx
FluxPrime1
 
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptxCFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
R&R Consult
 
MCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdfMCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdf
Osamah Alsalih
 
Standard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - NeometrixStandard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - Neometrix
Neometrix_Engineering_Pvt_Ltd
 
LIGA(E)11111111111111111111111111111111111111111.ppt
LIGA(E)11111111111111111111111111111111111111111.pptLIGA(E)11111111111111111111111111111111111111111.ppt
LIGA(E)11111111111111111111111111111111111111111.ppt
ssuser9bd3ba
 
ethical hacking in wireless-hacking1.ppt
ethical hacking in wireless-hacking1.pptethical hacking in wireless-hacking1.ppt
ethical hacking in wireless-hacking1.ppt
Jayaprasanna4
 
The role of big data in decision making.
The role of big data in decision making.The role of big data in decision making.
The role of big data in decision making.
ankuprajapati0525
 
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdfHybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
fxintegritypublishin
 
CME397 Surface Engineering- Professional Elective
CME397 Surface Engineering- Professional ElectiveCME397 Surface Engineering- Professional Elective
CME397 Surface Engineering- Professional Elective
karthi keyan
 
The Benefits and Techniques of Trenchless Pipe Repair.pdf
The Benefits and Techniques of Trenchless Pipe Repair.pdfThe Benefits and Techniques of Trenchless Pipe Repair.pdf
The Benefits and Techniques of Trenchless Pipe Repair.pdf
Pipe Restoration Solutions
 
ASME IX(9) 2007 Full Version .pdf
ASME IX(9)  2007 Full Version       .pdfASME IX(9)  2007 Full Version       .pdf
ASME IX(9) 2007 Full Version .pdf
AhmedHussein950959
 
Halogenation process of chemical process industries
Halogenation process of chemical process industriesHalogenation process of chemical process industries
Halogenation process of chemical process industries
MuhammadTufail242431
 
H.Seo, ICLR 2024, MLILAB, KAIST AI.pdf
H.Seo,  ICLR 2024, MLILAB,  KAIST AI.pdfH.Seo,  ICLR 2024, MLILAB,  KAIST AI.pdf
H.Seo, ICLR 2024, MLILAB, KAIST AI.pdf
MLILAB
 

Recently uploaded (20)

Quality defects in TMT Bars, Possible causes and Potential Solutions.
Quality defects in TMT Bars, Possible causes and Potential Solutions.Quality defects in TMT Bars, Possible causes and Potential Solutions.
Quality defects in TMT Bars, Possible causes and Potential Solutions.
 
weather web application report.pdf
weather web application report.pdfweather web application report.pdf
weather web application report.pdf
 
ethical hacking-mobile hacking methods.ppt
ethical hacking-mobile hacking methods.pptethical hacking-mobile hacking methods.ppt
ethical hacking-mobile hacking methods.ppt
 
addressing modes in computer architecture
addressing modes  in computer architectureaddressing modes  in computer architecture
addressing modes in computer architecture
 
road safety engineering r s e unit 3.pdf
road safety engineering  r s e unit 3.pdfroad safety engineering  r s e unit 3.pdf
road safety engineering r s e unit 3.pdf
 
Event Management System Vb Net Project Report.pdf
Event Management System Vb Net  Project Report.pdfEvent Management System Vb Net  Project Report.pdf
Event Management System Vb Net Project Report.pdf
 
Final project report on grocery store management system..pdf
Final project report on grocery store management system..pdfFinal project report on grocery store management system..pdf
Final project report on grocery store management system..pdf
 
DESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docxDESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docx
 
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptxCFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
 
MCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdfMCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdf
 
Standard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - NeometrixStandard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - Neometrix
 
LIGA(E)11111111111111111111111111111111111111111.ppt
LIGA(E)11111111111111111111111111111111111111111.pptLIGA(E)11111111111111111111111111111111111111111.ppt
LIGA(E)11111111111111111111111111111111111111111.ppt
 
ethical hacking in wireless-hacking1.ppt
ethical hacking in wireless-hacking1.pptethical hacking in wireless-hacking1.ppt
ethical hacking in wireless-hacking1.ppt
 
The role of big data in decision making.
The role of big data in decision making.The role of big data in decision making.
The role of big data in decision making.
 
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdfHybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
 
CME397 Surface Engineering- Professional Elective
CME397 Surface Engineering- Professional ElectiveCME397 Surface Engineering- Professional Elective
CME397 Surface Engineering- Professional Elective
 
The Benefits and Techniques of Trenchless Pipe Repair.pdf
The Benefits and Techniques of Trenchless Pipe Repair.pdfThe Benefits and Techniques of Trenchless Pipe Repair.pdf
The Benefits and Techniques of Trenchless Pipe Repair.pdf
 
ASME IX(9) 2007 Full Version .pdf
ASME IX(9)  2007 Full Version       .pdfASME IX(9)  2007 Full Version       .pdf
ASME IX(9) 2007 Full Version .pdf
 
Halogenation process of chemical process industries
Halogenation process of chemical process industriesHalogenation process of chemical process industries
Halogenation process of chemical process industries
 
H.Seo, ICLR 2024, MLILAB, KAIST AI.pdf
H.Seo,  ICLR 2024, MLILAB,  KAIST AI.pdfH.Seo,  ICLR 2024, MLILAB,  KAIST AI.pdf
H.Seo, ICLR 2024, MLILAB, KAIST AI.pdf
 

Constructors and Destructors

  • 1. Sarvajanik College Of Engineering And Technology Subject Name :- Object Oriented Programming With C++ Subject Code :- 2140705 Branch :- Computer Engineering Eve. (COE) 1
  • 2. Group Members And Topic ⮚ 170420107559 – Uttam Thummar ⮚ 170420107560 – Dhriti Uttamchandani ⮚ 170420107561 – Keyur Vadodariya ⮚ 170420107562 – Aditya Vaghela ⮚Topic:- Constructors & Destructors ⮚ Guided By :- Prof. Dipali Kasat Prof. Jaydeep Barad 2
  • 3. What Does Constructor Mean? ⮚ A constructor is a special method of a class or structure in object oriented programming that initializes an object of that type. ⮚ A constructor is an instance method that usually has the same name an the class. ⮚ A constructor can be used to set the values of the member of an object , either to default or to user-defined values.
  • 4. Need For Constructor ⮚ It is very common for some part of an object to require initialization before it can be used. ⮚ Suppose you are working on 100's of objects and the default value of a particular data member is needed to be zero. ⮚ Initializing all objects manually will be very tedious job. ⮚ Instead, you can define a constructor function which initializes that data member to zero. ⮚ Then all you have to do is declare object and constructor will initialize object automatically.
  • 5. Syntax Of Constructor class classname { … public: classname( Arguments ) { //Statements } };
  • 6. Characteristics Of Constructor. ⮚ They should be declared in the public section. ⮚ They are invoked automatically when the object are created. ⮚ They do not have return type not even void and there for they cannot return any values. ⮚ They cannot be inherited, a derived class can call the base class constructor. ⮚ They make implicit calls to the operator new and delete when memory allocation is required.
  • 7. Two Ways For Calling a Constructor ⮚ Implicit call Class circle { float radius; Public: circle() { radius = 0; } circle (float r) { radius = r; } }; Circle ob (7.6); ⮚ Explicit call Circle ob; Ob = circle(7.6);
  • 8. Static Initialization Of Object Class demo int main ( ) { { int pp; demo (2,3); public ( ): return 0; demo (int x , int y); } { a=x; b=y; } };
  • 9. Dynamic initialization of object Class demo int main() { { int a,b; int a,b; public: cout<<“enter 2 no.”; demo(int x,int y) cin>>a>>b; { demo obj (a,b); a=x; } b=y; } };
  • 10. Types Of Constructors ⮚ There are basically three main types of constructors. 1. Default Constructor ▪ One which is Implicitly called, when an object is created. 2. Parameterized Constructor ▪ One which has arguments. 3. Copy Constructor ▪ One which copies one object to another. 10
  • 11. Default Constructor ⮚ It is a constructor which is implicitly called at time of object creation. ⮚ It is automatically declared and defined by the compiler if not by the user. ⮚ This constructor has no arguments and may or may not have the body statements. ⮚ So it is also called Non-Arg. Constructor. ⮚Syntax :- ▪ By User Classname(no arguments) { //Statements } ▪ By Compiler Classname(no arguments) { //Empty Body } 11
  • 12. Parameterized Constructor ⮚ Sometimes it becomes necessary to initialize the various data elements of an objects with different values when they are created. ⮚ This is achieved by passing arguments to the constructor function when the objects are created. ⮚ The constructors that can take arguments are called parameterized constructors. ⮚Syntax :- Classname(arguments) { //Statements } 12
  • 13. Copy Constructor ⮚ It is basically a constructor which copies one object to another of the same class. ⮚ The object which is to be copied is passed as an argument o the constructor. ⮚ The object in which the data is to be copied is the object used to calling constructor. ⮚Syntax :- classname(classname &object) { var = object.var ; } 13
  • 14. Constructor Overloading ⮚ C++ permits the use of more than one Constructors in a class in a single program. ⮚ Such a situation is called Constructor Overloading. ⮚Rules For Constructor Overloading :- ⮚ Constructors should have different no.’s of arguments. ⮚ Constructors should have different types of arguments. 14
  • 15. Example Of Constructor Overloading ⮚ #include <iostream> using namespace std; class circle { int r; public: //Default Cons. circle() { r = 0; } //Parameterized Cons. circle(int x) { r = x; } //Copy Cons. circle(circle &x) { r = x.r; } void display() { cout << r; } }; int main() { circle r1; r1.display(); r1 = circle(20); circle r2(r1); r1.display(); r2.display(); } ⮚ Output :- 0 20 20 15
  • 16. Dynamic Constructor •The constructors can also be used to allocate memory while creating objects. •This will enable the system to allocate the right amount of memory for each object when the objects are not of the same size. •Allocation of memory to objects at the time of their construction is known as dynamic construction of objects. •The memory is created with the help of the new operator. 16
  • 17. Destructors ● A destructor is used to destroy the objects that have been created by a constructor. ● Like constructor, the destructor is a member function whose name is the same as the class name but is preceded by a tilde . eg: ~ integer ( ) { } ● Whenever new is used to allocate memory in the constructor, we should use delete to free that memory. ● There can only be 1 destructor in a class. 17
  • 18. Rules Of Destructors ● Destructors cannot be overloaded. ● Destructors take no arguments. ● Destructors don't return any value. ● Destructure cannot be inherited. A destructor function is called automatically when the object goes out of scope: ● Function ends ● Program ends ● A block containing local variables ends ● A delete operator is called 18 When is a Destructor called?
  • 19. Example of New and Delete Operators 19 #include <iostream> using namespace std; class Test { private: int n; float *a; public: Test() { cout << "Enter n: "; cin >> n; a = new float[n]; cout << "Enter array elements." << endl; for (int i = 0; i < n; i++) { cout << i + 1<<” element:”; cin >> *(a + i); } } ~Test() { delete[] a; } void Display() { cout << "nArray Elements:" << endl; for (int i = 0; i < n; i++) cout << *(a + i) <<”t”; } }; int main() { Test s; s.Display(); } ⮚ Output :- Enter n:3 Enter array elements: Enter 1 element:2 Enter 2 element:4 Enter 3 element:6 Array elements: 2 4 6
  • 20. Limitations Of Constructors ⮚ They should only be declared in public section. ⮚ They don’t have any return type, not even void, so they do not return any value. ⮚ They cannot be Inherited. However a Derived Class can call a Base Class Constructor. ⮚ Name of the constructor must be same as that of class. ⮚ Constructors may not be Static as well as Virtual. ⮚ We cannot fetch the address of the Constructor Function. 20