SlideShare a Scribd company logo
4
Most read
7
Most read
16
Most read
Constructors
• Characteristics:
1. The constructor is a special member function of a
class.
2. The constructor name is same as the class name.
3. The constructor is invoked automatically whenever
an object of its associated class is created.
4. It is called constructor because it constructs the
values of the data member of the class.
Constructors
• Characteristics:
1. The constructor do not have return type even void.
2. The constructor must be declared in the public
section.
3. The constructor cannot be virtual.
4. When we do not create any constructor in our class,
C++ compiler generates a default constructor and
insert it into our code.
Constructors
• Types of constructor:
1. Default constructor
2. Parameterized constructor
3. Copy constructor.
Constructors
• Default constructor:
 It is the constructor which doesn’t take any
argument. It has no parameters.
• Parameterized constructor:
 It is the constructor which has parameters. It
allows us to pass arguments while object creation.
Constructors
• Example:
class addition
{
int num;
public:
addition(); // default constructor
addition(int); // parameterized constructor
void sum( addition, addition );
void display();
};
int main()
{
addition a(10), b(20); // parameterized constructor invoked
addition c; // default constructor invoked
c.sum(a,b);
c.display( );
}
Constructors
addition::addition() //Definition of default constructor
{
num=0;
}
addition::addition(int x) //Definition of parameterized constructor
{
num=x;
}
void addition::sum(addition m, addition n)
{
num=m.num+n.num;
}
void addition::display()
{
cout<<“nAddition is:”<<num;
}
Output - Addition is:30
Constructors
• Copy Constructor:
 It is used to create a new object as a copy of an
existing object.
 When we need to initialize the variable of object with
the values of variable of another object of same type,
then we use concept of copy constructor.
 The copy constructor is invoked if:
a) Pass an object as an parameter to a call-by-value
function:
void addition::sum(addition m, addition n)
{
num=m.num+n.num;
}
Constructors
• Copy Constructor:
 The copy constructor is invoked if:
a) Return an object from a function:
friend addition sum( addition, addition ); //declaration
addition sum(addition x, addition y) //definition
{
addition temp;
temp.num=x.num+y.num;
return temp; //copy constructor invoked
}
c=sum(a,b); //calling
Constructors
• Copy Constructor:
 The copy constructor is invoked if:
a) Initialize an object from another object of the same
type
class item
{
int num;
public:
item() {num=10;}
item( item &x) { num=x.num} //copy constructor declaration and definition
void display() { cout<<“n Number is:”<<num; }
};
Constructors
• Copy Constructor:
int main()
{
item a;
item b(a); // copy constructor invoked
item c=b; // copy constructor invoked
a.display();
b.display();
c.display();
}
Output:
Number is:10
Number is:10
Number is:10
Destructor
 A destructor is a special member function that
destroy (or delete) the object.
 A destructor is called automatically when
 The program finished execution.
 A scope (the { } parenthesis) containing object ends.
 Call the delete operator.
Destructor
class book
{
int price;
public:
book()
~book(); //destructor declaration
void display();
};
book::book()
{
price=200;
cout<<“nConstructor”;
}
Book::~book() //destructor definition
{
cout<<“nDestructor”;
}
Void book::display()
{
cout<<“nPrice is:”<<price;
}
Destructor
int main()
{
book b;
b.display();
} //destructor invoked
Output:
Price is:200
Constructor
Destructor
Assignments:
 Write a class complex as follows:
Data members: Real and Imaginary members.
Member functions: get data (use constructor), show
data, add, subtract, multiply and divide.
Use the concept of constructors and destructor.
 A bag consists of zero or more objects of the same type. Each
object can be described by its color and weight. Design C++
program to create a new object. This can be done in two ways.
If the user provides information about color and/or weight of
the object to be created then this information will be used to
create the object otherwise the object will be created using
default values for these attributes. Provide a facility to keep
track of total number of objects and total weight of object from
a bag. Use the concept of constructors and destructor..
References:
 E Balagurusamy, “ Object-Oriented Programming with
C++”, Tata McGraw-Hill Education, 7th edition.
 Schildt Herbert ,”C++: The Complete Reference”, 5th
edition.
Thank You
http://www.isquareit.edu.in/

More Related Content

Similar to constructor in object oriented program.pptx (20)

PPTX
[OOP - Lec 13,14,15] Constructors / Destructor and its Types
Muhammad Hammad Waseem
 
PPTX
Constructors in C++.pptx
Rassjb
 
PPTX
Constructors and Destructors
Keyur Vadodariya
 
PPT
Friend this-new&delete
Shehzad Rizwan
 
PPTX
Constructor and desturctor
Somnath Kulkarni
 
PDF
Constructors & Destructors [Compatibility Mode].pdf
LadallaRajKumar
 
DOCX
Constructor-Types of Constructor:default,parameterized,copy constructor-Destr...
rajalakshmisf687
 
PDF
Constructor and Destructor
Kamal Acharya
 
PPTX
Constructor & destructor
Saharsh Anand
 
PPTX
Constructors and Destructors in C++.pptx
shivanigarg18041
 
PDF
Constructor and Destructor.pdf
MadnessKnight
 
PPT
Constructors and destructors in C++
RAJ KUMAR
 
PDF
Const-Dest-PPT-5_removedhbfhfhfhdhfdhd.pdf
mohitsinha7739289047
 
PPT
Constructor and destructor in C++
Lovely Professional University
 
PDF
Constructors and Destructors
Dr Sukhpal Singh Gill
 
PPT
Constructors and destructors in C++ part 2
Lovely Professional University
 
PPTX
constructorsfjy5ediykEASFul;IUWORHusi;gfb.pptx
AshrithaRokkam
 
PPTX
OOP-Lecture-05 (Constructor_Destructor).pptx
SirRafiLectures
 
PPT
constructor-.pptcucfkifkficuvguvucufjfugugigig
SILENTGAMER45
 
PDF
Constructors or destructors unit(II).pdf
malviyatanishk8
 
[OOP - Lec 13,14,15] Constructors / Destructor and its Types
Muhammad Hammad Waseem
 
Constructors in C++.pptx
Rassjb
 
Constructors and Destructors
Keyur Vadodariya
 
Friend this-new&delete
Shehzad Rizwan
 
Constructor and desturctor
Somnath Kulkarni
 
Constructors & Destructors [Compatibility Mode].pdf
LadallaRajKumar
 
Constructor-Types of Constructor:default,parameterized,copy constructor-Destr...
rajalakshmisf687
 
Constructor and Destructor
Kamal Acharya
 
Constructor & destructor
Saharsh Anand
 
Constructors and Destructors in C++.pptx
shivanigarg18041
 
Constructor and Destructor.pdf
MadnessKnight
 
Constructors and destructors in C++
RAJ KUMAR
 
Const-Dest-PPT-5_removedhbfhfhfhdhfdhd.pdf
mohitsinha7739289047
 
Constructor and destructor in C++
Lovely Professional University
 
Constructors and Destructors
Dr Sukhpal Singh Gill
 
Constructors and destructors in C++ part 2
Lovely Professional University
 
constructorsfjy5ediykEASFul;IUWORHusi;gfb.pptx
AshrithaRokkam
 
OOP-Lecture-05 (Constructor_Destructor).pptx
SirRafiLectures
 
constructor-.pptcucfkifkficuvguvucufjfugugigig
SILENTGAMER45
 
Constructors or destructors unit(II).pdf
malviyatanishk8
 

More from urvashipundir04 (20)

PPTX
stack in python using different datatypes.pptx
urvashipundir04
 
PPTX
Game Playing in Artificial intelligence.pptx
urvashipundir04
 
PPTX
extended modelling in dbms using different.pptx
urvashipundir04
 
PPTX
PRODUCTION SYSTEM in data science .pptx
urvashipundir04
 
PPTX
Presentation1 in datamining using techn.pptx
urvashipundir04
 
PPTX
Dependency modelling in data mining.pptx
urvashipundir04
 
PPTX
INTRODUCTION to datawarehouse IN DATA.pptx
urvashipundir04
 
PPTX
SOCIAL NETWORK ANALYISI in engeenireg.pptx
urvashipundir04
 
PPTX
datamining in engerring using different techniques.pptx
urvashipundir04
 
PPTX
datamining IN Artificial intelligence.pptx
urvashipundir04
 
PPTX
Underfitting and Overfitting in Machine Learning.pptx
urvashipundir04
 
PPTX
introduction values and best practices in
urvashipundir04
 
PPTX
ppt on different topics of circular.pptx
urvashipundir04
 
PPTX
list in python and traversal of list.pptx
urvashipundir04
 
PPT
ermodelN in database management system.ppt
urvashipundir04
 
PPTX
libraries in python using different .pptx
urvashipundir04
 
PPTX
tuple in python is an impotant topic.pptx
urvashipundir04
 
PPTX
ANIMATION in computer graphics using 3 D.pptx
urvashipundir04
 
PPTX
dispaly subroutines in computer graphics .pptx
urvashipundir04
 
PPTX
loopin gstatement in python using .pptx
urvashipundir04
 
stack in python using different datatypes.pptx
urvashipundir04
 
Game Playing in Artificial intelligence.pptx
urvashipundir04
 
extended modelling in dbms using different.pptx
urvashipundir04
 
PRODUCTION SYSTEM in data science .pptx
urvashipundir04
 
Presentation1 in datamining using techn.pptx
urvashipundir04
 
Dependency modelling in data mining.pptx
urvashipundir04
 
INTRODUCTION to datawarehouse IN DATA.pptx
urvashipundir04
 
SOCIAL NETWORK ANALYISI in engeenireg.pptx
urvashipundir04
 
datamining in engerring using different techniques.pptx
urvashipundir04
 
datamining IN Artificial intelligence.pptx
urvashipundir04
 
Underfitting and Overfitting in Machine Learning.pptx
urvashipundir04
 
introduction values and best practices in
urvashipundir04
 
ppt on different topics of circular.pptx
urvashipundir04
 
list in python and traversal of list.pptx
urvashipundir04
 
ermodelN in database management system.ppt
urvashipundir04
 
libraries in python using different .pptx
urvashipundir04
 
tuple in python is an impotant topic.pptx
urvashipundir04
 
ANIMATION in computer graphics using 3 D.pptx
urvashipundir04
 
dispaly subroutines in computer graphics .pptx
urvashipundir04
 
loopin gstatement in python using .pptx
urvashipundir04
 
Ad

Recently uploaded (20)

PPTX
Snet+Pro+Service+Software_SNET+Pro+2+Instructions.pptx
jenilsatikuvar1
 
PDF
Basic_Concepts_in_Clinical_Biochemistry_2018كيمياء_عملي.pdf
AdelLoin
 
PDF
Biomechanics of Gait: Engineering Solutions for Rehabilitation (www.kiu.ac.ug)
publication11
 
PPTX
Lecture 1 Shell and Tube Heat exchanger-1.pptx
mailforillegalwork
 
PDF
International Journal of Information Technology Convergence and services (IJI...
ijitcsjournal4
 
DOCX
CS-802 (A) BDH Lab manual IPS Academy Indore
thegodhimself05
 
PDF
MAD Unit - 2 Activity and Fragment Management in Android (Diploma IT)
JappanMavani
 
PDF
GTU Civil Engineering All Semester Syllabus.pdf
Vimal Bhojani
 
PPTX
GitOps_Without_K8s_Training_detailed git repository
DanialHabibi2
 
PPTX
Solar Thermal Energy System Seminar.pptx
Gpc Purapuza
 
PDF
Zilliz Cloud Demo for performance and scale
Zilliz
 
PDF
Pressure Measurement training for engineers and Technicians
AIESOLUTIONS
 
PPTX
The Role of Information Technology in Environmental Protectio....pptx
nallamillisriram
 
PDF
Water Design_Manual_2005. KENYA FOR WASTER SUPPLY AND SEWERAGE
DancanNgutuku
 
PPTX
MobileComputingMANET2023 MobileComputingMANET2023.pptx
masterfake98765
 
PPT
PPT2_Metal formingMECHANICALENGINEEIRNG .ppt
Praveen Kumar
 
PPTX
Introduction to Neural Networks and Perceptron Learning Algorithm.pptx
Kayalvizhi A
 
PPTX
Arduino Based Gas Leakage Detector Project
CircuitDigest
 
PPTX
原版一样(Acadia毕业证书)加拿大阿卡迪亚大学毕业证办理方法
Taqyea
 
PPTX
UNIT DAA PPT cover all topics 2021 regulation
archu26
 
Snet+Pro+Service+Software_SNET+Pro+2+Instructions.pptx
jenilsatikuvar1
 
Basic_Concepts_in_Clinical_Biochemistry_2018كيمياء_عملي.pdf
AdelLoin
 
Biomechanics of Gait: Engineering Solutions for Rehabilitation (www.kiu.ac.ug)
publication11
 
Lecture 1 Shell and Tube Heat exchanger-1.pptx
mailforillegalwork
 
International Journal of Information Technology Convergence and services (IJI...
ijitcsjournal4
 
CS-802 (A) BDH Lab manual IPS Academy Indore
thegodhimself05
 
MAD Unit - 2 Activity and Fragment Management in Android (Diploma IT)
JappanMavani
 
GTU Civil Engineering All Semester Syllabus.pdf
Vimal Bhojani
 
GitOps_Without_K8s_Training_detailed git repository
DanialHabibi2
 
Solar Thermal Energy System Seminar.pptx
Gpc Purapuza
 
Zilliz Cloud Demo for performance and scale
Zilliz
 
Pressure Measurement training for engineers and Technicians
AIESOLUTIONS
 
The Role of Information Technology in Environmental Protectio....pptx
nallamillisriram
 
Water Design_Manual_2005. KENYA FOR WASTER SUPPLY AND SEWERAGE
DancanNgutuku
 
MobileComputingMANET2023 MobileComputingMANET2023.pptx
masterfake98765
 
PPT2_Metal formingMECHANICALENGINEEIRNG .ppt
Praveen Kumar
 
Introduction to Neural Networks and Perceptron Learning Algorithm.pptx
Kayalvizhi A
 
Arduino Based Gas Leakage Detector Project
CircuitDigest
 
原版一样(Acadia毕业证书)加拿大阿卡迪亚大学毕业证办理方法
Taqyea
 
UNIT DAA PPT cover all topics 2021 regulation
archu26
 
Ad

constructor in object oriented program.pptx

  • 1. Constructors • Characteristics: 1. The constructor is a special member function of a class. 2. The constructor name is same as the class name. 3. The constructor is invoked automatically whenever an object of its associated class is created. 4. It is called constructor because it constructs the values of the data member of the class.
  • 2. Constructors • Characteristics: 1. The constructor do not have return type even void. 2. The constructor must be declared in the public section. 3. The constructor cannot be virtual. 4. When we do not create any constructor in our class, C++ compiler generates a default constructor and insert it into our code.
  • 3. Constructors • Types of constructor: 1. Default constructor 2. Parameterized constructor 3. Copy constructor.
  • 4. Constructors • Default constructor:  It is the constructor which doesn’t take any argument. It has no parameters. • Parameterized constructor:  It is the constructor which has parameters. It allows us to pass arguments while object creation.
  • 5. Constructors • Example: class addition { int num; public: addition(); // default constructor addition(int); // parameterized constructor void sum( addition, addition ); void display(); }; int main() { addition a(10), b(20); // parameterized constructor invoked addition c; // default constructor invoked c.sum(a,b); c.display( ); }
  • 6. Constructors addition::addition() //Definition of default constructor { num=0; } addition::addition(int x) //Definition of parameterized constructor { num=x; } void addition::sum(addition m, addition n) { num=m.num+n.num; } void addition::display() { cout<<“nAddition is:”<<num; } Output - Addition is:30
  • 7. Constructors • Copy Constructor:  It is used to create a new object as a copy of an existing object.  When we need to initialize the variable of object with the values of variable of another object of same type, then we use concept of copy constructor.  The copy constructor is invoked if: a) Pass an object as an parameter to a call-by-value function: void addition::sum(addition m, addition n) { num=m.num+n.num; }
  • 8. Constructors • Copy Constructor:  The copy constructor is invoked if: a) Return an object from a function: friend addition sum( addition, addition ); //declaration addition sum(addition x, addition y) //definition { addition temp; temp.num=x.num+y.num; return temp; //copy constructor invoked } c=sum(a,b); //calling
  • 9. Constructors • Copy Constructor:  The copy constructor is invoked if: a) Initialize an object from another object of the same type class item { int num; public: item() {num=10;} item( item &x) { num=x.num} //copy constructor declaration and definition void display() { cout<<“n Number is:”<<num; } };
  • 10. Constructors • Copy Constructor: int main() { item a; item b(a); // copy constructor invoked item c=b; // copy constructor invoked a.display(); b.display(); c.display(); } Output: Number is:10 Number is:10 Number is:10
  • 11. Destructor  A destructor is a special member function that destroy (or delete) the object.  A destructor is called automatically when  The program finished execution.  A scope (the { } parenthesis) containing object ends.  Call the delete operator.
  • 12. Destructor class book { int price; public: book() ~book(); //destructor declaration void display(); }; book::book() { price=200; cout<<“nConstructor”; } Book::~book() //destructor definition { cout<<“nDestructor”; } Void book::display() { cout<<“nPrice is:”<<price; }
  • 13. Destructor int main() { book b; b.display(); } //destructor invoked Output: Price is:200 Constructor Destructor
  • 14. Assignments:  Write a class complex as follows: Data members: Real and Imaginary members. Member functions: get data (use constructor), show data, add, subtract, multiply and divide. Use the concept of constructors and destructor.  A bag consists of zero or more objects of the same type. Each object can be described by its color and weight. Design C++ program to create a new object. This can be done in two ways. If the user provides information about color and/or weight of the object to be created then this information will be used to create the object otherwise the object will be created using default values for these attributes. Provide a facility to keep track of total number of objects and total weight of object from a bag. Use the concept of constructors and destructor..
  • 15. References:  E Balagurusamy, “ Object-Oriented Programming with C++”, Tata McGraw-Hill Education, 7th edition.  Schildt Herbert ,”C++: The Complete Reference”, 5th edition.