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

2.8. Local and nested classes

Lecture



A class can be declared inside a block, for example, inside a function definition. This class is called local . Localization of a class implies the inaccessibility of its components outside the class definition area (outside the block).

The local class cannot have static data, since local class components cannot be defined outside the class text. Inside a local class, it is allowed to use from the scope of its scope only type names, static variables, external variables, external functions, and enumeration elements. From what is forbidden it is important to note the variables of automatic memory. There is another important limitation for local classes - their component functions can only be inline.

Inside a class it is allowed to define types, therefore, one class can be described inside another. This class is called nested . The nested class is local for the class in which it is described, and the rules for using the local class mentioned above are applied to it. It should be noted that the nested class does not have any special access rights to the members of the enclosing class, that is, it can access them only through an object of the type of this class (just as the enclosing class does not have any special access rights to the nested class ).

Example 2.8.1

int i;

class global {

static int n;

public:

int i;

static float f;

class intern {

void func (global & glob)

{i = 3; // Error: the name of the non-static data is used

// from the spanning class

f = 3.5; // Correct: f-static function

:: i = 3; // Correct: i-external (relative to the class)

// variable

glob.i = 3; // Correct: appeal to members covering

// class through an object of this class

n = 3; // Error: Accessing private member encompassing

// class

}}; };

Example 2.8.2

// Class “RECTANGLE”.

// Define the class “rectangle”. Inside this class, we define a class as // nested class “segment”. The rectangle will be constructed from segments.

#include

#include

// point

class point {

protected:

int x, y;

public:

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

int & getx () {return x;}

int & gety () {return y;}

};

// rectangle

class rect {

// nested class “segment”

class segment {

point a, b; // beginning and end of the segment

public:

segment (point a1 = point (0,0), point b1 = point (0,0))

{a.getx () = a1.getx ();

a.gety () = a1.gety ();

b.getx () = b1.getx ();

b.gety () = b1.gety ();}

point & beg () {return a;}

point & end () {return b;}

void Show () // show segment

{line (a.getx (), a.gety (), b.getx (), b.gety ());}

}; // end of class definition

segment ab, bc, cd, da; // sides of a rectangle

public:

rect (point c1 = point (0,0), int d1 = 0, int d2 = 0)

{point a, b, c, d; // vertex coordinates

a.getx () = c1.getx ();

a.gety () = c1.gety ();

b.getx () = c1.getx () + d1;

b.gety () = c1.gety ();

c.getx () = c1.getx () + d1;

c.gety () = c1.gety () + d2;

d.getx () = c1.getx ();

d.gety () = c1.gety () + d2;

// boundary points of segments

ab.beg () = a; ab.end () = b;

bc.beg () = b; bc.end () = c;

cd.beg () = c; cd.end () = d;

da.beg () = d; da.end () = a;}

void Show () // bye rectangle

{ab.Show ();

bc.Show ();

cd.Show ();

da.Show ();}

}; // end of class definition rect

void main ()

{int dr = DETECT, mod;

initgraph (& dr, & mod, "C: \\ tc3 \\ bgi");

point p1 (120.80);

point p2 (250,240);

rect A (p1, 80, 30);

rect B (p2,100,200);

A.Show (); getch ();

B.Show (); getch ();

closegraph ();

}

Using this technique, you can define any geometric fugur, consisting of straight line segments.

Example 2.8.3

Class “LINE”

The string class stores a string as an array of characters with a terminating zero with the C style and uses a reference counting mechanism to minimize copying operations.

The string class uses three helper classes:

srep , which allows you to share a valid representation between multiple objects of type string with the same values;

range- to generate an exception in the case of out of range;

cref - to implement the index operation, which distinguishes read and write operations .

class string {

struct srep;

srep * rep;

public:

class cref; // link to char

class range {};

// ...

};

Like other members, a nested class can be declared in the class itself, and defined later.

struct string :: srep {

char * s; // pointer to elements

int sz; //Characters

int n; // number of hits

srep (const char * p)

{n = 1;

sz = strlen (p);

s = new char [sz + 1];

strcpy (s, p);

}

~ srep () {delete [] s;}

srep * get_copy () // make a copy if necessary

{if (n == 1) return this;

n--;

return new srep (s);}

void assign (const char * p)

{if (strlen (p)! = sz) {delete [] s;

sz = strlen (p);

s = new char [sz + 1];}

strcpy (s, p);}

private: // prevents copying

sper (const srep &);

srep & operator = (const srep &);

}

The string class provides the usual set of constructors, destructors, and assignment operators.

created: 2015-12-20
updated: 2021-03-22
132419



Rating 9 of 10. count vote: 2
Are you satisfied?:



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)