SlideShare a Scribd company logo
1 of 22
Topic-
                Constructor & destructor
                                  Submitted by-
                                 Megha Agrawal
           SUBMITTED TO:
                               Enroll. -0101it101039
           I.T. DEPARTMENT           Branch-
           U.I.T.,R.G.P.V.         Information
           BHOPAL              technology(3rd sem.)
9/7/2012
9/7/2012
Constructors
 It is a special member function of a
class , which is used to construct the
memory of object & provides
initialization.
Its name is same as class name.
It must be declared in public part
otherwise result will be error.
  9/7/2012
It does not return any value not even
void otherwise result will be error.
It can be defined by inline /offline
method.
Does not need to call because it get call
automatically whenever object is
created.
It can be called explicitly also.
It can take parameters.
Constructer can be overloaded.
It does not inherit.
 9/7/2012
Destructors

•It is a special member function of a class , which
is used to destroy the memory of object
•Its name is same as class name but till sign
followed by destructor.
•It must be declared in public part otherwise
result will be error.
•It does not return any value not even void
otherwise result will be error.
•It can be defined by inline /offline method.
  9/7/2012
•Does not need to call because it get call
automatically whenever object destroy from his
scope.
•It can be called explicitly also using delete
operator.
•It does not take parameters.
•Destructor can not be overloaded , only one
destructor is possible in a class.
•It does not inherit.


 9/7/2012
Types of constructor
i. default constructor
ii. parameterized constructor
iii. default parameterized constructor
iv. copy constructor
v. dynamic constructor
Their is no type of destructor.



9/7/2012
Example 1-
 #include<iostream.h>
 class demo
 {
 int x,y;
 };
 void main()
 {
 demo d1,d2,d3;
 }

 Note- if we are not defining user define constructor and
  destructor then compiler create its own constructor and
  destructor to create the memory of object and to
  destroy the memory of object but we cannot initialize
  our object in compiler constructor.
 9/7/2012
Example of default constructor.
#include<iostream.h>            void main()
class demo                      {
{int x,,y;                      demo d1,d2,d3;//implicit
public:                            call of default
                                   constructor
demo()                                             output-
                                }
{                                                  dc-65119
x=0,y=0;                                           dc-65125
cout<<"n dc"<<(unsigned int)this;                 dc-65129
}                                                  od-65129
~demo(){                                           od-65125
cout<<"n od"<<(unsigned                           od-65119
int)this;
}};
  9/7/2012
NOTE-
• In this scope compiler create the memory
  of object sequentially and destroy in
  reverse order because "c++"compiler uses
  the concept of stack in memory allocation
  and de allocation.        d3
                                  d2
                             d1
                        d3
           Allocation




                                   Delocation
                        d2
                        d1

9/7/2012
Example of parameterized constructor
#include<iostream.h>   void main()
class demo             {
{int x,,y;             demo d1,d2;//implicit call of default
                          constructor
public:
                       demo d3(5,7);// implicit call of
demo()                    parameterized constructor
{                      demo d4=demo(10,12);//explicit call of
x=0,y=0;                  parameterized constructor output-
                       demo d5;                         dc
cout<<"n dc";}
                       }                                dc
~demo()
                                                     pc
{                                                    pc
cout<<"n od";}                                      dc
demo(int a, int b)                                   od
{                                                    od
                                                     od
x=a,y=b;
                                                     od
cout<<"n pc";
     9/7/2012                                        od
}};
Example of default & default
parameterized constructor
                              void main()
#include<iostream.h>
class demo
                              {                                           output
                              demo d1,d2;//implicit call of default       Dc
{int x,,y,z;                                                              Dc
                                 constructor
public:                                                                   Dpc
                              demo d3(5,7);// implicit call of default    Dpc
demo()                           par. constructor                         Dpc
{                             demo d4(1,2,3);//explicit call of default   dpc
x=0,y=0,z=0;                   par. constructor                           dpc
cout<<"n dc";}                                                           Od
                              demo d5=demo(10,12);                        Dpc
~demo()                       demo d6=demo(1,8,2);                        Od
{                             d1=demo(9,9);//explicit call for existing   Od
cout<<"n od";}                                                           Od
                                 object
                                                                          Od
demo(int a, int b,int c=10)   d2=demo(1,7,1);//explicit call for          Od
{                                existing object                          Od
x=a,y=b,z=c;                  }                                           Od

cout<<"n dpc";}};
   9/7/2012
Invalid definition of default
            parameterized constructor
Case 1-                     Case2-
Demo(int a,int b,int c=8)   Demo(int a,int b=8,int c)
{                           {
----------;                 ----------;
----------;                 ----------;
----------;                 ----------;
}                           }


     Demo( , ,8)             Demo( ,8, )


9/7/2012
Copy constructor



9/7/2012
Its a kind of constructor n gets called in
  following cases-
case 1-
when ever we are going to initialize any new object by existing object.
Ex-
demod4(d2);
demod4=d2;
case2-
when ever we pass object as an arguments a function compiler create the
   memory of formal parameter(object)using copy constructor.
EX-void call(demo a, demo b)
{
demo c;
}
called by-
call(d4 ,d5)

  9/7/2012
case 3-
when ever any function return object as a return value
compiler create a temporary object using copy constructor to
return this value to one fn to another fn.
Ex-
demo call(_ _ _)
{
demo c;
.
.
.
return c;

}
NOTE-Compiler create one temporary object using
copy constructor to return this value.
9/7/2012
syntax:-
class_name(const class_name & ref_object)
{
---
---
---
}
NOTE-
    if ‘&’ is not used in copy constructor compiler will
   create a new object in place of sharing the memory
   and constructor is used to create an object so its a
   recursive process and const keyword is used so that
   no change can be made in existing object.

9/7/2012
Example-
#include<iostream.h>   demo (const demo &d)
class demo             {
{int x,,y;             x=d.x;
public:                y=d.y;
demo()                 cout<<"n cc";                          output-
{                      }};                                      dc,dc
x=0,y=0;               void main()                              pc,pc
                                                               CC,CC,
cout<<"n dc";}        {
                                                               od,od,
~demo()                demo d1,d2;//implicit call of default   od,od,
{                          constructor                         od,od
cout<<"n od";}        demo d3(5,7);
demo(int a, int b)     demo d4=(10,12);
{                      demo d5(D3);
x=a,y=b;               demo d6=d4;
cout<<"n pc";}        }
9/7/2012
WAP of sum using copy constructor
#inckude<iostream.h>
class demo             demo sum(demo a, demo b)
                                                  Output-
{                      {
                       demo c;                     Dc,dc
int x,,y;
                       c.x=a.x+b.x;                Pc,pc
public:
demo()
                       c.y=a.y+b.y;                Cc,cc
                       return c;                    dc
{x=0,y=0;
                       }
cout<<"n dc";}                                    Cc,od
                       void main()
~demo()                {                          Od,od,o
{cout<<"n od";}       demo d1,d2;                   d
demo(int a, int b)     demo d3(5,7);              Od,od,
{x=a,y=b;              demo d4=demo(10,12);        Od,od
cout<<"n pc";}        d2=d1.sum(d3,d4);
demo (const demo &d)   }
{x=d.x;
y=d.y;
cout<<"n cc";}};
 9/7/2012
In user define Constructors &
   Destructors programming, we can not
   create any other type of object if its
   constructor definition is not define
   otherwise result will be compile time
   error.

9/7/2012
BIBLIOGRAPHY:

• www.google.com
• www.wikipedia.com
• Computer science C++ by Sumita
  Arora.
• C++ by E Balagurusamy.


9/7/2012
9/7/2012

More Related Content

What's hot (20)

array of object pointer in c++
array of object pointer in c++array of object pointer in c++
array of object pointer in c++
 
Templates in C++
Templates in C++Templates in C++
Templates in C++
 
Functions in C
Functions in CFunctions in C
Functions in C
 
Function overloading(c++)
Function overloading(c++)Function overloading(c++)
Function overloading(c++)
 
[OOP - Lec 19] Static Member Functions
[OOP - Lec 19] Static Member Functions[OOP - Lec 19] Static Member Functions
[OOP - Lec 19] Static Member Functions
 
Method overriding
Method overridingMethod overriding
Method overriding
 
Recursion in c++
Recursion in c++Recursion in c++
Recursion in c++
 
Constructor
ConstructorConstructor
Constructor
 
Constructors and destructors
Constructors and destructorsConstructors and destructors
Constructors and destructors
 
Pointers in c++
Pointers in c++Pointers in c++
Pointers in c++
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
 
Oop c++class(final).ppt
Oop c++class(final).pptOop c++class(final).ppt
Oop c++class(final).ppt
 
Constructor and Destructor
Constructor and DestructorConstructor and Destructor
Constructor and Destructor
 
Oops in java
Oops in javaOops in java
Oops in java
 
Polymorphism in C++
Polymorphism in C++Polymorphism in C++
Polymorphism in C++
 
String and string buffer
String and string bufferString and string buffer
String and string buffer
 
classes and objects in C++
classes and objects in C++classes and objects in C++
classes and objects in C++
 
File handling in c++
File handling in c++File handling in c++
File handling in c++
 
friend function(c++)
friend function(c++)friend function(c++)
friend function(c++)
 
Function overloading
Function overloadingFunction overloading
Function overloading
 

Similar to Constructors & destructors

Similar to Constructors & destructors (20)

Constructor and Destructors in C++
Constructor and Destructors in C++Constructor and Destructors in C++
Constructor and Destructors in C++
 
Constructor and destructor
Constructor and destructorConstructor and destructor
Constructor and destructor
 
Test Engine
Test EngineTest Engine
Test Engine
 
Lecture07
Lecture07Lecture07
Lecture07
 
Declaring friend function with inline code
Declaring friend function with inline codeDeclaring friend function with inline code
Declaring friend function with inline code
 
C++totural file
C++totural fileC++totural file
C++totural file
 
C++ tutorial
C++ tutorialC++ tutorial
C++ tutorial
 
C++ idioms.pptx
C++ idioms.pptxC++ idioms.pptx
C++ idioms.pptx
 
Test Engine
Test EngineTest Engine
Test Engine
 
L10
L10L10
L10
 
The Big Three
The Big ThreeThe Big Three
The Big Three
 
Constructors and Destructors
Constructors and DestructorsConstructors and Destructors
Constructors and Destructors
 
Introduction to CUDA C: NVIDIA : Notes
Introduction to CUDA C: NVIDIA : NotesIntroduction to CUDA C: NVIDIA : Notes
Introduction to CUDA C: NVIDIA : Notes
 
Objective c beginner's guide
Objective c beginner's guideObjective c beginner's guide
Objective c beginner's guide
 
data Structure Lecture 1
data Structure Lecture 1data Structure Lecture 1
data Structure Lecture 1
 
CONSTRUCTORS, DESTRUCTORS AND OPERATOR OVERLOADING.pptx
CONSTRUCTORS, DESTRUCTORS AND OPERATOR OVERLOADING.pptxCONSTRUCTORS, DESTRUCTORS AND OPERATOR OVERLOADING.pptx
CONSTRUCTORS, DESTRUCTORS AND OPERATOR OVERLOADING.pptx
 
Inheritance and polymorphism
Inheritance and polymorphismInheritance and polymorphism
Inheritance and polymorphism
 
Course1
Course1Course1
Course1
 
Tut Constructor
Tut ConstructorTut Constructor
Tut Constructor
 
More on Classes and Objects
More on Classes and ObjectsMore on Classes and Objects
More on Classes and Objects
 

More from ForwardBlog Enewzletter (10)

Sorting searching
Sorting searchingSorting searching
Sorting searching
 
Pn junction
Pn junctionPn junction
Pn junction
 
Feedback amplifiers
Feedback amplifiersFeedback amplifiers
Feedback amplifiers
 
Oscillators
OscillatorsOscillators
Oscillators
 
Compile time polymorphism
Compile time polymorphismCompile time polymorphism
Compile time polymorphism
 
Oo ps
Oo psOo ps
Oo ps
 
Parameter passing to_functions_in_c
Parameter passing to_functions_in_cParameter passing to_functions_in_c
Parameter passing to_functions_in_c
 
Propositional logic
Propositional logicPropositional logic
Propositional logic
 
Stack a Data Structure
Stack a Data StructureStack a Data Structure
Stack a Data Structure
 
Presentation on graphs
Presentation on graphsPresentation on graphs
Presentation on graphs
 

Recently uploaded

9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room servicediscovermytutordmt
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfAdmir Softic
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhikauryashika82
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxVishalSingh1417
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...fonyou31
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajanpragatimahajan3
 

Recently uploaded (20)

9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room service
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajan
 

Constructors & destructors

  • 1. Topic- Constructor & destructor Submitted by- Megha Agrawal SUBMITTED TO: Enroll. -0101it101039 I.T. DEPARTMENT Branch- U.I.T.,R.G.P.V. Information BHOPAL technology(3rd sem.) 9/7/2012
  • 3. Constructors  It is a special member function of a class , which is used to construct the memory of object & provides initialization. Its name is same as class name. It must be declared in public part otherwise result will be error. 9/7/2012
  • 4. It does not return any value not even void otherwise result will be error. It can be defined by inline /offline method. Does not need to call because it get call automatically whenever object is created. It can be called explicitly also. It can take parameters. Constructer can be overloaded. It does not inherit. 9/7/2012
  • 5. Destructors •It is a special member function of a class , which is used to destroy the memory of object •Its name is same as class name but till sign followed by destructor. •It must be declared in public part otherwise result will be error. •It does not return any value not even void otherwise result will be error. •It can be defined by inline /offline method. 9/7/2012
  • 6. •Does not need to call because it get call automatically whenever object destroy from his scope. •It can be called explicitly also using delete operator. •It does not take parameters. •Destructor can not be overloaded , only one destructor is possible in a class. •It does not inherit. 9/7/2012
  • 7. Types of constructor i. default constructor ii. parameterized constructor iii. default parameterized constructor iv. copy constructor v. dynamic constructor Their is no type of destructor. 9/7/2012
  • 8. Example 1- #include<iostream.h> class demo { int x,y; }; void main() { demo d1,d2,d3; } Note- if we are not defining user define constructor and destructor then compiler create its own constructor and destructor to create the memory of object and to destroy the memory of object but we cannot initialize our object in compiler constructor. 9/7/2012
  • 9. Example of default constructor. #include<iostream.h> void main() class demo { {int x,,y; demo d1,d2,d3;//implicit public: call of default constructor demo() output- } { dc-65119 x=0,y=0; dc-65125 cout<<"n dc"<<(unsigned int)this; dc-65129 } od-65129 ~demo(){ od-65125 cout<<"n od"<<(unsigned od-65119 int)this; }}; 9/7/2012
  • 10. NOTE- • In this scope compiler create the memory of object sequentially and destroy in reverse order because "c++"compiler uses the concept of stack in memory allocation and de allocation. d3 d2 d1 d3 Allocation Delocation d2 d1 9/7/2012
  • 11. Example of parameterized constructor #include<iostream.h> void main() class demo { {int x,,y; demo d1,d2;//implicit call of default constructor public: demo d3(5,7);// implicit call of demo() parameterized constructor { demo d4=demo(10,12);//explicit call of x=0,y=0; parameterized constructor output- demo d5; dc cout<<"n dc";} } dc ~demo() pc { pc cout<<"n od";} dc demo(int a, int b) od { od od x=a,y=b; od cout<<"n pc"; 9/7/2012 od }};
  • 12. Example of default & default parameterized constructor void main() #include<iostream.h> class demo { output demo d1,d2;//implicit call of default Dc {int x,,y,z; Dc constructor public: Dpc demo d3(5,7);// implicit call of default Dpc demo() par. constructor Dpc { demo d4(1,2,3);//explicit call of default dpc x=0,y=0,z=0; par. constructor dpc cout<<"n dc";} Od demo d5=demo(10,12); Dpc ~demo() demo d6=demo(1,8,2); Od { d1=demo(9,9);//explicit call for existing Od cout<<"n od";} Od object Od demo(int a, int b,int c=10) d2=demo(1,7,1);//explicit call for Od { existing object Od x=a,y=b,z=c; } Od cout<<"n dpc";}}; 9/7/2012
  • 13. Invalid definition of default parameterized constructor Case 1- Case2- Demo(int a,int b,int c=8) Demo(int a,int b=8,int c) { { ----------; ----------; ----------; ----------; ----------; ----------; } } Demo( , ,8) Demo( ,8, ) 9/7/2012
  • 15. Its a kind of constructor n gets called in following cases- case 1- when ever we are going to initialize any new object by existing object. Ex- demod4(d2); demod4=d2; case2- when ever we pass object as an arguments a function compiler create the memory of formal parameter(object)using copy constructor. EX-void call(demo a, demo b) { demo c; } called by- call(d4 ,d5) 9/7/2012
  • 16. case 3- when ever any function return object as a return value compiler create a temporary object using copy constructor to return this value to one fn to another fn. Ex- demo call(_ _ _) { demo c; . . . return c; } NOTE-Compiler create one temporary object using copy constructor to return this value. 9/7/2012
  • 17. syntax:- class_name(const class_name & ref_object) { --- --- --- } NOTE- if ‘&’ is not used in copy constructor compiler will create a new object in place of sharing the memory and constructor is used to create an object so its a recursive process and const keyword is used so that no change can be made in existing object. 9/7/2012
  • 18. Example- #include<iostream.h> demo (const demo &d) class demo { {int x,,y; x=d.x; public: y=d.y; demo() cout<<"n cc"; output- { }}; dc,dc x=0,y=0; void main() pc,pc CC,CC, cout<<"n dc";} { od,od, ~demo() demo d1,d2;//implicit call of default od,od, { constructor od,od cout<<"n od";} demo d3(5,7); demo(int a, int b) demo d4=(10,12); { demo d5(D3); x=a,y=b; demo d6=d4; cout<<"n pc";} } 9/7/2012
  • 19. WAP of sum using copy constructor #inckude<iostream.h> class demo demo sum(demo a, demo b) Output- { { demo c; Dc,dc int x,,y; c.x=a.x+b.x; Pc,pc public: demo() c.y=a.y+b.y; Cc,cc return c; dc {x=0,y=0; } cout<<"n dc";} Cc,od void main() ~demo() { Od,od,o {cout<<"n od";} demo d1,d2; d demo(int a, int b) demo d3(5,7); Od,od, {x=a,y=b; demo d4=demo(10,12); Od,od cout<<"n pc";} d2=d1.sum(d3,d4); demo (const demo &d) } {x=d.x; y=d.y; cout<<"n cc";}}; 9/7/2012
  • 20. In user define Constructors & Destructors programming, we can not create any other type of object if its constructor definition is not define otherwise result will be compile time error. 9/7/2012
  • 21. BIBLIOGRAPHY: • www.google.com • www.wikipedia.com • Computer science C++ by Sumita Arora. • C++ by E Balagurusamy. 9/7/2012