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

1.4.4. Static class members

Lecture



Each object of the same class has its own copy of the class data. This does not always meet the requirements of the problem to be solved. For example, an object counter, pointers to the first and last object in the list of objects of the same class, or mark-up in the goods class in Example 1.1.3. This data should be class components, but you only need to have them in the singular. Such components must be defined in the class as static . Static class data is not duplicated when creating objects, i.e. each static component exists in a single copy. Access to a static component is possible only after its initialization. For initialization the construction is used.

type class_name:: data_name initializer;

For example,

int goods:: percent = 12;

This clause must be placed in the global scope after class definition. Only at initialization the static given class gets the memory and becomes available. You can access the static given class in the usual way through the object name.

object_name.com_name

But static components can be accessed when the class object does not yet exist. Access to static components is possible not only through the name of the object, but also through the name of the class

class_name:: component_name

However, this can only be addressed to public components. And how to access a private static component from outside the definition of an object? Using the component function of this class. However, when calling such a function, you must specify the name of the object, and the object may not exist yet. This problem is solved by static component functions . These functions can be called through the class name.

class_name:: static_function_name

Example 1.4.1

#include <iostream.h>

class TPoint

{

double x, y;

static int N; // static component - given: number of points

public:

// constructor

TPoint (double x1 = 0.0, double y1 = 0.0) {N ++; x = x1; y = y1;}

static int & count () {return N;} // static component function

};

int TPoint:: N = 0; // initialization of the static component given

void main (void)

{TPoint A (1.0,2.0);

TPoint B (4.0,5.0);

TPoint C (7.0,8.0);

cout << “\ nDefined” << TPoint:: count () << “points.”;

}

Example 1.4.2

// Simulate the list.

class list

{int x; //information field

list * next; // pointer to the next element

static list * begin; // top of list

public:

list (int x1);

~ list ();

add (); // object adds itself to the list

static show (); // static function to view the list

};

list * list:: begin = NULL;

void main ()

{list * p;

p = new list (5); p-> add (); // create the first object and add it to the list

p = new list (8); p-> add (); // create the second object and add it to the list

p = new list (35); p-> add (); // create a third object and add it to the list

list :: show (); // show the whole list

}

Example 1.4.3

// Another implementation of the class goods- see example 1.1.3.

#include <iostream.h>

// Class “goods”

class goods

{char name [40];

float price;

static int percent; // markup

public:

void Input () {cout << “name:”;

cin >> name;

cout << “price:”;

cin >> price;}

void print () {cout << “\ n” << name;

cout << “, price:”;

cout << long (price * (1.0 + goods:: percent * 0.01));}

};

int goods:: percent = 12;

void main (void)

{

goods wares [5];

int k = 5;

for (int i = 0; i <k; i ++) wares [i] .Input ();

cout << “\ nList of goods with markup” << wares [0] .percent << “%”;

for (i = 0; i <k; i ++) wares [i] .print ();

goods:: percent = 10;

cout << “\ nList of goods at extra charge” << goods:: percent << "%";

goods * pGoods = wares;

for (i = 0; i <k; i ++) pGoods ++ -> print ();

}


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)