C++ static and const

If a data item in a class is declared as static, only one such item is created for the entire class, no matter how many objects there are.

Static variable is visible only within the class, but its lifetime is the entire program. It continues to exist even if there are no objects of the class.

A static data item is useful when all objects of the same class must share a common item of information.

The following program helps to have clear understanding about static member.

//example of static data

#include<iostream>
using namespace std;
class Counter
{
private:
    static int count;   //only one data item for all objects
                        //note: “declaration” only!
public:
    Counter()           //increments count when object created
    {
        count++;
    }
    int get_count()     //returns count
    {
        return count;
    }
};

int Counter::count = 0; //*definition* of count

int main()
{
    Counter c1, c2, c3; //create three objects
    cout << "count is " << c1.get_count() << endl; //each object
    cout << "count is " << c2.get_count() << endl; //sees the
    cout << "count is " << c3.get_count() << endl; //same value
    return 0;
}

The output of the program is:

count is 3
count is 3
count is 3

Here, whenever an object is created, static member count is incremented and since each object share the same value, count on each print is 3.

In case of ordinary data member, count would have been 1 in each case.

 

const Member function

A const member function guarantees that it will never modify any of its class’s member data.

A function is made into a constant function by placing the keyword const after the declarator but before the function body.

Syntax:

return_type function_name(args) const 
{
      //body 
}

Example:

int get_count() const    //const member function; returns count
{
   return count;
}

const Objects

In similar way as in const to variables of basic types, which keep them from being modified, we can apply const to objects
of classes. When an object is declared as const, you can’t modify it.

It follows that you can use only const member functions with it, because they’re the only ones that guarantee not to modify it.

Syntax to declare constant object:

const class_name object_name;

Example:

//example of const member function and const object

#include<iostream>
using namespace std;
class Counter
{
private:
    static int count;   //only one data item for all objects
                        //note: “declaration” only!
public:
    Counter()           //increments count when object created
    {
        count++;
    }
    void set_count(int n){
        count = n;
    }
    int get_count() const    //const member function; returns count
    {
        return count;
    }
};

int Counter::count = 0; //*definition* of count

int main()
{
    const Counter c1, c2, c3; //create three const objects
    //c1.set_count(5); //ERROR: set_count() is not const
    cout << "count is " << c1.get_count() << endl; //each object
    cout << "count is " << c2.get_count() << endl; //sees the
    cout << "count is " << c3.get_count() << endl; //same value
    return 0;
}

The output of the program is:

count is 3
count is 3
count is 3
C++ Constructors and Destructors
C++ Arrays