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

PPTX
PDF
Constructor and Destructor.pdf
PPTX
Constructors in C++.pptx
PPTX
constructocvbcvbcvbcvbr-Destructor (1).pptx
PPTX
An introduction to Constructors and destructors in c++ .pptx
PPTX
Constructors and Destructors in C++.pptx
PPT
Constructor and destructor in C++
PPT
Constructors and destructors in C++ part 2
Constructor and Destructor.pdf
Constructors in C++.pptx
constructocvbcvbcvbcvbr-Destructor (1).pptx
An introduction to Constructors and destructors in c++ .pptx
Constructors and Destructors in C++.pptx
Constructor and destructor in C++
Constructors and destructors in C++ part 2

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

PDF
Constructors and destructors
PPTX
Constructor & destructor
PPT
Constructor,destructors cpp
PPTX
PPTX
constructors shailee.pptxhhhtyygdxixixxxxix
PPTX
Constructor and Destructor
PPT
constructor-.pptcucfkifkficuvguvucufjfugugigig
PPTX
Constructor and destructor
PDF
Constructors or destructors unit(II).pdf
PPTX
.NET with C# - Destructor .pptx
PPT
Constructor & Destructor
PDF
chapter-9-constructors.pdf
PPTX
[OOP - Lec 13,14,15] Constructors / Destructor and its Types
PPTX
Constructors and Destructor_detilaed contents.pptx
PPTX
C++ Constructor and Destructors.pptx
PDF
Const-Dest-PPT-5_removedhbfhfhfhdhfdhd.pdf
PPT
5 Constructors and Destructors
PPTX
C++ Unit-III Lecture-3a-C++ Programming Concepts
PPTX
Constructors & Destructors
PPT
Constructors & Destructors in C++ Simplified
Constructors and destructors
Constructor & destructor
Constructor,destructors cpp
constructors shailee.pptxhhhtyygdxixixxxxix
Constructor and Destructor
constructor-.pptcucfkifkficuvguvucufjfugugigig
Constructor and destructor
Constructors or destructors unit(II).pdf
.NET with C# - Destructor .pptx
Constructor & Destructor
chapter-9-constructors.pdf
[OOP - Lec 13,14,15] Constructors / Destructor and its Types
Constructors and Destructor_detilaed contents.pptx
C++ Constructor and Destructors.pptx
Const-Dest-PPT-5_removedhbfhfhfhdhfdhd.pdf
5 Constructors and Destructors
C++ Unit-III Lecture-3a-C++ Programming Concepts
Constructors & Destructors
Constructors & Destructors in C++ Simplified
Ad

More from urvashipundir04 (20)

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

Recently uploaded (20)

PDF
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
PDF
Operating System & Kernel Study Guide-1 - converted.pdf
PDF
R24 SURVEYING LAB MANUAL for civil enggi
PDF
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
PPTX
UNIT 4 Total Quality Management .pptx
PPTX
Sustainable Sites - Green Building Construction
PDF
composite construction of structures.pdf
DOCX
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
PPTX
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
PPTX
CYBER-CRIMES AND SECURITY A guide to understanding
PDF
Well-logging-methods_new................
DOCX
573137875-Attendance-Management-System-original
PPTX
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
PPTX
OOP with Java - Java Introduction (Basics)
PDF
Model Code of Practice - Construction Work - 21102022 .pdf
PDF
Embodied AI: Ushering in the Next Era of Intelligent Systems
PDF
PPT on Performance Review to get promotions
PPTX
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
PDF
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
PPTX
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
Operating System & Kernel Study Guide-1 - converted.pdf
R24 SURVEYING LAB MANUAL for civil enggi
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
UNIT 4 Total Quality Management .pptx
Sustainable Sites - Green Building Construction
composite construction of structures.pdf
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
CYBER-CRIMES AND SECURITY A guide to understanding
Well-logging-methods_new................
573137875-Attendance-Management-System-original
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
OOP with Java - Java Introduction (Basics)
Model Code of Practice - Construction Work - 21102022 .pdf
Embodied AI: Ushering in the Next Era of Intelligent Systems
PPT on Performance Review to get promotions
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx

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.