You get a bonus - 1 coin for daily activity. Now you have 1 coin

2.2.Constructors and destructors of derived classes

Lecture



Since constructors are not inherited, when creating a derived class, the member data that it inherits must be initialized with the constructor of the base class. The base class constructor is called automatically and runs before the constructor of the derived class. If several base classes are inherited, their constructors are executed in the sequence in which the base classes in the definition of the derived class are listed. The constructor of the derived class is called when the constructors of the base classes are finished. The parameters of the base class constructor are specified in the definition of the constructor of the derived class. Thus, the arguments are passed from the constructor of the derived class to the constructor of the base class.

For example.

class basis

{int a, b;

public:

Basis (int x, int y) {a = x; b = y;}

};

class Inherit: public Basis

{int sum;

public:

Inherit (int x, int y, int s): Basis (x, y) {sum = s;}

};

Remember that the constructor of the base class is called automatically and we specify it in the definition of the constructor of the derived class only to pass arguments to it.

Class objects are constructed from bottom to top: first the base, then the component-objects (if they exist), and then the derived class itself. So the derived class object contains the base class object as a sub-object. Objects are destroyed in the reverse order: first the derived object, then its object components, and then the base object. As we know, the object is destroyed when the program ends or when the object definition is out of scope and these actions are performed by the destructor. The default destructor status is public . Destructors are not inherited, so even if there is no destructor in the derived class, it is not transferred from the base class, but is formed by the compiler as silent. Classes in the hierarchy must have virtual destructors at their disposal. Destructors can be redefined, but not overloaded. In any class, other classes may be defined as components. In these classes, there may be destructors of their own, which, when destroying an object of the enclosing (external) class, are executed after the destructor of the enclosing class. Base class destructors are executed in the reverse order of enumeration of classes in the definition of a derived class. Thus, the order of destruction of an object is opposite with respect to the order of its construction.

Example 2.2.1

Example 2.2.1

// Determining the class of the base class POINT and the derived class of SPOT.

#include <graphics.h> // use graphics

#include <conio.h>

class point // POINT class definition

{

protected:

int x, y;

public:

point (int x1 = 0, int y1 = 0);

int & getx (void);

int & gety (void);

void show (void);

void move (int x1 = 0, int y1 = 0);

private:

void hide ();

};

class spot : public point // Class definition SPOT

{protected:

int r; // radius

int vis; // visibility flag

int tag; // sign of saving the visible image of the object in memory

spot * pspot; // pointer to the memory area for the visible image

public:

spot (int, int, int);

void show ();

void hide ();

void move (int, int);

void change (float d) // resize

};

// Definition of functions - members of the class POINT

point:: point (int x1, int y1) {x = x1; y = y1;}

int & point:: getx (void) {return x;}

int & point:: gety (void) {return y;}

void point:: show (void) {putpixel (x, y, getcolor ());}

void point:: hide (void) {putpixel (x, y, getbkcolor ());}

void point:: move (int x1, int y1)

{

hide ();

x = x1; y = y1;

show ();

}

// Definition of functions - members of the class SPOT

spot :: spot (int x1, int y1, int r1): point (x1, y1)

{int size;

vis = 0; tag = 0; r = r1;

size = imagesize (x1 – r, y1 – r, x1 + r, y1 + r);

pspot = (spot *) new char [size];}

spor :: ~ spot () {hide (); tag = 0; delete pspot;}

void show ()

{if (tag = = 0)

{circle (x, y, r);

floodfill (x, y, getcolor ());

getimage (x – r, y – r, x + r, y + r, pspot);

tag = 1; }

else putimage (x – r, y – r, pspot, XOR_PUT);

vis = 1;}

void spot :: hide ()

{if (vis = = 0) return;

putimage (x – r, y – r, pspot, XOR_PUT);

vis = 0;}

void spot :: move (int x1, int y1)

{hide ();

x = x1; y = y1;

show ();}

void spot :: change (float d)

{float a; int size; hide (); tag = 0;

delete pspot;

a = d * r;

if (a <= 0) r = 0;

else r = (int) a;

size = imagesize (x – r, y – r, x + r, y + r);

pspot = (spot *) new char [size];

show ();}

int & spot :: getr (void) {return r;}

};

// Two objects are created, shown, then one moves, and the other

// resizes

void main ()

{

// initialization of graphics

int dr = DETECT, mod;

initgraph (& dr, & mod, “C: \ tc \ bgi”);

{

spot A (200,50,20);

spot B (500,200,30);

A.show (); getch ();

B.show (); getch ();

A.move (50.60); getch ();

B.change (3); getch ();

}

closegraph ();

}

In this example, in the spot object, a point is created as a nameless object of class point. A feature of the main function in the example is the presence of an internal block for working with spot objects. This is due to the presence of a destructor in the spot class, during which the hide () method is called, requiring a graphical mode. If you build a program without an internal block, the destructor will be called at the end of the program when the graphics mode is closed. This problem can also be solved by explicitly calling the destructor, for example

...

B.change (3); getch ();

A.spot:: ~ spot ();

getch ();

B.spot:: ~ spot ();

closegraph ();


Comments


To leave a comment
If you have any suggestion, idea, thanks or comment, feel free to write. We really value feedback and are glad to hear your opinion.
To reply

C ++ (C plus plus)

Terms: C ++ (C plus plus)