Home C++ Introduction Decisions Loops Input/Output Functions Stack and Heap References Arrays Searching and Sorting Recursion Pointers Character and Strings Structures Classes Inheritance Exceptions Templatess STL Modern C++ Misc Books ----

Classes


Contents

Instance and Static Members

We can have static members in a class. A static member is like a global variable and only one instance is ever created regardless of how many class objects are created.

File: static1.cpp
#include <iostream>
using namespace std ;

class Demo
{
    public:
    static int z1 ;
    int  x1 ;
    int  x2 ;
};

int Demo::z1 = 0 ;

int main()
{
    Demo obj1 ;
    obj1.x1 = 2 ;
    obj1.x2 = 3 ;
    obj1.z1 = 1 ;
    Demo obj2 ;
    obj2.x1 = 4 ;
    obj2.x2 = 5 ;
    obj1.z1 = 2 ;

    cout << obj1.x1 << " " << obj1.x2 << " " << obj1.z1 <<  endl ;
    cout << obj2.x1 << " " << obj2.x2 << " " << obj2.z1 << endl ;
   return 0 ;

}
$ ./a.exe
2 3 2
4 5 2
We use the "static" qualifier to indicate that the variable is static. For static variables we also need to do another declaration outside the class such as:

int Demo::z1 = 0 ;

We then create 2 objects of the class Demo and assign values. Observe that the static variable "z1" only has 1 value printed out. It does not belong to an object. The object variables have different values printed out.

We can also have static method members in a class.


File: static2.cpp
#include <iostream>
using namespace std ;

class Demo
{
    public:
     static int z1 ;
     static void print()
     {
         cout << "z1:" << z1 << endl ;
     }
     int  x1 ;
     int x2 ;
};

int Demo::z1 = 0 ;

int main()
{
    Demo obj1 ;
    obj1.x1 = 2 ;
    obj1.x2 = 3 ;
    obj1.z1 = 1 ;
    Demo obj2 ;
    obj2.x1 = 4 ;
    obj2.x2 = 5 ;
    obj1.z1 = 2 ;
    cout << obj1.x1 << " " << obj1.x2 << " " << obj1.z1 <<  endl ;
    cout << obj2.x1 << " " << obj2.x2 << " " << obj2.z1 << endl ;
    obj1.print() ;
    obj2.print() ;
    Demo::z1 = 100 ;
    Demo::print() ;

    return 0 ;
}
$ g++ static2.cpp  ; ./a.exe
2 3 2
4 5 2
z1:2
z1:2
z1:100
We can refer to the static variables using the class "Demo" instead of the object variables. We can use the syntax "Demo::z1" and "Demo::print()" ti refer to the variable and the method name. Since these are global variables we can use the object variable or the class name. However using class names makes it easier to read the code. Note that we cannot place the class non-static variables inside a static function. Since we don't need to have an object in order to call this method.
File: static3.cpp
#include <iostream>
using namespace std ;

class Demo
{
    public:
    static int z1 ;
    static void print()
    {
        cout << "z1:" << z1 << " " << x1 << endl ;
    }
    int  x1 ;
    int x2 ;
};

int Demo::z1 = 0 ;

int main()
{
    Demo obj1 ;
    obj1.x1 = 2 ;
    obj1.x2 = 3 ;
    obj1.z1 = 1 ;
    Demo obj2 ;
    obj2.x1 = 4 ;
    obj2.x2 = 5 ;
    obj1.z1 = 2 ;
    cout << obj1.x1 << " " << obj1.x2 << " " << obj1.z1 <<  endl ;
    cout << obj2.x1 << " " << obj2.x2 << " " << obj2.z1 << endl ;
    obj1.print() ;
    obj2.print() ;
    Demo::z1 = 100 ;
    Demo::print() ;
    return 0 ;

}
$ g++ static3.cpp  ; ./a.exe
static3.cpp: In static member function ‘static void Demo::print()’:
static3.cpp:10:47: error: invalid use of member ‘Demo::x1’ in
static member function
   10 |                 cout << "z1:" << z1 << " " << x1 << endl ;
Exercise

Complete the "TO DO" section.


File: static_ex1.cpp
#include <iostream>
using namespace std ;

class car
{
    public:
    static int numberCarsCreated ;
    static void print()
    {
        cout << "numberCarsCreated:" << numberCarsCreated << endl ;
    }
    car()
    {
        //TO DO INCREMENT THE STATIC VARIABLE
    }
};
//TO DO DEFINE AND INITIALIZE THE STATIC VARIABLE

int main()
{
    car car1 ;
    car car2 ;
    car car3 ;
    car car4 ;
    car::print() ;
    return 0 ;

}

Friend

Const functions

OO

Smart Pointer