Presentation is loading. Please wait.

Presentation is loading. Please wait.

Constructors and Destructors. Constructor Constructor—what’s this? Constructor—what’s this? method used for initializing objects (of certain class) method.

Similar presentations


Presentation on theme: "Constructors and Destructors. Constructor Constructor—what’s this? Constructor—what’s this? method used for initializing objects (of certain class) method."— Presentation transcript:

1 Constructors and Destructors

2 Constructor Constructor—what’s this? Constructor—what’s this? method used for initializing objects (of certain class) method used for initializing objects (of certain class) a recipe for creating object of a given class a recipe for creating object of a given class Constructor—do we need it? Constructor—do we need it? do the class variables need initialization? do the class variables need initialization?

3 Constructor—declaring For the T class: For the T class: T(args); or T::T(args); class point { double x, y; public: // … point(double, double);// no return value, even void point(double);// overloadable point(); };

4 Constructor—an unusual method We cannot not specify the return value (even: no void). We cannot not specify the return value (even: no void). We cannot call it for the already constucted object. We cannot call it for the already constucted object. We cannot get it’s address. We cannot get it’s address. It is not even visible in the class scope and has no name (according to ANSI). It is not even visible in the class scope and has no name (according to ANSI).

5 Calling Constructor with no args (default constructor): Constructor with no args (default constructor): point p1; Others: Others: point p3(10.0, 20.0); // 2 arg. point p2(1.0);// 1 arg. // point p1(); // this would be a call of p1 function point p2=1.0;// this way 1 arg. c. only

6 Calling we may specify which one to call we may specify which one to call point p3=point(10.0, 20.0); point p2=point(1.0); point p1=point(); or: point p3=point::point(10.0, 20.0); point p3=point::point(1.0); point p1=point::point();

7 Calling annonymous objects annonymous objects point pu;// not anonymous object pu pu = point (3); // temporary anonymous object, // it is deleted (right after assignment or // later, „when not needed anymore”) point(20); // temporary anonymous and useless point * ppu; ppu=&point(); // an error hard to trace, correct syntax

8 Defining class point { double x, y; public: // … point(double, double); point(double); point() {x=y=0.0; }; }; inline point::point(double d) { x=y=d; } point::point(double x0, double y0) { x=x0; y=y0; }

9 Initialization list in a definition of a constructor (exclusivelly here) we may use the initialization list in a definition of a constructor (exclusivelly here) we may use the initialization list T() : member(initializer) [, member(initializer) …] { } class point { double x, y; public: // … point():x(0.0), y(0.0) {}; };

10 Initialization list inline point::point(double d) :x(d), y(d) { // all done } point::point(double x, double y) :x(x), y(y)// it is unambiguous !!! { // all done }

11 Initialization list the only way of initializing const and reference members. the only way of initializing const and reference members. in i.l. except the class members (declared in the very class, and not inherited) we may also define the way of constructing virtual and direct base classes. in i.l. except the class members (declared in the very class, and not inherited) we may also define the way of constructing virtual and direct base classes. position on the i.l. is of no importance, initialization is performed in a following order: base classes (virtual and then direct bases in order of declaration), member variables in order of declaration, constructor’s body. position on the i.l. is of no importance, initialization is performed in a following order: base classes (virtual and then direct bases in order of declaration), member variables in order of declaration, constructor’s body.

12 Initialization list point::point(int x, int y):x(x), y(y) {}; //OK. inline point::point(int i) :x(i), y(x) {}; //OK. inline point::point(int i) :y(x), x(i) {}; //OK. // point::point(int i) :y(i), x(y) {} — oops !!!

13 The default constructor if no constructor was declared, then compiler generates the default constructor (constructor with no arguments) of an empty body, for T class: if no constructor was declared, then compiler generates the default constructor (constructor with no arguments) of an empty body, for T class:T::T(){} if we declare any constructor (even constructor with args) then compiler does not generate the default constructor. if we declare any constructor (even constructor with args) then compiler does not generate the default constructor.

14 Order of constructor calling Constructors Constructors 1. base class (classes in order of declaration) 2. class members in order of declaration 3. constructor’s body Important: object class members (and base classes), if not initialized in the initialization list, are actually initialized twice: Important: object class members (and base classes), if not initialized in the initialization list, are actually initialized twice: 1. by the default constructor of their class 2. in the body of constructor of object being created

15 An example class segment { point p1; int number; point p2; }; segment::segment()// what happens here ??? :p1(1.0, 1.0) {p2=point(1.0);};

16 An example class segment { point p1; int number; point p2; };segment::segment() :p1(1.0, 1.0)// constr. p1 (2-arg), constr. p2 (default) {// number not initialized p2=point(1.0);// constr. temp. (1 arg.), assignmemt };// destr. temp.

17 Copy constructor it is for initializing objects using other objects of the same class, for the T class: it is for initializing objects using other objects of the same class, for the T class: T::T(const T &); parameter has to be a reference (otherwise temp. object would be created, that should also be somehow initialized – with the copy constructor!) parameter has to be a reference (otherwise temp. object would be created, that should also be somehow initialized – with the copy constructor!) parameter should be const to permit to call the constructor with the const argument parameter should be const to permit to call the constructor with the const argument

18 Copy constructor for example. c.c. of the point class: for example. c.c. of the point class: point(const point & p) :x(p.x), y(p.y) {} as a copy constructor for class T we may use other constructor that may be called with one argument of type &T: as a copy constructor for class T we may use other constructor that may be called with one argument of type &T: point(const point &, int=7);

19 Compiler generated copy constructor Compiler generates copy constructor if programmer does not define one (other constructors are not important in this case). Compiler generates copy constructor if programmer does not define one (other constructors are not important in this case). Compiler generated copy constructor copies object field by field Compiler generated copy constructor copies object field by field (base classes and object members with their copy constructors – just like in case of initialization list it works also when the class has const members) for the point class: point(const point & p) :x(p.x), y(p.y) {}

20 Destructor What is the destructor? What is the destructor? Easy to guess Easy to guess When do we need it? When do we need it? Even easier Even easier

21 Destructor Destructor of the class T: Destructor of the class T: ~T(); np.: np.: ~point() // no args, no ret. value { cout << ”\nit’s me, The Point (x:” << x << „ y:” << y; << x << „ y:” << y; << „) in a moment I’ll be The Late Point”; << „) in a moment I’ll be The Late Point”;}

22 Destructor as opposed to constructor it is in the class scope as opposed to constructor it is in the class scope we may call it we may call it destructor, if not defined by programmer, is generated by compiler destructor, if not defined by programmer, is generated by compiler it has empty body, but … it has empty body, but …

23 Order of calling constructors and destructors Constructors Constructors 1. base class (classes in order of declaration) 2. class members in order of declaration 3. constructor’s body Destructors – simply opposite order Destructors – simply opposite order 1. destructor’s body 2. destructors of class members (order opposite to declaration) 3. destructor of the base class (classes in order opposite to declaration)

24 Order of calling constructors and destructors objects defined in blocks (local, automatic) objects defined in blocks (local, automatic) constructors are called when the definition is executed (met) constructors are called when the definition is executed (met) destructors after leaving the block, order opposite to constructors destructors after leaving the block, order opposite to constructors global objects (static) global objects (static) constructors are called in an order of objects’ definitions, before calling the main() function constructors are called in an order of objects’ definitions, before calling the main() function destructors in order opposite to constructors, after finishing main(). destructors in order opposite to constructors, after finishing main().

25 Order of calling constructors and destructors dynamic objects are controlled by the programmer (new and delete). dynamic objects are controlled by the programmer (new and delete). memory allocation and a constructor call: when the operator new is executed memory allocation and a constructor call: when the operator new is executed destructor and deallocation: delete destructor and deallocation: delete

26 Constructing and destructing dynamic objects point *pp0=new point; point *pp1=new point(1.0); point *pp2=new point(10.0, 20.0); point *arrPoints=new point[10]; // array of 10 points // initialized with the default constructor // in order of increasing addresses // new T[] – calls only the default constructor delete pp1; delete pp2; delete pp0; delete [] arrPoints;

27 Example point global=777; int f(int) { segment o; } void main() { point local; point *p_local; local=global;local=point(10,20); int a=f(1); for (int i=0; i<2; i++) { point in_block=local; point in_block=local; } p_local = new point(1.0); point local2(10,20); delete p_local; p_local = new point(); }

28 Example point global=777; int f(int) { segment o; }c4, c5, d5, d4 c1 void main() { point local;c2 point *p_local; local=global; local=point(10,20); c3, d3 int a=f(1); for (int i=0; i<2; i++) { point in_block=local;c6, i==1 c7 point in_block=local;c6, i==1 c7 }d6, i==1 d7 }d6, i==1 d7 p_local = new point(1.0);c8 point local2(10,20);c9 delete p_local;d8 p_local = new point();c10 }d9, d2, d1, ???10

29 Example define class: person define class: person class person { int age; int age; char *name, char *name, *lastName; *lastName;public: person(const char *name, const char *lastName, const int age); person(const char *name, const char *lastName, const int age); person(const person & o); person(const person & o); ~person(); ~person();};

30 inline person::person(const char *name, const char *lastName, const int age) :age(age) { person::name=new char[strlen(name) + 1]; person::name=new char[strlen(name) + 1]; strcpy(person::name, name); strcpy(person::name, name); person::lastName=new char[strlen(lastName) + 1]; person::lastName=new char[strlen(lastName) + 1]; strcpy(person::lastName, lastName); strcpy(person::lastName, lastName);} inline person::person(const person & o) :age(o.age), name(new char[strlen(o.name) + 1]), lastName(new char[strlen(o.lastName) + 1]) { strcpy(name, o.name); strcpy(name, o.name); strcpy(lastName, o.lastName); strcpy(lastName, o.lastName);} Example

31 inline person::~person() { delete [] name; delete [] name; delete [] lastName; delete [] lastName;} Example

32 Example define the queue class (FIFO queue of persons) define the queue class (FIFO queue of persons) class queue { person **persons; // array of pointers to persons in queue person **persons; // array of pointers to persons in queue const int capacity;// queue capacity const int capacity;// queue capacity int length;// number of persons currently in queue int length;// number of persons currently in queuepublic: queue(int capacity); queue(int capacity); ~queue(); ~queue(); int insert(const person &o); // 1-no more room, 0-inserted int insert(const person &o); // 1-no more room, 0-inserted int collect(person &o); // 1-empty queue, 0-collected int collect(person &o); // 1-empty queue, 0-collected};

33 queue::queue(int capacity) :capacity(capacity), length(0), persons(new (person*) [capacity]) {}queue::~queue(){ for (int i=0; i

34 int queue::insert(const person &o) { if (length==capacity) if (length==capacity) return 1; return 1; persons[length++]=new person(o); // copy the method’s arg. persons[length++]=new person(o); // copy the method’s arg. return 0; return 0;} Example

35 int queue::collect(person &o) { if (length==0) if (length==0) return 1; return 1; o.~person(); // we do not know (yet) o.~person(); // we do not know (yet) o=*persons[0]; // how to overload the = operator o=*persons[0]; // how to overload the = operator length--; length--; for(int i=0; i

36 int queue::collect(person &o) { if (length==0) if (length==0) return 1; return 1; o.~person(); // we do not know (yet) o.~person(); // we do not know (yet) o=*persons[0];// how to overload the = operator o=*persons[0];// how to overload the = operator length--; length--; for(int i=0; i


Download ppt "Constructors and Destructors. Constructor Constructor—what’s this? Constructor—what’s this? method used for initializing objects (of certain class) method."

Similar presentations


Ads by Google