English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

How to use enumerations in C? ++Double-ended queue in Python

Static keywords can be used to define C ++static members of the class. There is only one copy of the static class member in memory, regardless of the number of objects of the class. Therefore, static members are shared by all class objects.

If the first object of the class is not initialized in any other way, the static class member will be initialized to zero when the first object of the class is created.

The program demonstrating the definition of static class members is as follows:

Example

#include <iostream>
using namespace std;
class Point{
   int x;
   int y;
   public:
   static int count;
   Point(int x1, int y1{
      x = x1;
      y = y1;
      count++;
   }
   void display()
      cout << "The point is (" << x << "," << y << ")\n";
   }
};
int Point::count = 0;
int main(void){
   Point p1(10,5);
   Point p2(7,9);
   Point p3(1,2);
   p1.display();
   p2.display();
   p3.display();
   cout << "\nThe number of objects are: " << Point::count;
   return 0;
}

The output of the above program is as follows-

The point is (10,5)
The point is (7,9)
The point is (1,2)
The number of objects are: 3

Now let's understand the above program.

The Point class has2two data members x and y that make up a point. There is also a static member count, used to monitor the number of objects created by the Point class. The constructorPoint()Initialize the values of x and y, and then the functiondisplay()Display their values. The code snippet to display is as follows-

class Point{
   int x;
   int y;
   public:
   static int count;
   Point(int x1, int y1{
      x = x1;
      y = y1;
      count++;
   }
   void display()
      cout << "The point is (" << x << "," << y << ")\n";
   }
};

In this functionmain(), created3number of Point class objects. Then, by calling the function, display the values of these objectsdisplay(). Then display the count value. The code snippet to display is as follows-

Point p1(10,5);
Point p2(7,9);
Point p3(1,2);
p1.display();
p2.display();
p3.display();
cout << "\nThe number of objects are: " << Point::count;