Static Data Members in C++ Classes

 Static Data Members in C++ Classes


Static Data Member : It is a data member of a class that is qualified as static. The properties of a static data member is similar to that of a C static member.

Characteristics of a Static Data Member or Static Member Variable :
  • It is initialized to zero when the first object of its class is created. No other initialization is permitted.
  • Only one copy of the that member is created for the entire class and is shared by all the objects of that class, irrespective of the number of objects created.
  • It is visible only within the class, but its lifetime is the entire program. 

Static variables are normally used to maintain values common to the entire class. 

For example, cur_id is used as a static data member, that increment with the number of object of that class with "putdata()" function.

#include <cmath>

#include <cstdio>

#include <vector>

#include <iostream>

#include <algorithm>

using namespace std;


class Person

{

    protected:

    string name;

    int age;

    public:

    Person()

    {

        name = "NA";

        age = 0;

    }


    virtual void getdata(){}

    virtual void putdata(){}

};

class Professor: public Person

{

    private:

    int publications;

    static int cur_id;

    

    public:

    Professor()

    {

        name = "NA";

        age = 0;

        publications = 0;

    }

    

    void getdata()

    {

        cin >> name >> age >> publications;

    }

    void putdata()

    {

        cur_id++;

        cout << name << " "<< age << " "<< publications << " "<< cur_id <<         endl;

    }

};

class Student : public Person

{

    private:

    int marks[6];

    static int cur_id;

    int sum;

    public:

    Student()

    {

        for(int i=0; i<6; i++)

            marks[i] = 0;

        age = 0;

        sum = 0;

    }

    void getdata()

    {

        cin >> name >> age;

        

        for(int i=0; i<6; i++)

            cin >> marks[i];

    }

    void putdata()

    {

        cur_id++;

        for(int i=0; i<6; i++)

            sum += marks[i];

        

        cout << name << " " << age << " "<< sum << " " << cur_id << endl;

    }

};


int Professor :: cur_id;

int Student :: cur_id;


int main(){

int n, val;

cin >> n; //The number of objects that is going to be created.

Person *per[n];

for(int i = 0;i < n;i++)

{

    cin >> val;

    if(val == 1)

    {

        // If val is 1 current object is of type Professor

        per[i] = new Professor;

    }

   else per[i] = new Student; // Else the current object is of type Student

    per[i]->getdata(); // Get the data from the user.

}

for(int i=0;i<n;i++)

    per[i]->putdata(); // Print the required output for each object.

return 0;

}



Note : The type and scope of each static member variable must be defined outside the class definition. For Example, in the above given program:

int Professor :: cur_id; // definition of static data member outside the class

int Student :: cur_id; // definition of static data member outside the class

This is necessary because the static data members are stored separately rather than part of an object. Since they are associated with the class itself rather than with any class object, they are also known as class variables.


The static variable cur_id is initialized to zero only once when the first object of the class is created. Then the same copy of the static variable is also shared with other objects of the class created after first object. When every time putdata() function of Student class or Professor class is called , value of the static variable of the respective class is incremented by 1.


Static variables are like non-inline member functions as they are declared in a class declaration and defined in the source file. While defining a static variable, some initial value can also be assigned to the variable.

e.g., int Student :: cur_id = 5;




Comments

Popular posts from this blog

Setting up a USB thermal printer with a Raspberry Pi 3B+

Basic Input/Output C Program

Autostart an app on reboot - Raspberry Pi