SlideShare a Scribd company logo
Introductions to
Constructors
Introductions to Constructors:
• When a variable is declared and if it is not initialized, it contains the
garbage value.
• The programmer needs to initialize variables with appropriate values.
• This can be done through the public member functions.
• In the first step the object is created and in the second step the data
members are initialized through public member functions.
• It could be better if the initialization is done at the time the object is
created or declared.
• This can avoid calling the public member function to initialize the data
members. This initialization is possible with constructors.
• The constructor is a special member function of a class that
is executed whenever an object is created.
• A constructor will have same name as the class and it does
not have any return type at all, not even void. Constructors
can be very useful for initializing the data members.
Syntax:
class name (argument list)
{
constructor body;
}
Eg:
Class s4
{
int a,b;
public:
s4 ()// constructor fun
{a=b=0;}
};
Main()
{
s4 s;// constructor fun invoked automatically when an object created
}
 Constructors are 7 type:
1) Default constructor
2) Parameterized constructor
3) Multiple constructor
4) Default argument constructor
5) Dynamic initialization of object
6) Dynamic constructor
7) Copy constructor
Default constructor:
The constructor fun does not having parameters
Class p
{int a,b;
Public:
P()
{a=b=10;}
};
Parameterized constructor:
The constructor fun which having parameters
Class pp
{ int a,b;
Public:
pp( int x, int y)
{a=x;b=y;}
};
Multiple constructor (constructor overloading):
One class contains more than one constructor function Default constructor
and parameterized constructor in one class
class pp
{ int a,b;
Public:
pp() // default constructor
{a=b=10;}
pp(int x, int y)// parameterized constructor {a=b=10;}
{a=x;b=y;}
};
Default argument constructor:
Constructor fun with default arguments
class pp
{ int a,b;
Public:
pp( int x=10,int y=20)// default argument constructor
{a=x;b=y;}
};
A class does not allow to contain both default constructor and default
argument constructor, it leads to an ambiguity error
Dynamic initialization of object:
Class object initialized dynamically ie initial values are provide during
run time, by reading values from main program
class pp
{ int a,b;
Public:
pp( int x,int y)
{a=x;b=y;}
};
Dynamic constructor:
The memory allocation of the object at run time using the memory
management operator new.
New operator is used to allocate memory space at run time.
Pointer-variable = new data type;
int*p; p = new int;
Here 2 bytes of memory is allocated and address is return to the
pointer variable p
Copy constructor:
Copy constructor is used to copy an object. The copy constructor allows the
programmer to create a new object from an existing one by initialization. The
argument of copy constructor is the address of class object
class pp
{ int a,b;
Public:
pp( int x=10,int y=20)// default argument constructor
{a=x;b=y;}
pp( pp &ob) // copy constructor
{a=ob.a;
b=ob.b;}
};

More Related Content

Similar to Introductions to Constructors.pptx

CONSTRUCTORS, DESTRUCTORS AND OPERATOR OVERLOADING.pptx
CONSTRUCTORS, DESTRUCTORS AND OPERATOR OVERLOADING.pptxCONSTRUCTORS, DESTRUCTORS AND OPERATOR OVERLOADING.pptx
CONSTRUCTORS, DESTRUCTORS AND OPERATOR OVERLOADING.pptx
DeepasCSE
 

Similar to Introductions to Constructors.pptx (20)

[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 & Destructors [Compatibility Mode].pdf
Constructors & Destructors [Compatibility Mode].pdfConstructors & Destructors [Compatibility Mode].pdf
Constructors & Destructors [Compatibility Mode].pdf
 
Constructors and Destructors
Constructors and DestructorsConstructors and Destructors
Constructors and Destructors
 
Constructors and destructors in C++
Constructors and destructors in  C++Constructors and destructors in  C++
Constructors and destructors in C++
 
Constructor and Destructor
Constructor and DestructorConstructor and Destructor
Constructor and Destructor
 
Constructor and Destructor.pdf
Constructor and Destructor.pdfConstructor and Destructor.pdf
Constructor and Destructor.pdf
 
C++.pptx
C++.pptxC++.pptx
C++.pptx
 
CONSTRUCTORS, DESTRUCTORS AND OPERATOR OVERLOADING.pptx
CONSTRUCTORS, DESTRUCTORS AND OPERATOR OVERLOADING.pptxCONSTRUCTORS, DESTRUCTORS AND OPERATOR OVERLOADING.pptx
CONSTRUCTORS, DESTRUCTORS AND OPERATOR OVERLOADING.pptx
 
constructor.ppt
constructor.pptconstructor.ppt
constructor.ppt
 
Constructor
ConstructorConstructor
Constructor
 
Constructors and destructors
Constructors and destructorsConstructors and destructors
Constructors and destructors
 
constructor in object oriented program.pptx
constructor in object oriented program.pptxconstructor in object oriented program.pptx
constructor in object oriented program.pptx
 
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
 
object oriented programming language.pptx
object oriented programming language.pptxobject oriented programming language.pptx
object oriented programming language.pptx
 
Constructors and Destructor in C++
Constructors and Destructor in C++Constructors and Destructor in C++
Constructors and Destructor in C++
 
chapter-9-constructors.pdf
chapter-9-constructors.pdfchapter-9-constructors.pdf
chapter-9-constructors.pdf
 
CST 203 Lecture 7.pptx
CST 203 Lecture 7.pptxCST 203 Lecture 7.pptx
CST 203 Lecture 7.pptx
 
C questions
C questionsC questions
C questions
 
OOPs & C++ UNIT 3
OOPs & C++ UNIT 3OOPs & C++ UNIT 3
OOPs & C++ UNIT 3
 
Constructor&method
Constructor&methodConstructor&method
Constructor&method
 

Recently uploaded

RS Khurmi Machine Design Clutch and Brake Exercise Numerical Solutions
RS Khurmi Machine Design Clutch and Brake Exercise Numerical SolutionsRS Khurmi Machine Design Clutch and Brake Exercise Numerical Solutions
RS Khurmi Machine Design Clutch and Brake Exercise Numerical Solutions
Atif Razi
 
Digital Signal Processing Lecture notes n.pdf
Digital Signal Processing Lecture notes n.pdfDigital Signal Processing Lecture notes n.pdf
Digital Signal Processing Lecture notes n.pdf
AbrahamGadissa
 
Online blood donation management system project.pdf
Online blood donation management system project.pdfOnline blood donation management system project.pdf
Online blood donation management system project.pdf
Kamal Acharya
 
Standard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - NeometrixStandard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - Neometrix
Neometrix_Engineering_Pvt_Ltd
 
Hall booking system project report .pdf
Hall booking system project report  .pdfHall booking system project report  .pdf
Hall booking system project report .pdf
Kamal Acharya
 

Recently uploaded (20)

RS Khurmi Machine Design Clutch and Brake Exercise Numerical Solutions
RS Khurmi Machine Design Clutch and Brake Exercise Numerical SolutionsRS Khurmi Machine Design Clutch and Brake Exercise Numerical Solutions
RS Khurmi Machine Design Clutch and Brake Exercise Numerical Solutions
 
RESORT MANAGEMENT AND RESERVATION SYSTEM PROJECT REPORT.pdf
RESORT MANAGEMENT AND RESERVATION SYSTEM PROJECT REPORT.pdfRESORT MANAGEMENT AND RESERVATION SYSTEM PROJECT REPORT.pdf
RESORT MANAGEMENT AND RESERVATION SYSTEM PROJECT REPORT.pdf
 
Introduction to Machine Learning Unit-5 Notes for II-II Mechanical Engineering
Introduction to Machine Learning Unit-5 Notes for II-II Mechanical EngineeringIntroduction to Machine Learning Unit-5 Notes for II-II Mechanical Engineering
Introduction to Machine Learning Unit-5 Notes for II-II Mechanical Engineering
 
Digital Signal Processing Lecture notes n.pdf
Digital Signal Processing Lecture notes n.pdfDigital Signal Processing Lecture notes n.pdf
Digital Signal Processing Lecture notes n.pdf
 
KIT-601 Lecture Notes-UNIT-4.pdf Frequent Itemsets and Clustering
KIT-601 Lecture Notes-UNIT-4.pdf Frequent Itemsets and ClusteringKIT-601 Lecture Notes-UNIT-4.pdf Frequent Itemsets and Clustering
KIT-601 Lecture Notes-UNIT-4.pdf Frequent Itemsets and Clustering
 
ENERGY STORAGE DEVICES INTRODUCTION UNIT-I
ENERGY STORAGE DEVICES  INTRODUCTION UNIT-IENERGY STORAGE DEVICES  INTRODUCTION UNIT-I
ENERGY STORAGE DEVICES INTRODUCTION UNIT-I
 
Online blood donation management system project.pdf
Online blood donation management system project.pdfOnline blood donation management system project.pdf
Online blood donation management system project.pdf
 
Cloud-Computing_CSE311_Computer-Networking CSE GUB BD - Shahidul.pptx
Cloud-Computing_CSE311_Computer-Networking CSE GUB BD - Shahidul.pptxCloud-Computing_CSE311_Computer-Networking CSE GUB BD - Shahidul.pptx
Cloud-Computing_CSE311_Computer-Networking CSE GUB BD - Shahidul.pptx
 
ASME IX(9) 2007 Full Version .pdf
ASME IX(9)  2007 Full Version       .pdfASME IX(9)  2007 Full Version       .pdf
ASME IX(9) 2007 Full Version .pdf
 
NO1 Pandit Amil Baba In Bahawalpur, Sargodha, Sialkot, Sheikhupura, Rahim Yar...
NO1 Pandit Amil Baba In Bahawalpur, Sargodha, Sialkot, Sheikhupura, Rahim Yar...NO1 Pandit Amil Baba In Bahawalpur, Sargodha, Sialkot, Sheikhupura, Rahim Yar...
NO1 Pandit Amil Baba In Bahawalpur, Sargodha, Sialkot, Sheikhupura, Rahim Yar...
 
fluid mechanics gate notes . gate all pyqs answer
fluid mechanics gate notes . gate all pyqs answerfluid mechanics gate notes . gate all pyqs answer
fluid mechanics gate notes . gate all pyqs answer
 
Natalia Rutkowska - BIM School Course in Kraków
Natalia Rutkowska - BIM School Course in KrakówNatalia Rutkowska - BIM School Course in Kraków
Natalia Rutkowska - BIM School Course in Kraków
 
fundamentals of drawing and isometric and orthographic projection
fundamentals of drawing and isometric and orthographic projectionfundamentals of drawing and isometric and orthographic projection
fundamentals of drawing and isometric and orthographic projection
 
The Benefits and Techniques of Trenchless Pipe Repair.pdf
The Benefits and Techniques of Trenchless Pipe Repair.pdfThe Benefits and Techniques of Trenchless Pipe Repair.pdf
The Benefits and Techniques of Trenchless Pipe Repair.pdf
 
Courier management system project report.pdf
Courier management system project report.pdfCourier management system project report.pdf
Courier management system project report.pdf
 
The Ultimate Guide to External Floating Roofs for Oil Storage Tanks.docx
The Ultimate Guide to External Floating Roofs for Oil Storage Tanks.docxThe Ultimate Guide to External Floating Roofs for Oil Storage Tanks.docx
The Ultimate Guide to External Floating Roofs for Oil Storage Tanks.docx
 
WATER CRISIS and its solutions-pptx 1234
WATER CRISIS and its solutions-pptx 1234WATER CRISIS and its solutions-pptx 1234
WATER CRISIS and its solutions-pptx 1234
 
Construction method of steel structure space frame .pptx
Construction method of steel structure space frame .pptxConstruction method of steel structure space frame .pptx
Construction method of steel structure space frame .pptx
 
Standard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - NeometrixStandard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - Neometrix
 
Hall booking system project report .pdf
Hall booking system project report  .pdfHall booking system project report  .pdf
Hall booking system project report .pdf
 

Introductions to Constructors.pptx

  • 2. Introductions to Constructors: • When a variable is declared and if it is not initialized, it contains the garbage value. • The programmer needs to initialize variables with appropriate values. • This can be done through the public member functions. • In the first step the object is created and in the second step the data members are initialized through public member functions. • It could be better if the initialization is done at the time the object is created or declared. • This can avoid calling the public member function to initialize the data members. This initialization is possible with constructors.
  • 3. • The constructor is a special member function of a class that is executed whenever an object is created. • A constructor will have same name as the class and it does not have any return type at all, not even void. Constructors can be very useful for initializing the data members.
  • 4. Syntax: class name (argument list) { constructor body; } Eg: Class s4 { int a,b; public: s4 ()// constructor fun {a=b=0;} }; Main() { s4 s;// constructor fun invoked automatically when an object created }
  • 5.  Constructors are 7 type: 1) Default constructor 2) Parameterized constructor 3) Multiple constructor 4) Default argument constructor 5) Dynamic initialization of object 6) Dynamic constructor 7) Copy constructor
  • 6. Default constructor: The constructor fun does not having parameters Class p {int a,b; Public: P() {a=b=10;} };
  • 7. Parameterized constructor: The constructor fun which having parameters Class pp { int a,b; Public: pp( int x, int y) {a=x;b=y;} };
  • 8. Multiple constructor (constructor overloading): One class contains more than one constructor function Default constructor and parameterized constructor in one class class pp { int a,b; Public: pp() // default constructor {a=b=10;} pp(int x, int y)// parameterized constructor {a=b=10;} {a=x;b=y;} };
  • 9. Default argument constructor: Constructor fun with default arguments class pp { int a,b; Public: pp( int x=10,int y=20)// default argument constructor {a=x;b=y;} }; A class does not allow to contain both default constructor and default argument constructor, it leads to an ambiguity error
  • 10. Dynamic initialization of object: Class object initialized dynamically ie initial values are provide during run time, by reading values from main program class pp { int a,b; Public: pp( int x,int y) {a=x;b=y;} };
  • 11. Dynamic constructor: The memory allocation of the object at run time using the memory management operator new. New operator is used to allocate memory space at run time. Pointer-variable = new data type; int*p; p = new int; Here 2 bytes of memory is allocated and address is return to the pointer variable p
  • 12. Copy constructor: Copy constructor is used to copy an object. The copy constructor allows the programmer to create a new object from an existing one by initialization. The argument of copy constructor is the address of class object class pp { int a,b; Public: pp( int x=10,int y=20)// default argument constructor {a=x;b=y;} pp( pp &ob) // copy constructor {a=ob.a; b=ob.b;} };