Static Members of a Class - C++ Class

C++ examples for Class:static member

Introduction

Static data members have class-wide storage of data which is independent of any particular object.

A static function member is independent of any individual class object.

The following class use static member field to track object count.

Demo Code

                                                                                                                                                           
#include <iostream>
class Pool {// w ww .j a v a2  s . co  m
private:
  double length {1.0};
  double width {1.0};
  double height {1.0};
  static int objectCount;                // Count of objects in existence
                                                                                                                                                           
public:
  // Constructors
  Pool(double lv, double wv, double hv);
  Pool(double side) : Pool {side, side, side}                           // Constructor for a cube
  {
    std::cout << "Pool constructor 2 called." << std::endl;
  }
                                                                                                                                                           
  Pool()                                                               // No-arg constructor
  {
    ++objectCount;
    std::cout << "No-arg Pool constructor called." << std::endl;
  }
                                                                                                                                                           
  Pool(const Pool& pool) :                                               // Copy constructor
        length {pool.length}, width {pool.width}, height {pool.height}
  {
    ++objectCount;
    std::cout << "Pool copy constructor called." << std::endl;
  }
                                                                                                                                                           
  double volume() const;                            // Function to calculate the volume of a pool
  int getObjectCount() const { return objectCount; }
};
int Pool::objectCount {};                 // Initialize static member of Pool class to 0
                                                                                                                                                           
// Constructor definition
Pool::Pool(double lv, double wv, double hv) : length {lv}, width {wv}, height {hv}
{
  ++objectCount;
  std::cout << "Pool constructor 1 called." << std::endl;
}
                                                                                                                                                           
// Function to calculate the volume of a pool
double Pool::volume() const
{
  return length*width*height;
}
                                                                                                                                                           
int main() {
  const Pool pool1 {2.0, 3.0, 4.0};                                // An arbitrary pool
  Pool pool2 {5.0};                                                // A pool that is a cube
  std::cout << "pool1 volume = " << pool1.volume() << std::endl;
  std::cout << "pool2 volume = " << pool2.volume() << std::endl;
  Pool pool3 {pool2};
  std::cout << "pool3 volume = " << pool3.volume() << std::endl;   // Volume = 125
  Pool pools[6] {pool1, pool2, pool3, Pool {2.0}};
  std::cout << "There are now " << pool1.getObjectCount() << " objects." << std::endl;
}

Result


Related Tutorials