Latest web development tutorials

Static members of C ++ classes

C ++ Class & Objects C ++ Class & Objects

We can use thestatic keyword to the class members is defined as static.When we declare a member of the class is static, which means that no matter how many classes of objects created, static members have only one copy.

Static members in all classes of objects are shared. If other initialization statement does not exist when the first object is created, all static data will be initialized to zero. We can not take place in the definition of a static member of the class, but the class can be resolved externally by using the range operator:: to re-declare a static variable so it is initialized, as shown in the following example.

The following examples help to better understand the concept of static data members:

#include <iostream>
 
using namespace std;

class Box
{
   public:
      static int objectCount;
      // 构造函数定义
      Box(double l=2.0, double b=2.0, double h=2.0)
      {
         cout <<"Constructor called." << endl;
         length = l;
         breadth = b;
         height = h;
         // 每次创建对象时增加 1
         objectCount++;
      }
      double Volume()
      {
         return length * breadth * height;
      }
   private:
      double length;     // 长度
      double breadth;    // 宽度
      double height;     // 高度
};

// 初始化类 Box 的静态成员
int Box::objectCount = 0;

int main(void)
{
   Box Box1(3.3, 1.2, 1.5);    // 声明 box1
   Box Box2(8.5, 6.0, 2.0);    // 声明 box2

   // 输出对象的总数
   cout << "Total objects: " << Box::objectCount << endl;

   return 0;
}

When the above code is compiled and executed, it produces the following results:

Constructor called.
Constructor called.
Total objects: 2

Static member function

If the function is declared as static member, you can put any particular class of object functions and to open an independent. Even static member functions can also be invoked in the case of the class object does not exist, as long as thestatic function using the class name plus the scope resolution operator ::to access.

Static member functions can only access static data members, can not access other functions and other static member functions outside the class.

Static member function has a range of classes, they can not access this type of pointer. You can use a static member function to determine whether a certain object class has been created.

The following examples help to better understand the concept of a static member function:

#include <iostream>
 
using namespace std;

class Box
{
   public:
      static int objectCount;
      // 构造函数定义
      Box(double l=2.0, double b=2.0, double h=2.0)
      {
         cout <<"Constructor called." << endl;
         length = l;
         breadth = b;
         height = h;
         // 每次创建对象时增加 1
         objectCount++;
      }
      double Volume()
      {
         return length * breadth * height;
      }
      static int getCount()
      {
         return objectCount;
      }
   private:
      double length;     // 长度
      double breadth;    // 宽度
      double height;     // 高度
};

// 初始化类 Box 的静态成员
int Box::objectCount = 0;

int main(void)
{
  
   // 在创建对象之前输出对象的总数
   cout << "Inital Stage Count: " << Box::getCount() << endl;

   Box Box1(3.3, 1.2, 1.5);    // 声明 box1
   Box Box2(8.5, 6.0, 2.0);    // 声明 box2

   // 在创建对象之后输出对象的总数
   cout << "Final Stage Count: " << Box::getCount() << endl;

   return 0;
}

When the above code is compiled and executed, it produces the following results:

Inital Stage Count: 0
Constructor called.
Constructor called.
Final Stage Count: 2

C ++ Class & Objects C ++ Class & Objects