SlideShare a Scribd company logo
CONSTRUCTORS
AND
DESTRUCTORS
Harinder Singh
Sukhpal Singh
Thapar University, Patiala
Batch: 2011-2013
WHAT IS A CONSTRUCTOR ?
 In c++ , a constructor is a ‘special’ member
function whose task is to initialize the objects
of its class.
 It is special because its name is the same as
the class name.
 It is called constructor because it constructs
the value of data members of the class.
 The constructor is invoked whenever an object
of its associated class is created.
ssgill©tupatiala
CLASS WITH A CONSTRUCTOR
Class integer {
int m, n;
public:
integer(void); // constructor declared
};
Integer : : integer (void) // constructor defined
{
m=0; n=0;
}
Object declartion:
Integer int1; // object int1 created
ssgill©tupatiala
CHARACTERISTICS OF THE CONSTRUCTOR
 They should be declared in public section.
 They are invoked automatically when the objects are
created.
 They don't have return types, cannot return values.
 A constructor that accepts no parameters is called
default constructor. The default constructor for class
A is A:: A() . The statement A a; invokes the default
constructor of the compiler to create the object a.
ssgill©tupatiala
PARAMETERIZED CONSTRUCTOR
The constructor that can take arguments.
Class integer {
int m, n;
public:
integer(int x, int y); //parameterized constructor
};
Integer : : integer (int x, int y)
{
m=x; n=y;
}
Object declaration statement “integer int1 ; “ do not work
here.
ssgill©tupatiala
CALL IN PARAMETERIZED CONSTRUCTOR
 We must pass the initial values as arguments to
the constructor function when object is created.
This can be done in two ways:
 By calling the constructor explicitly.
integer int1 = integer(0, 100);
 By calling the constructor implicitly.
integer int1(0, 100);
This method is also called shorthand method.
ssgill©tupatiala
EXPLICIT/IMPLICIT CALL
int main()
{
integer int1(0, 100); //implicit call
integer int2 = integer(25, 75); //explicit call
cout<<“object 1:”
int1.function1();
cout<<“object 2:”
int2.function1();
return 0;
}
Output
object1: m=0 and n= 100
object2: m=25 and n= 75
ssgill©tupatiala
MULTIPLE CONSTRUCTOR IN A CLASS
Class integer
{
int m, n;
public:
integer() //default constructor
{m=0; n=0;}
integer(int a, int b) //parameterized constructor
{m=a; n=b;}
integer(integer & i) //copy constructor
{m= i.m; n= i.n;}
};
ssgill©tupatiala
CONSTRUCTORS WITH DEFAULT ARGUMENTS
complex(float real , float imag=0)
{
}
 complex c1(5.0);
 complex c2(2.0,3.0);
 Difference b/w A::A() and A::A(int a=0).
ssgill©tupatiala
DYNAMIC INITIALIZATION OF OBJECTS
Provide various initialization formats using
overloaded constructors.
 Fixed_deposit() { }
 Fixed_deposit(long int p, int y ,float r=0.12);
 Fixed_deposit( long int p, int y, int r);
ssgill©tupatiala
COPY CONSTRUCTOR
 It is used to declare and initialize an object
from another object.
e.g
• code() { }
• code( int a) {id = a;}
• code( code &x){ id = x.id; } //copy constructor.
 And various calls for them are:
• code A(100); // object A is Created and initialized.
• code B(A); // copy constructor called.
• code C = A; // copy constructor called again.
• code D; // object D is created , not initialized
D=A; // copy constructor is not called.
ssgill©tupatiala
DYNAMIC CONSTRUCTORS
 It is used to allocate memory while creating
objects.
• Enable the system to allocate right amount of
memory for each object when objects are not of
same size.
• Thus resulting in saving of memory.
ssgill©tupatiala
DYNAMIC CONSTRUCTORS
class String
{
char *name;
int length;
public:
String ()
{ length = 0;
name = new char[length+1];
}
String (char *s)
{ length = strlen(s);
name = new char[length+1];
strcpy(name, s);
}
};
main()
{
Char *first = “ME_SE_CR” ;
String name1( first);
String name2(“ Rupinder”);
.
.
.
}
ssgill©tupatiala
DESTRUCTORS
 ~ integer() { }
 As new is used to allocate memory in
dynamic constructors, here we use delete to
free that memory.
 Never take any argument nor does it return any
value.
 It will be invoked implicitly by compiler upon
exit from program or a block or a function.
ssgill©tupatiala
DESTRUCTORS
Int count = 0;
class test1
{
public :
~test1()
{
count ++;
cout <<“n no. of object created “
<< count ;
}
~test1 ()
{ cout <<“n no. of object destroyed”
<< count ;
Count - - ;
}
};
main()
{
cout << “n Enter Main n”;
test1 t1, t2, t3, t4;
{
cout <<“n Entered in block1” ;
test1 t5;
}
{
cout <<“n Entered in block2” ;
test1 t6;
}
cout << “n Re-Enter Main n”;
return 0;
}
ssgill©tupatiala
OUT PUT
 Enter Main
No. of object created 1
No. of object created 2
No. of object created 3
No. of object created 4
 Enter block 1
No. of object created 5
No. of object destroyed 5
 Enter block 2
No. of object created 5
No. of object destroyed 5
 Re - Enter Main
No. of object destroyed 4
No. of object destroyed 3
No. of object destroyed 2
No. of object destroyed 1
ssgill©tupatiala
THANK YOU
ssgill©tupatiala

More Related Content

What's hot

Polymorphism in c++(ppt)
Polymorphism in c++(ppt)Polymorphism in c++(ppt)
Polymorphism in c++(ppt)
Sanjit Shaw
 
Templates in C++
Templates in C++Templates in C++
Templates in C++Tech_MX
 
virtual function
virtual functionvirtual function
virtual function
VENNILAV6
 
Friend function
Friend functionFriend function
Friend function
zindadili
 
Constructor and Destructors in C++
Constructor and Destructors in C++Constructor and Destructors in C++
Constructor and Destructors in C++
sandeep54552
 
Constructors in C++
Constructors in C++Constructors in C++
Constructors in C++
RubaNagarajan
 
class and objects
class and objectsclass and objects
class and objectsPayel Guria
 
Destructors
DestructorsDestructors
Destructors
DeepikaT13
 
c++ programming Unit 2 basic structure of a c++ program
c++ programming Unit 2 basic structure of a c++ programc++ programming Unit 2 basic structure of a c++ program
c++ programming Unit 2 basic structure of a c++ program
AAKASH KUMAR
 
Constructor and destructor
Constructor  and  destructor Constructor  and  destructor
Constructor and destructor
Shubham Vishwambhar
 
Union in c language
Union  in c languageUnion  in c language
Union in c language
tanmaymodi4
 
Structure of C++ - R.D.Sivakumar
Structure of C++ - R.D.SivakumarStructure of C++ - R.D.Sivakumar
Structure of C++ - R.D.Sivakumar
Sivakumar R D .
 
Static Data Members and Member Functions
Static Data Members and Member FunctionsStatic Data Members and Member Functions
Static Data Members and Member Functions
MOHIT AGARWAL
 
Java static keyword
Java static keywordJava static keyword
Java static keyword
Lovely Professional University
 
Constructor overloading & method overloading
Constructor overloading & method overloadingConstructor overloading & method overloading
Constructor overloading & method overloading
garishma bhatia
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
Ramish Suleman
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
Vineeta Garg
 
07. Virtual Functions
07. Virtual Functions07. Virtual Functions
07. Virtual Functions
Haresh Jaiswal
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
Paumil Patel
 

What's hot (20)

Polymorphism in c++(ppt)
Polymorphism in c++(ppt)Polymorphism in c++(ppt)
Polymorphism in c++(ppt)
 
Templates in C++
Templates in C++Templates in C++
Templates in C++
 
Constructors & destructors
Constructors & destructorsConstructors & destructors
Constructors & destructors
 
virtual function
virtual functionvirtual function
virtual function
 
Friend function
Friend functionFriend function
Friend function
 
Constructor and Destructors in C++
Constructor and Destructors in C++Constructor and Destructors in C++
Constructor and Destructors in C++
 
Constructors in C++
Constructors in C++Constructors in C++
Constructors in C++
 
class and objects
class and objectsclass and objects
class and objects
 
Destructors
DestructorsDestructors
Destructors
 
c++ programming Unit 2 basic structure of a c++ program
c++ programming Unit 2 basic structure of a c++ programc++ programming Unit 2 basic structure of a c++ program
c++ programming Unit 2 basic structure of a c++ program
 
Constructor and destructor
Constructor  and  destructor Constructor  and  destructor
Constructor and destructor
 
Union in c language
Union  in c languageUnion  in c language
Union in c language
 
Structure of C++ - R.D.Sivakumar
Structure of C++ - R.D.SivakumarStructure of C++ - R.D.Sivakumar
Structure of C++ - R.D.Sivakumar
 
Static Data Members and Member Functions
Static Data Members and Member FunctionsStatic Data Members and Member Functions
Static Data Members and Member Functions
 
Java static keyword
Java static keywordJava static keyword
Java static keyword
 
Constructor overloading & method overloading
Constructor overloading & method overloadingConstructor overloading & method overloading
Constructor overloading & method overloading
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
 
07. Virtual Functions
07. Virtual Functions07. Virtual Functions
07. Virtual Functions
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
 

Viewers also liked

Constructor ppt
Constructor pptConstructor ppt
Constructor ppt
Vinod Kumar
 
constructor and destructor-object oriented programming
constructor and destructor-object oriented programmingconstructor and destructor-object oriented programming
constructor and destructor-object oriented programming
Ashita Agrawal
 
Constructor and destructor in c++
Constructor and destructor in c++Constructor and destructor in c++
Constructor and destructor in c++
Learn By Watch
 
Constructor & Destructor
Constructor & DestructorConstructor & Destructor
Constructor & Destructor
KV(AFS) Utarlai, Barmer (Rajasthan)
 
Constructor and Destructor PPT
Constructor and Destructor PPTConstructor and Destructor PPT
Constructor and Destructor PPT
Shubham Mondal
 
Constructor in java
Constructor in javaConstructor in java
Constructor in java
Hitesh Kumar
 
Constructor in java
Constructor in javaConstructor in java
Constructor in java
Pavith Gunasekara
 
Java constructors
Java constructorsJava constructors
Java constructors
QUONTRASOLUTIONS
 
Oop lec 5-(class objects, constructor & destructor)
Oop lec 5-(class objects, constructor & destructor)Oop lec 5-(class objects, constructor & destructor)
Oop lec 5-(class objects, constructor & destructor)Asfand Hassan
 
Java lec constructors
Java lec constructorsJava lec constructors
Java lec constructors
Jan Niño Acierto
 
Methods and constructors in java
Methods and constructors in javaMethods and constructors in java
Methods and constructors in java
baabtra.com - No. 1 supplier of quality freshers
 
Java lec class, objects and constructors
Java lec class, objects and constructorsJava lec class, objects and constructors
Java lec class, objects and constructors
Jan Niño Acierto
 
constructor & destructor in cpp
constructor & destructor in cppconstructor & destructor in cpp
constructor & destructor in cpp
gourav kottawar
 
Class object method constructors in java
Class object method constructors in javaClass object method constructors in java
Class object method constructors in java
Raja Sekhar
 
Object Oriented Programming Concepts
Object Oriented Programming ConceptsObject Oriented Programming Concepts
Object Oriented Programming Conceptsthinkphp
 
Constructor & destructor
Constructor & destructorConstructor & destructor
Constructor & destructor
Saharsh Anand
 
Inheritance in oops
Inheritance in oopsInheritance in oops
Inheritance in oops
Hirra Sultan
 

Viewers also liked (20)

Constructor ppt
Constructor pptConstructor ppt
Constructor ppt
 
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 destructor in c++
Constructor and destructor in c++Constructor and destructor in c++
Constructor and destructor in c++
 
Constructor & Destructor
Constructor & DestructorConstructor & Destructor
Constructor & Destructor
 
Constructor and Destructor PPT
Constructor and Destructor PPTConstructor and Destructor PPT
Constructor and Destructor PPT
 
Constructor in java
Constructor in javaConstructor in java
Constructor in java
 
Constructor in java
Constructor in javaConstructor in java
Constructor in java
 
Java constructors
Java constructorsJava constructors
Java constructors
 
Oop lec 5-(class objects, constructor & destructor)
Oop lec 5-(class objects, constructor & destructor)Oop lec 5-(class objects, constructor & destructor)
Oop lec 5-(class objects, constructor & destructor)
 
Java lec constructors
Java lec constructorsJava lec constructors
Java lec constructors
 
Methods and constructors in java
Methods and constructors in javaMethods and constructors in java
Methods and constructors in java
 
Java lec class, objects and constructors
Java lec class, objects and constructorsJava lec class, objects and constructors
Java lec class, objects and constructors
 
constructor & destructor in cpp
constructor & destructor in cppconstructor & destructor in cpp
constructor & destructor in cpp
 
Oops ppt
Oops pptOops ppt
Oops ppt
 
Class object method constructors in java
Class object method constructors in javaClass object method constructors in java
Class object method constructors in java
 
Object Oriented Programming Concepts
Object Oriented Programming ConceptsObject Oriented Programming Concepts
Object Oriented Programming Concepts
 
Constructor
ConstructorConstructor
Constructor
 
Constructor & destructor
Constructor & destructorConstructor & destructor
Constructor & destructor
 
Constructor & destructor
Constructor & destructorConstructor & destructor
Constructor & destructor
 
Inheritance in oops
Inheritance in oopsInheritance in oops
Inheritance in oops
 

Similar to Constructors and Destructors

Constructors and Destructors
Constructors and DestructorsConstructors and Destructors
Constructors and Destructors
Keyur Vadodariya
 
CONSTRUCTORS, DESTRUCTORS AND OPERATOR OVERLOADING.pptx
CONSTRUCTORS, DESTRUCTORS AND OPERATOR OVERLOADING.pptxCONSTRUCTORS, DESTRUCTORS AND OPERATOR OVERLOADING.pptx
CONSTRUCTORS, DESTRUCTORS AND OPERATOR OVERLOADING.pptx
DeepasCSE
 
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
 
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
dharawagh9999
 
Constructors in C++.pptx
Constructors in C++.pptxConstructors in C++.pptx
Constructors in C++.pptx
Rassjb
 
C++Constructors
C++ConstructorsC++Constructors
C++Constructors
Nusrat Gulbarga
 
Constructors destructors
Constructors destructorsConstructors destructors
Constructors destructors
Pranali Chaudhari
 
constructors and destructors
constructors and destructorsconstructors and destructors
constructors and destructors
Akshaya Parida
 
Tut Constructor
Tut ConstructorTut Constructor
Operator overload rr
Operator overload  rrOperator overload  rr
Operator overload rr
Dhivya Shanmugam
 
chapter-9-constructors.pdf
chapter-9-constructors.pdfchapter-9-constructors.pdf
chapter-9-constructors.pdf
study material
 
Constructors & Destructors [Compatibility Mode].pdf
Constructors & Destructors [Compatibility Mode].pdfConstructors & Destructors [Compatibility Mode].pdf
Constructors & Destructors [Compatibility Mode].pdf
LadallaRajKumar
 
Constructor in c++
Constructor in c++Constructor in c++
Constructor in c++
Jay Patel
 
Constructors and destructors in C++
Constructors and destructors in  C++Constructors and destructors in  C++
Constructors and destructors in C++
RAJ KUMAR
 
Java Programming - 04 object oriented in java
Java Programming - 04 object oriented in javaJava Programming - 04 object oriented in java
Java Programming - 04 object oriented in java
Danairat Thanabodithammachari
 
Chapter 6.6
Chapter 6.6Chapter 6.6
Chapter 6.6sotlsoc
 
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
 
Tutconstructordes
TutconstructordesTutconstructordes
TutconstructordesNiti Arora
 

Similar to Constructors and Destructors (20)

Constructors and Destructors
Constructors and DestructorsConstructors and Destructors
Constructors and Destructors
 
CONSTRUCTORS, DESTRUCTORS AND OPERATOR OVERLOADING.pptx
CONSTRUCTORS, DESTRUCTORS AND OPERATOR OVERLOADING.pptxCONSTRUCTORS, DESTRUCTORS AND OPERATOR OVERLOADING.pptx
CONSTRUCTORS, DESTRUCTORS AND OPERATOR OVERLOADING.pptx
 
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCECONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
 
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
 
Constructors in C++.pptx
Constructors in C++.pptxConstructors in C++.pptx
Constructors in C++.pptx
 
Constructor,destructors cpp
Constructor,destructors cppConstructor,destructors cpp
Constructor,destructors cpp
 
C++Constructors
C++ConstructorsC++Constructors
C++Constructors
 
Constructors destructors
Constructors destructorsConstructors destructors
Constructors destructors
 
constructors and destructors
constructors and destructorsconstructors and destructors
constructors and destructors
 
Tut Constructor
Tut ConstructorTut Constructor
Tut Constructor
 
Operator overload rr
Operator overload  rrOperator overload  rr
Operator overload rr
 
chapter-9-constructors.pdf
chapter-9-constructors.pdfchapter-9-constructors.pdf
chapter-9-constructors.pdf
 
Constructors & Destructors [Compatibility Mode].pdf
Constructors & Destructors [Compatibility Mode].pdfConstructors & Destructors [Compatibility Mode].pdf
Constructors & Destructors [Compatibility Mode].pdf
 
Constructor in c++
Constructor in c++Constructor in c++
Constructor in c++
 
Constructors and destructors in C++
Constructors and destructors in  C++Constructors and destructors in  C++
Constructors and destructors in C++
 
Java Programming - 04 object oriented in java
Java Programming - 04 object oriented in javaJava Programming - 04 object oriented in java
Java Programming - 04 object oriented in java
 
Overloading
OverloadingOverloading
Overloading
 
Chapter 6.6
Chapter 6.6Chapter 6.6
Chapter 6.6
 
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
 
Tutconstructordes
TutconstructordesTutconstructordes
Tutconstructordes
 

More from Dr Sukhpal Singh Gill

RESEARCH METHODOLOGY: A PRACTITIONER APPROACH
RESEARCH METHODOLOGY: A PRACTITIONER APPROACHRESEARCH METHODOLOGY: A PRACTITIONER APPROACH
RESEARCH METHODOLOGY: A PRACTITIONER APPROACH
Dr Sukhpal Singh Gill
 
Cloud Data Centers and the Challenge of Sustainable Energy
Cloud Data Centers and the Challenge of Sustainable EnergyCloud Data Centers and the Challenge of Sustainable Energy
Cloud Data Centers and the Challenge of Sustainable Energy
Dr Sukhpal Singh Gill
 
If you know nothing about HTML, this is where you can start !!
If you know nothing about HTML, this is where you can start !!If you know nothing about HTML, this is where you can start !!
If you know nothing about HTML, this is where you can start !!
Dr Sukhpal Singh Gill
 
Software Requirement Specification
Software Requirement SpecificationSoftware Requirement Specification
Software Requirement Specification
Dr Sukhpal Singh Gill
 
Introduction to RDF
Introduction to RDFIntroduction to RDF
Introduction to RDF
Dr Sukhpal Singh Gill
 
Network Topologies
Network TopologiesNetwork Topologies
Network Topologies
Dr Sukhpal Singh Gill
 
How to Write an Effective Research Paper
How to Write an Effective Research PaperHow to Write an Effective Research Paper
How to Write an Effective Research Paper
Dr Sukhpal Singh Gill
 
GREEN CLOUD COMPUTING-A Data Center Approach
GREEN CLOUD COMPUTING-A Data Center ApproachGREEN CLOUD COMPUTING-A Data Center Approach
GREEN CLOUD COMPUTING-A Data Center Approach
Dr Sukhpal Singh Gill
 
End-to-End Security in Mobile-Cloud Computing
End-to-End Security in Mobile-Cloud ComputingEnd-to-End Security in Mobile-Cloud Computing
End-to-End Security in Mobile-Cloud Computing
Dr Sukhpal Singh Gill
 
Java.NET: Integration of Java and .NET
Java.NET: Integration of Java and .NETJava.NET: Integration of Java and .NET
Java.NET: Integration of Java and .NET
Dr Sukhpal Singh Gill
 
Software Verification, Validation and Testing
Software Verification, Validation and TestingSoftware Verification, Validation and Testing
Software Verification, Validation and Testing
Dr Sukhpal Singh Gill
 
Software Requirements Specification (SRS) for Online Tower Plotting System (O...
Software Requirements Specification (SRS) for Online Tower Plotting System (O...Software Requirements Specification (SRS) for Online Tower Plotting System (O...
Software Requirements Specification (SRS) for Online Tower Plotting System (O...
Dr Sukhpal Singh Gill
 
Reduction of Blocking Artifacts In JPEG Compressed Image
 Reduction of Blocking Artifacts In JPEG Compressed Image Reduction of Blocking Artifacts In JPEG Compressed Image
Reduction of Blocking Artifacts In JPEG Compressed Image
Dr Sukhpal Singh Gill
 
Workshop on Basics of Software Engineering (DFD, UML and Project Culture)
Workshop on Basics of Software Engineering (DFD, UML and Project Culture)Workshop on Basics of Software Engineering (DFD, UML and Project Culture)
Workshop on Basics of Software Engineering (DFD, UML and Project Culture)
Dr Sukhpal Singh Gill
 
Case Study Based Software Engineering Project Development: State of Art
Case Study Based Software Engineering Project Development: State of ArtCase Study Based Software Engineering Project Development: State of Art
Case Study Based Software Engineering Project Development: State of Art
Dr Sukhpal Singh Gill
 
Reduction of Blocking Artifacts In JPEG Compressed Image
Reduction of Blocking Artifacts In JPEG Compressed ImageReduction of Blocking Artifacts In JPEG Compressed Image
Reduction of Blocking Artifacts In JPEG Compressed Image
Dr Sukhpal Singh Gill
 
Reusability Framework for Cloud Computing
Reusability Framework for Cloud ComputingReusability Framework for Cloud Computing
Reusability Framework for Cloud Computing
Dr Sukhpal Singh Gill
 
Topological methods
Topological methods Topological methods
Topological methods
Dr Sukhpal Singh Gill
 

More from Dr Sukhpal Singh Gill (19)

RESEARCH METHODOLOGY: A PRACTITIONER APPROACH
RESEARCH METHODOLOGY: A PRACTITIONER APPROACHRESEARCH METHODOLOGY: A PRACTITIONER APPROACH
RESEARCH METHODOLOGY: A PRACTITIONER APPROACH
 
Cloud Data Centers and the Challenge of Sustainable Energy
Cloud Data Centers and the Challenge of Sustainable EnergyCloud Data Centers and the Challenge of Sustainable Energy
Cloud Data Centers and the Challenge of Sustainable Energy
 
If you know nothing about HTML, this is where you can start !!
If you know nothing about HTML, this is where you can start !!If you know nothing about HTML, this is where you can start !!
If you know nothing about HTML, this is where you can start !!
 
Software Requirement Specification
Software Requirement SpecificationSoftware Requirement Specification
Software Requirement Specification
 
Introduction to RDF
Introduction to RDFIntroduction to RDF
Introduction to RDF
 
Network Topologies
Network TopologiesNetwork Topologies
Network Topologies
 
How to Write an Effective Research Paper
How to Write an Effective Research PaperHow to Write an Effective Research Paper
How to Write an Effective Research Paper
 
GREEN CLOUD COMPUTING-A Data Center Approach
GREEN CLOUD COMPUTING-A Data Center ApproachGREEN CLOUD COMPUTING-A Data Center Approach
GREEN CLOUD COMPUTING-A Data Center Approach
 
End-to-End Security in Mobile-Cloud Computing
End-to-End Security in Mobile-Cloud ComputingEnd-to-End Security in Mobile-Cloud Computing
End-to-End Security in Mobile-Cloud Computing
 
Java.NET: Integration of Java and .NET
Java.NET: Integration of Java and .NETJava.NET: Integration of Java and .NET
Java.NET: Integration of Java and .NET
 
Software Verification, Validation and Testing
Software Verification, Validation and TestingSoftware Verification, Validation and Testing
Software Verification, Validation and Testing
 
Software Requirements Specification (SRS) for Online Tower Plotting System (O...
Software Requirements Specification (SRS) for Online Tower Plotting System (O...Software Requirements Specification (SRS) for Online Tower Plotting System (O...
Software Requirements Specification (SRS) for Online Tower Plotting System (O...
 
Reduction of Blocking Artifacts In JPEG Compressed Image
 Reduction of Blocking Artifacts In JPEG Compressed Image Reduction of Blocking Artifacts In JPEG Compressed Image
Reduction of Blocking Artifacts In JPEG Compressed Image
 
Workshop on Basics of Software Engineering (DFD, UML and Project Culture)
Workshop on Basics of Software Engineering (DFD, UML and Project Culture)Workshop on Basics of Software Engineering (DFD, UML and Project Culture)
Workshop on Basics of Software Engineering (DFD, UML and Project Culture)
 
Case Study Based Software Engineering Project Development: State of Art
Case Study Based Software Engineering Project Development: State of ArtCase Study Based Software Engineering Project Development: State of Art
Case Study Based Software Engineering Project Development: State of Art
 
Reduction of Blocking Artifacts In JPEG Compressed Image
Reduction of Blocking Artifacts In JPEG Compressed ImageReduction of Blocking Artifacts In JPEG Compressed Image
Reduction of Blocking Artifacts In JPEG Compressed Image
 
Reusability Framework for Cloud Computing
Reusability Framework for Cloud ComputingReusability Framework for Cloud Computing
Reusability Framework for Cloud Computing
 
The reuse capability model
The reuse capability modelThe reuse capability model
The reuse capability model
 
Topological methods
Topological methods Topological methods
Topological methods
 

Recently uploaded

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
 
Basic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumersBasic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumers
PedroFerreira53928
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
Atul Kumar Singh
 
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
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
Special education needs
 
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
 
Sectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdfSectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdf
Vivekanand Anglo Vedic Academy
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
Balvir Singh
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
JosvitaDsouza2
 
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.
 
How to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERPHow to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERP
Celine George
 
Instructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxInstructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptx
Jheel Barad
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
EverAndrsGuerraGuerr
 
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
 
How to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS ModuleHow to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS Module
Celine George
 
How to Break the cycle of negative Thoughts
How to Break the cycle of negative ThoughtsHow to Break the cycle of negative Thoughts
How to Break the cycle of negative Thoughts
Col Mukteshwar Prasad
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
Thiyagu K
 
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
 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
MIRIAMSALINAS13
 
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
 

Recently uploaded (20)

Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
 
Basic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumersBasic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumers
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
 
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 ...
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
 
Template Jadual Bertugas Kelas (Boleh Edit)
Template Jadual Bertugas Kelas (Boleh Edit)Template Jadual Bertugas Kelas (Boleh Edit)
Template Jadual Bertugas Kelas (Boleh Edit)
 
Sectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdfSectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdf
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
 
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
 
How to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERPHow to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERP
 
Instructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxInstructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptx
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
 
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
 
How to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS ModuleHow to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS Module
 
How to Break the cycle of negative Thoughts
How to Break the cycle of negative ThoughtsHow to Break the cycle of negative Thoughts
How to Break the cycle of negative Thoughts
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
 
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
 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
 
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
 

Constructors and Destructors

  • 2. WHAT IS A CONSTRUCTOR ?  In c++ , a constructor is a ‘special’ member function whose task is to initialize the objects of its class.  It is special because its name is the same as the class name.  It is called constructor because it constructs the value of data members of the class.  The constructor is invoked whenever an object of its associated class is created. ssgill©tupatiala
  • 3. CLASS WITH A CONSTRUCTOR Class integer { int m, n; public: integer(void); // constructor declared }; Integer : : integer (void) // constructor defined { m=0; n=0; } Object declartion: Integer int1; // object int1 created ssgill©tupatiala
  • 4. CHARACTERISTICS OF THE CONSTRUCTOR  They should be declared in public section.  They are invoked automatically when the objects are created.  They don't have return types, cannot return values.  A constructor that accepts no parameters is called default constructor. The default constructor for class A is A:: A() . The statement A a; invokes the default constructor of the compiler to create the object a. ssgill©tupatiala
  • 5. PARAMETERIZED CONSTRUCTOR The constructor that can take arguments. Class integer { int m, n; public: integer(int x, int y); //parameterized constructor }; Integer : : integer (int x, int y) { m=x; n=y; } Object declaration statement “integer int1 ; “ do not work here. ssgill©tupatiala
  • 6. CALL IN PARAMETERIZED CONSTRUCTOR  We must pass the initial values as arguments to the constructor function when object is created. This can be done in two ways:  By calling the constructor explicitly. integer int1 = integer(0, 100);  By calling the constructor implicitly. integer int1(0, 100); This method is also called shorthand method. ssgill©tupatiala
  • 7. EXPLICIT/IMPLICIT CALL int main() { integer int1(0, 100); //implicit call integer int2 = integer(25, 75); //explicit call cout<<“object 1:” int1.function1(); cout<<“object 2:” int2.function1(); return 0; } Output object1: m=0 and n= 100 object2: m=25 and n= 75 ssgill©tupatiala
  • 8. MULTIPLE CONSTRUCTOR IN A CLASS Class integer { int m, n; public: integer() //default constructor {m=0; n=0;} integer(int a, int b) //parameterized constructor {m=a; n=b;} integer(integer & i) //copy constructor {m= i.m; n= i.n;} }; ssgill©tupatiala
  • 9. CONSTRUCTORS WITH DEFAULT ARGUMENTS complex(float real , float imag=0) { }  complex c1(5.0);  complex c2(2.0,3.0);  Difference b/w A::A() and A::A(int a=0). ssgill©tupatiala
  • 10. DYNAMIC INITIALIZATION OF OBJECTS Provide various initialization formats using overloaded constructors.  Fixed_deposit() { }  Fixed_deposit(long int p, int y ,float r=0.12);  Fixed_deposit( long int p, int y, int r); ssgill©tupatiala
  • 11. COPY CONSTRUCTOR  It is used to declare and initialize an object from another object. e.g • code() { } • code( int a) {id = a;} • code( code &x){ id = x.id; } //copy constructor.  And various calls for them are: • code A(100); // object A is Created and initialized. • code B(A); // copy constructor called. • code C = A; // copy constructor called again. • code D; // object D is created , not initialized D=A; // copy constructor is not called. ssgill©tupatiala
  • 12. DYNAMIC CONSTRUCTORS  It is used to allocate memory while creating objects. • Enable the system to allocate right amount of memory for each object when objects are not of same size. • Thus resulting in saving of memory. ssgill©tupatiala
  • 13. DYNAMIC CONSTRUCTORS class String { char *name; int length; public: String () { length = 0; name = new char[length+1]; } String (char *s) { length = strlen(s); name = new char[length+1]; strcpy(name, s); } }; main() { Char *first = “ME_SE_CR” ; String name1( first); String name2(“ Rupinder”); . . . } ssgill©tupatiala
  • 14. DESTRUCTORS  ~ integer() { }  As new is used to allocate memory in dynamic constructors, here we use delete to free that memory.  Never take any argument nor does it return any value.  It will be invoked implicitly by compiler upon exit from program or a block or a function. ssgill©tupatiala
  • 15. DESTRUCTORS Int count = 0; class test1 { public : ~test1() { count ++; cout <<“n no. of object created “ << count ; } ~test1 () { cout <<“n no. of object destroyed” << count ; Count - - ; } }; main() { cout << “n Enter Main n”; test1 t1, t2, t3, t4; { cout <<“n Entered in block1” ; test1 t5; } { cout <<“n Entered in block2” ; test1 t6; } cout << “n Re-Enter Main n”; return 0; } ssgill©tupatiala
  • 16. OUT PUT  Enter Main No. of object created 1 No. of object created 2 No. of object created 3 No. of object created 4  Enter block 1 No. of object created 5 No. of object destroyed 5  Enter block 2 No. of object created 5 No. of object destroyed 5  Re - Enter Main No. of object destroyed 4 No. of object destroyed 3 No. of object destroyed 2 No. of object destroyed 1 ssgill©tupatiala