SlideShare ist ein Scribd-Unternehmen logo
1 von 32
Computer Science
Constructors and Destructors…
</>
Saharsh Anand
</>
Constructor Function
giving the class object some sense…
Ever thought that what kind of
problems can an object create if it’s
not initialised.
It’s similar to giving your vehicle the
final gear just during the startup.
The Object
What if user
didn’t assign me
any value initially
(by chance he
forget) and finally
ask me my value?
I’ll be helpless.
So to make a
program
robust the
object should
be initialised
every time.
And in class we do this by constructors...
• Well! It is a special member function
that is used for initialization of objects
(data members).
• A constructor function is called
whenever an object is created.
• Constructors are also called when an
object is created as a part of another
object.
What is “ Constructor ” !!!
</>
Initialising a Constructor:
/* Outside the Class */
class twelfth{
int total; //Total twelfth of student
char sec; //Section of the class
public:
twelfth(void); //Constructor Function Declared
---
---
};
//Constructor Function Defined
twelfth :: twelfth( ){
total=50;
sec=‘A’;
}
Initialising a Constructor:
</> /* Outside the Class */
class twelfth{
int total; //Total twelfth of student
char sec; //Section of the class
public:
twelfth( ){ //Function Declared
total=50;
sec=‘A’;
}
Constructor Functions have “same
name” as that of class name.
They do not have return types, not even
void.
May or may not have parameters.
C++ has default constructors that are
called whenever a class is declared even
if no function with the same name exists
Some of it’s characteristics
Mind It!
They should be declared in “public
section”.
They are invoked “automatically” when
objects are created.
We cannot call it for the already
constructed object.
Default Constructor
• Because in C++ default constructor is there.
• This constructor has no arguments in it.
• Default Constructor is also called as no
argument constructor.
So you often didn’t
provide any argument to
constructor, but still
compiler didn’t bother to
show any error! How?
</>
Example for default constructor:
class person {
int DOB;
public:
person(){
cout<<“Contructor called";
}
};
void main(){
person obj;
getch();
}
Parameterised Constructor
• A parameterized constructor is just one that
has parameters specified in it.
• We can pass the arguments to constructor
function when object are created.
• A constructor that can take arguments are
called parameterized constructors.
</>
Example of a Parameterised Constructor:
class person{
int DOB;
public:
Creature(int year){ //Parameterized Constructor
DOB = year;
}
};
Copy Constructor
• Copy Constructor is used to declare and
initialize an object from another object.
• For example the statement:
abc c2(c1);
would define the object c2 and at the same
time initialize it to the value of c1.
• The process of initializing through a copy
constructor is known as copy initialization.
</>
Example of a Copy Constructor:
class abc{
int a, b;
public:
abc(int x, int y){
a = x;
b = y;
}
abc::abc(abc &p){
a = p.a;
b = p.b;
}
void showdata(){
cout << a << " " << b << endl;
}
};
continued…
void main(){
abc c1(10, 20);
abc c2(c1);
c1.showdata();
c2.showdata();
getch();
}
</>
continued…
Default Constructor
• Default argument is an argument to a
function that a programmer is not required
to specify.
• C++ allow the programmer to specify
default arguments that always have a value,
even if one is not specified when calling the
function.
• For example, in the following function
declaration:
int MyFunc(int a,int b,int c=12);
• The programmer may call this function in
two ways:
result = MyFunc(1, 2, 3);
result = MyFunc(1, 2);
• In the first case the value for the argument
called c is specified as normal. In the second
one, the argument is omitted, and the
default value of 12 will be used instead.
• It is possible to define constructors with
default arguments.
Something Important
• Automatically called when an object is
created.
• We can define our own constructors
• A constructor takes the same name as the
class name.
• We can’t define a constructor in the
private section.
• No return type is specified for a
constructor.
• Constructor must be defined in the
public.
• Overloading of constructors is possible.
• If an object is copied from another
object then the copy constructor is
called.
</>
Destructor Function
ending up the class data…
You won’t ever
want that your
program's
variable taking all
your hard disk
space.
In case if you want
that the program
should end up after
destroying the data
then you’ll have to
use destructors
•Destructors are special member
functions.
• Release dynamic allocated memory.
• Destructors are automatically named.
• Takes the same name of class name.
What is “ Destructor ” !!!
Syntax of Destructor
Well there is nothing to think about
the syntax of destructor if you are
familiar with the constructors.
Just add a tilde (~).
~class-name();
Something Important
• Destructors take the same name as
class name.
• As in constructors, destructors are also
defined in the public.
• Destructors cannot be overloaded.
• No return type is specified.
</>
Example of a Destructors:
class person{
int DOB;
public:
person(){
DOB=1998;
cout<<"constructor called"<<endl;
}
~person(){
cout<<"destructor called"<<endl;
}
};
continued…
void main(){
cout<<“n Main startn"<<endl;
{
person obj;
}
cout<<“n Main End"<<endl;
getch();
}
continued…
</>
</>
Another example of destructor:
#include<iostream.h>
#include<stdio.h>
class Line{
public:
void setLength(double len);
double getLength(void);
Line(); // This is the constructor declaration
~Line(); // This is the destructor: declaration
private:
double length;
};
continued…
// Member functions definitions including constructor
Line::Line(void){
cout << "Object is being created" << endl;
}
Line::~Line(void){
cout << "Object is being deleted" << endl;
}
void Line::setLength(double len){
length = len;
}
double Line::getLength(void){
return length;
}
// Main function for the program
</>
continued…
void main(){
Line line;
//Set line’s length
line.setLength(6.0);
cout<<"Length of line:"<<line.getLength()<<endl;
getch();
}
</>
continued…
Constructor
Destructor
Constructs (or
create) the data in
the objects of the
class.
Destroy (or delete)
the data in the
objects of the class.
Constructor & destructor

Weitere ähnliche Inhalte

Was ist angesagt?

Oop Constructor Destructors Constructor Overloading lecture 2
Oop Constructor  Destructors Constructor Overloading lecture 2Oop Constructor  Destructors Constructor Overloading lecture 2
Oop Constructor Destructors Constructor Overloading lecture 2Abbas Ajmal
 
constructor and destructor-object oriented programming
constructor and destructor-object oriented programmingconstructor and destructor-object oriented programming
constructor and destructor-object oriented programmingAshita Agrawal
 
Constructor and Types of Constructors
Constructor and Types of ConstructorsConstructor and Types of Constructors
Constructor and Types of ConstructorsDhrumil Panchal
 
C++ Constructor destructor
C++ Constructor destructorC++ Constructor destructor
C++ Constructor destructorDa Mystic Sadi
 
Constructors and destructors
Constructors and destructorsConstructors and destructors
Constructors and destructorsVineeta Garg
 
Constructor and Destructor in c++
Constructor  and Destructor in c++Constructor  and Destructor in c++
Constructor and Destructor in c++aleenaguen
 
Constructor and Destructor
Constructor and DestructorConstructor and Destructor
Constructor and DestructorKamal Acharya
 
Types of Constructor in C++
Types of Constructor in C++Types of Constructor in C++
Types of Constructor in C++Bhavik Vashi
 
constructors in java ppt
constructors in java pptconstructors in java ppt
constructors in java pptkunal kishore
 
Constructors and destructors
Constructors and destructorsConstructors and destructors
Constructors and destructorsNilesh Dalvi
 
Constructor and Destructor
Constructor and DestructorConstructor and Destructor
Constructor and DestructorSunipa Bera
 

Was ist angesagt? (20)

Oop Constructor Destructors Constructor Overloading lecture 2
Oop Constructor  Destructors Constructor Overloading lecture 2Oop Constructor  Destructors Constructor Overloading lecture 2
Oop Constructor Destructors Constructor Overloading lecture 2
 
constructor and destructor-object oriented programming
constructor and destructor-object oriented programmingconstructor and destructor-object oriented programming
constructor and destructor-object oriented programming
 
Constructor and Types of Constructors
Constructor and Types of ConstructorsConstructor and Types of Constructors
Constructor and Types of Constructors
 
Tut Constructor
Tut ConstructorTut Constructor
Tut Constructor
 
C++ Constructor destructor
C++ Constructor destructorC++ Constructor destructor
C++ Constructor destructor
 
Constructor & Destructor
Constructor & DestructorConstructor & Destructor
Constructor & Destructor
 
Constructors and destructors
Constructors and destructorsConstructors and destructors
Constructors and destructors
 
Constructor
ConstructorConstructor
Constructor
 
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++
 
Constructor and Destructor
Constructor and DestructorConstructor and Destructor
Constructor and Destructor
 
Types of Constructor in C++
Types of Constructor in C++Types of Constructor in C++
Types of Constructor in C++
 
Constructor & destructor
Constructor & destructorConstructor & destructor
Constructor & destructor
 
constructors in java ppt
constructors in java pptconstructors in java ppt
constructors in java ppt
 
Constructors and destructors
Constructors and destructorsConstructors and destructors
Constructors and destructors
 
C# Constructors
C# ConstructorsC# Constructors
C# Constructors
 
Constructor and destructor
Constructor and destructorConstructor and destructor
Constructor and destructor
 
Constructors destructors
Constructors destructorsConstructors destructors
Constructors destructors
 
Constructor and Destructor
Constructor and DestructorConstructor and Destructor
Constructor and Destructor
 
5 Constructors and Destructors
5 Constructors and Destructors5 Constructors and Destructors
5 Constructors and Destructors
 

Andere mochten auch

Inheritance in oops
Inheritance in oopsInheritance in oops
Inheritance in oopsHirra Sultan
 
Bca 2nd sem u-2 classes & objects
Bca 2nd sem u-2 classes & objectsBca 2nd sem u-2 classes & objects
Bca 2nd sem u-2 classes & objectsRai University
 
constructors and destructors in c++
constructors and destructors in c++constructors and destructors in c++
constructors and destructors in c++HalaiHansaika
 
Inheritance OOP Concept in C++.
Inheritance OOP Concept in C++.Inheritance OOP Concept in C++.
Inheritance OOP Concept in C++.MASQ Technologies
 
4GMAT Diagnostic Test Q14 - Problem Solving - Coordinate Geometry
4GMAT Diagnostic Test Q14 - Problem Solving - Coordinate Geometry4GMAT Diagnostic Test Q14 - Problem Solving - Coordinate Geometry
4GMAT Diagnostic Test Q14 - Problem Solving - Coordinate Geometry4gmatprep
 
Dynamics allocation
Dynamics allocationDynamics allocation
Dynamics allocationKumar
 
Constructor overloading in C++
Constructor overloading in C++Constructor overloading in C++
Constructor overloading in C++Learn By Watch
 
Java Static Factory Methods
Java Static Factory MethodsJava Static Factory Methods
Java Static Factory MethodsYe Win
 
Builder pattern vs constructor
Builder pattern vs constructorBuilder pattern vs constructor
Builder pattern vs constructorLiviu Tudor
 
04. constructor & destructor
04. constructor & destructor04. constructor & destructor
04. constructor & destructorHaresh Jaiswal
 
Object Oriented Programming in Python
Object Oriented Programming in PythonObject Oriented Programming in Python
Object Oriented Programming in PythonSujith Kumar
 

Andere mochten auch (17)

Inheritance in OOPS
Inheritance in OOPSInheritance in OOPS
Inheritance in OOPS
 
Inheritance in oops
Inheritance in oopsInheritance in oops
Inheritance in oops
 
Constructors & destructors
Constructors & destructorsConstructors & destructors
Constructors & destructors
 
Bca 2nd sem u-2 classes & objects
Bca 2nd sem u-2 classes & objectsBca 2nd sem u-2 classes & objects
Bca 2nd sem u-2 classes & objects
 
constructors and destructors in c++
constructors and destructors in c++constructors and destructors in c++
constructors and destructors in c++
 
Inheritance OOP Concept in C++.
Inheritance OOP Concept in C++.Inheritance OOP Concept in C++.
Inheritance OOP Concept in C++.
 
4GMAT Diagnostic Test Q14 - Problem Solving - Coordinate Geometry
4GMAT Diagnostic Test Q14 - Problem Solving - Coordinate Geometry4GMAT Diagnostic Test Q14 - Problem Solving - Coordinate Geometry
4GMAT Diagnostic Test Q14 - Problem Solving - Coordinate Geometry
 
Dynamics allocation
Dynamics allocationDynamics allocation
Dynamics allocation
 
Ashish oot
Ashish ootAshish oot
Ashish oot
 
Constructor overloading in C++
Constructor overloading in C++Constructor overloading in C++
Constructor overloading in C++
 
Java Static Factory Methods
Java Static Factory MethodsJava Static Factory Methods
Java Static Factory Methods
 
Builder pattern vs constructor
Builder pattern vs constructorBuilder pattern vs constructor
Builder pattern vs constructor
 
04. constructor & destructor
04. constructor & destructor04. constructor & destructor
04. constructor & destructor
 
Oop concepts
Oop conceptsOop concepts
Oop concepts
 
01 introduction to oop and java
01 introduction to oop and java01 introduction to oop and java
01 introduction to oop and java
 
Inheritance
InheritanceInheritance
Inheritance
 
Object Oriented Programming in Python
Object Oriented Programming in PythonObject Oriented Programming in Python
Object Oriented Programming in Python
 

Ähnlich wie Constructor & destructor

constructor in object oriented program.pptx
constructor in object oriented program.pptxconstructor in object oriented program.pptx
constructor in object oriented program.pptxurvashipundir04
 
C++ Constructor and Destructors.pptx
C++ Constructor and Destructors.pptxC++ Constructor and Destructors.pptx
C++ Constructor and Destructors.pptxsasukeman
 
Constructor destructor.ppt
Constructor destructor.pptConstructor destructor.ppt
Constructor destructor.pptKarthik Sekar
 
Constructors in C++.pptx
Constructors in C++.pptxConstructors in C++.pptx
Constructors in C++.pptxRassjb
 
Constructor and Destructor.pdf
Constructor and Destructor.pdfConstructor and Destructor.pdf
Constructor and Destructor.pdfMadnessKnight
 
ConsTRUCTION AND DESTRUCTION
ConsTRUCTION AND DESTRUCTIONConsTRUCTION AND DESTRUCTION
ConsTRUCTION AND DESTRUCTIONShweta Shah
 
CST 203 Lecture 7.pptx
CST 203 Lecture 7.pptxCST 203 Lecture 7.pptx
CST 203 Lecture 7.pptxDrKalkaDubey1
 
[OOP - Lec 13,14,15] Constructors / Destructor and its Types
[OOP - Lec 13,14,15] Constructors / Destructor and its Types[OOP - Lec 13,14,15] Constructors / Destructor and its Types
[OOP - Lec 13,14,15] Constructors / Destructor and its TypesMuhammad Hammad Waseem
 
Constructors and Destructors
Constructors and DestructorsConstructors and Destructors
Constructors and DestructorsKeyur Vadodariya
 
Constructor&amp; destructor
Constructor&amp; destructorConstructor&amp; destructor
Constructor&amp; destructorchauhankapil
 
C++ Unit-III Lecture-3a-C++ Programming Concepts
C++ Unit-III Lecture-3a-C++ Programming ConceptsC++ Unit-III Lecture-3a-C++ Programming Concepts
C++ Unit-III Lecture-3a-C++ Programming Conceptsdharawagh9999
 
Constructor and destructor
Constructor and destructorConstructor and destructor
Constructor and destructorrajshreemuthiah
 
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCECONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCEVenugopalavarma Raja
 

Ähnlich wie Constructor & destructor (20)

Constructors and Destructor in C++
Constructors and Destructor in C++Constructors and Destructor in C++
Constructors 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
 
C++ Constructor and Destructors.pptx
C++ Constructor and Destructors.pptxC++ Constructor and Destructors.pptx
C++ Constructor and Destructors.pptx
 
Constructors and Destructors
Constructors and DestructorsConstructors and Destructors
Constructors and Destructors
 
Constructor destructor.ppt
Constructor destructor.pptConstructor destructor.ppt
Constructor destructor.ppt
 
Constructors & Destructors
Constructors  & DestructorsConstructors  & Destructors
Constructors & Destructors
 
Constructors in C++.pptx
Constructors in C++.pptxConstructors in C++.pptx
Constructors in C++.pptx
 
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
 
ConsTRUCTION AND DESTRUCTION
ConsTRUCTION AND DESTRUCTIONConsTRUCTION AND DESTRUCTION
ConsTRUCTION AND DESTRUCTION
 
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
 
CST 203 Lecture 7.pptx
CST 203 Lecture 7.pptxCST 203 Lecture 7.pptx
CST 203 Lecture 7.pptx
 
[OOP - Lec 13,14,15] Constructors / Destructor and its Types
[OOP - Lec 13,14,15] Constructors / Destructor and its Types[OOP - Lec 13,14,15] Constructors / Destructor and its Types
[OOP - Lec 13,14,15] Constructors / Destructor and its Types
 
Constructors and Destructors
Constructors and DestructorsConstructors and Destructors
Constructors and Destructors
 
Constructor&amp; destructor
Constructor&amp; destructorConstructor&amp; destructor
Constructor&amp; destructor
 
C++ Unit-III Lecture-3a-C++ Programming Concepts
C++ Unit-III Lecture-3a-C++ Programming ConceptsC++ Unit-III Lecture-3a-C++ Programming Concepts
C++ Unit-III Lecture-3a-C++ Programming Concepts
 
Constructor and destructor
Constructor and destructorConstructor and destructor
Constructor and destructor
 
Oops
OopsOops
Oops
 
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCECONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
 

Kürzlich hochgeladen

TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providermohitmore19
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfkalichargn70th171
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerThousandEyes
 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionSolGuruz
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsJhone kinadey
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comFatema Valibhai
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...OnePlan Solutions
 
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female serviceCALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female serviceanilsa9823
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️Delhi Call girls
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfkalichargn70th171
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsAndolasoft Inc
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...MyIntelliSource, Inc.
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Steffen Staab
 
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️anilsa9823
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...panagenda
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsArshad QA
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...MyIntelliSource, Inc.
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Modelsaagamshah0812
 

Kürzlich hochgeladen (20)

TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with Precision
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.com
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS LiveVip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
 
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female serviceCALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.js
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview Questions
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Models
 

Constructor & destructor

  • 1. Computer Science Constructors and Destructors… </> Saharsh Anand
  • 2. </> Constructor Function giving the class object some sense…
  • 3. Ever thought that what kind of problems can an object create if it’s not initialised. It’s similar to giving your vehicle the final gear just during the startup.
  • 4. The Object What if user didn’t assign me any value initially (by chance he forget) and finally ask me my value? I’ll be helpless. So to make a program robust the object should be initialised every time. And in class we do this by constructors...
  • 5. • Well! It is a special member function that is used for initialization of objects (data members). • A constructor function is called whenever an object is created. • Constructors are also called when an object is created as a part of another object. What is “ Constructor ” !!!
  • 6. </> Initialising a Constructor: /* Outside the Class */ class twelfth{ int total; //Total twelfth of student char sec; //Section of the class public: twelfth(void); //Constructor Function Declared --- --- }; //Constructor Function Defined twelfth :: twelfth( ){ total=50; sec=‘A’; }
  • 7. Initialising a Constructor: </> /* Outside the Class */ class twelfth{ int total; //Total twelfth of student char sec; //Section of the class public: twelfth( ){ //Function Declared total=50; sec=‘A’; }
  • 8. Constructor Functions have “same name” as that of class name. They do not have return types, not even void. May or may not have parameters. C++ has default constructors that are called whenever a class is declared even if no function with the same name exists Some of it’s characteristics Mind It!
  • 9. They should be declared in “public section”. They are invoked “automatically” when objects are created. We cannot call it for the already constructed object.
  • 10. Default Constructor • Because in C++ default constructor is there. • This constructor has no arguments in it. • Default Constructor is also called as no argument constructor. So you often didn’t provide any argument to constructor, but still compiler didn’t bother to show any error! How?
  • 11. </> Example for default constructor: class person { int DOB; public: person(){ cout<<“Contructor called"; } }; void main(){ person obj; getch(); }
  • 12. Parameterised Constructor • A parameterized constructor is just one that has parameters specified in it. • We can pass the arguments to constructor function when object are created. • A constructor that can take arguments are called parameterized constructors.
  • 13. </> Example of a Parameterised Constructor: class person{ int DOB; public: Creature(int year){ //Parameterized Constructor DOB = year; } };
  • 14. Copy Constructor • Copy Constructor is used to declare and initialize an object from another object. • For example the statement: abc c2(c1); would define the object c2 and at the same time initialize it to the value of c1. • The process of initializing through a copy constructor is known as copy initialization.
  • 15. </> Example of a Copy Constructor: class abc{ int a, b; public: abc(int x, int y){ a = x; b = y; } abc::abc(abc &p){ a = p.a; b = p.b; } void showdata(){ cout << a << " " << b << endl; } }; continued…
  • 16. void main(){ abc c1(10, 20); abc c2(c1); c1.showdata(); c2.showdata(); getch(); } </> continued…
  • 17. Default Constructor • Default argument is an argument to a function that a programmer is not required to specify. • C++ allow the programmer to specify default arguments that always have a value, even if one is not specified when calling the function. • For example, in the following function declaration: int MyFunc(int a,int b,int c=12);
  • 18. • The programmer may call this function in two ways: result = MyFunc(1, 2, 3); result = MyFunc(1, 2); • In the first case the value for the argument called c is specified as normal. In the second one, the argument is omitted, and the default value of 12 will be used instead. • It is possible to define constructors with default arguments.
  • 19. Something Important • Automatically called when an object is created. • We can define our own constructors • A constructor takes the same name as the class name. • We can’t define a constructor in the private section.
  • 20. • No return type is specified for a constructor. • Constructor must be defined in the public. • Overloading of constructors is possible. • If an object is copied from another object then the copy constructor is called.
  • 22. You won’t ever want that your program's variable taking all your hard disk space. In case if you want that the program should end up after destroying the data then you’ll have to use destructors
  • 23. •Destructors are special member functions. • Release dynamic allocated memory. • Destructors are automatically named. • Takes the same name of class name. What is “ Destructor ” !!!
  • 24. Syntax of Destructor Well there is nothing to think about the syntax of destructor if you are familiar with the constructors. Just add a tilde (~). ~class-name();
  • 25. Something Important • Destructors take the same name as class name. • As in constructors, destructors are also defined in the public. • Destructors cannot be overloaded. • No return type is specified.
  • 26. </> Example of a Destructors: class person{ int DOB; public: person(){ DOB=1998; cout<<"constructor called"<<endl; } ~person(){ cout<<"destructor called"<<endl; } }; continued…
  • 27. void main(){ cout<<“n Main startn"<<endl; { person obj; } cout<<“n Main End"<<endl; getch(); } continued… </>
  • 28. </> Another example of destructor: #include<iostream.h> #include<stdio.h> class Line{ public: void setLength(double len); double getLength(void); Line(); // This is the constructor declaration ~Line(); // This is the destructor: declaration private: double length; }; continued…
  • 29. // Member functions definitions including constructor Line::Line(void){ cout << "Object is being created" << endl; } Line::~Line(void){ cout << "Object is being deleted" << endl; } void Line::setLength(double len){ length = len; } double Line::getLength(void){ return length; } // Main function for the program </> continued…
  • 30. void main(){ Line line; //Set line’s length line.setLength(6.0); cout<<"Length of line:"<<line.getLength()<<endl; getch(); } </> continued…
  • 31. Constructor Destructor Constructs (or create) the data in the objects of the class. Destroy (or delete) the data in the objects of the class.