Constructors & destructors


Published on

Published in: Education, Technology

Constructors & destructors

  1. 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
  2. 2. 9/7/2012
  3. 3. Constructors It is a special member function of aclass , which is used to construct thememory of object & providesinitialization.Its name is same as class name.It must be declared in public partotherwise result will be error. 9/7/2012
  4. 4. It does not return any value not evenvoid otherwise result will be error.It can be defined by inline /offlinemethod.Does not need to call because it get callautomatically whenever object iscreated.It can be called explicitly also.It can take parameters.Constructer can be overloaded.It does not inherit. 9/7/2012
  5. 5. Destructors•It is a special member function of a class , whichis used to destroy the memory of object•Its name is same as class name but till signfollowed by destructor.•It must be declared in public part otherwiseresult will be error.•It does not return any value not even voidotherwise result will be error.•It can be defined by inline /offline method. 9/7/2012
  6. 6. •Does not need to call because it get callautomatically whenever object destroy from hisscope.•It can be called explicitly also using deleteoperator.•It does not take parameters.•Destructor can not be overloaded , only onedestructor is possible in a class.•It does not inherit. 9/7/2012
  7. 7. Types of constructori. default constructorii. parameterized constructoriii. default parameterized constructoriv. copy constructorv. dynamic constructorTheir is no type of destructor.9/7/2012
  8. 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. 9. Example of default constructor.#include<iostream.h> void main()class demo {{int x,,y; demo d1,d2,d3;//implicitpublic: call of default constructordemo() output- }{ dc-65119x=0,y=0; dc-65125cout<<"n dc"<<(unsigned int)this; dc-65129} od-65129~demo(){ od-65125cout<<"n od"<<(unsigned od-65119int)this;}}; 9/7/2012
  10. 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 d19/7/2012
  11. 11. Example of parameterized constructor#include<iostream.h> void main()class demo {{int x,,y; demo d1,d2;//implicit call of default constructorpublic: demo d3(5,7);// implicit call ofdemo() parameterized constructor{ demo d4=demo(10,12);//explicit call ofx=0,y=0; parameterized constructor output- demo d5; dccout<<"n dc";} } dc~demo() pc{ pccout<<"n od";} dcdemo(int a, int b) od{ od odx=a,y=b; odcout<<"n pc"; 9/7/2012 od}};
  12. 12. Example of default & defaultparameterized constructor void main()#include<iostream.h>class demo { output demo d1,d2;//implicit call of default Dc{int x,,y,z; Dc constructorpublic: Dpc demo d3(5,7);// implicit call of default Dpcdemo() par. constructor Dpc{ demo d4(1,2,3);//explicit call of default dpcx=0,y=0,z=0; par. constructor dpccout<<"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 Odcout<<"n od";} Od object Oddemo(int a, int b,int c=10) d2=demo(1,7,1);//explicit call for Od{ existing object Odx=a,y=b,z=c; } Odcout<<"n dpc";}}; 9/7/2012
  13. 13. Invalid definition of default parameterized constructorCase 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
  14. 14. Copy constructor9/7/2012
  15. 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. 16. case 3-when ever any function return object as a return valuecompiler create a temporary object using copy constructor toreturn this value to one fn to another fn.Ex-demo call(_ _ _){demo c;...return c;}NOTE-Compiler create one temporary object usingcopy constructor to return this value.9/7/2012
  17. 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. 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,dcx=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,odcout<<"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. 19. WAP of sum using copy constructor#inckude<iostream.h>class demo demo sum(demo a, demo b) Output-{ { demo c; Dc,dcint x,,y; c.x=a.x+b.x; Pc,pcpublic: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; ddemo(int a, int b) demo d3(5,7); Od,od,{x=a,y=b; demo d4=demo(10,12); Od,odcout<<"n pc";} d2=d1.sum(d3,d4);demo (const demo &d) }{x=d.x;y=d.y;cout<<"n cc";}}; 9/7/2012
  20. 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. 21. BIBLIOGRAPHY:••• Computer science C++ by Sumita Arora.• C++ by E Balagurusamy.9/7/2012
  22. 22. 9/7/2012