C++ 28: Static Data Member and Functions

Static Data Members:
  • Static Data Member
  • Static Functions

1. The data members whose value is common(i.e. same) for all
the objects of that class such a data member can be declared as static.

2. If a data member is declared as static then data member space is not created
inside the object.

3.For static data member space is created outside the object only one time all the
objects of that class can use that data member.

4.Static data members will not destroy until the program terminates.

5 For static data member the initial value can be assigned after the declaration 
of class as
follows:

* Syntax:
*          data_type class_name::member_name = value;

 Ex.

class Sample{

   private:

        int a;

        static float b;

        int c;

    public:

        void showdata(){

                 ......

                 ......

        }

        static void display{

                .....

        }

};

Note:

  • If no initialization is specified then data member static data members are initialized to zero.
  • If no initialization is specified then char static data members are initializes to blank.
  •  It is not possible to define static array.
  • The static data members can be used inside non-static member function, constructor and destructor. 


Example:
For static data member space is not created As static member is common for all the objects. So, static members helps to save space, in case a member is common to all the object then we can declare it static. for here Z is static so space for Z is not created so object of class sample only reserves space for float x and y. i.e 8 byte --> 
#include <iostream>
using namespace std;
class Sample
{
private:
    float XY;
    static float Z; ///Creating Static data member

public:
    void display()
    {
        cout << "Value of z: " << Z << endl;
    }
};

int main()
{

    Sample ab;
    cout << "Size of Obj a is : " << sizeof(a<< " bytes" << endl;
    cout << "Size of Obj b is : " << sizeof(b<< " bytes" << endl;

    return 0;
}

Output:

Size of Obj a is : 8 bytes
Size of Obj b is : 8 bytes

Static Member Functions:
1.The static member function can be called with the class name. That 
     means to call the member function we do not need  declare  object 
     of that class.
 2. In static member function we can use only other static data membrs 
     &static member function of same class.
 3. But, In static member function we can use static as well as non static
     data members of the same class.
 4. It is not possible to override static member function in Derived class.


Syntax:
         datatype classname::function


Example:
#include <iostream>
using namespace std;

class Sample
{
private:
    float ab;     //non static datamember
    static float c; //static data member

public:
    void display()
    {
        cout << "This is Simple non-static memeber Functions" << endl;
        cout << "Value of a:" << a << " Value of b: " << b << endl;
        cout << "Value of c = " << c << endl;
    }
    static void showdata()
    {
        cout << "This is static member function" << endl;
        // cout << "Value of a:" << a << " Value of b: " << b << endl;
        cout << "Value of c = " << c << endl;
    }
};
float Sample::c = 0;
main()
{
    Sample a;
    a.display();
    a.showdata();
    // Demo::dispay(); //this will  threw error --->cannot call member function 'void Demo::showdata()' without object
    cout << "Calling static member function using class" << endl;
    Sample::showdata();
}

Output:

his is Simple non-static memeber Functions
Value of a:2.24208e-44 Value of b: 0
Value of c = 0
This is static member function
Value of c = 0
Calling static member function using class
This is static member function
Value of c = 0


Example 2:
Design a class Circle having data_member function area() & circumference().
Also define a static data member PI = 3.14 and a static member function showpi()
in the class The objects of class must be created by passing the data.

#include <iostream>
using namespace std;

class Circle
{
private:
    float R;
    static float pi;

public:
    static void showpi()
    {
        cout << "The value of PI is " << pi << endl;
    }
    Circle(float r)
    {
        R = r;
    }
    void area()
    {
        float area = pi * R * R;
        cout << "Area of circle is " << area << endl;
    }
    void circumference()
    {
        float circumference = pi * R * 2;
        cout << "Circumference of circle is " << circumference << endl;
    }
};

float Circle::pi = 3.14;

main()
{
    Circle c1(2);
    c1.area();
    c1.circumference();

    Circle::showpi(); //Static Member FUnction
}

Output:

Area of circle is 12.56
Circumference of circle is 12.56
The value of PI is 3.14

Example 3:
Design a class Employee with data members employee number, employee 
name , job and Slalry Define members functions showdata() whose object
can be initilize by passing and employee number must be autoGenerated
Also define a class with members and functions in the class count_emp()
which will print total employees.

#include <iostream>
#include <string.h>
using namespace std;

class Employee
{
private:
    int ENO;
    char EN[20], J[20];
    int S;
    static int count;

public:
    Employee(char n[], char j[], int s)
    {
        strcpy(EN, n);
        strcpy(J, j);
        S = s;

        count++;
        ENO = count;
    }
    void showData()
    {
        cout << "Employee no: " << ENO << endl;
        cout << "Employee name " << EN << endl;
        cout << "Job: " << J << endl;
        cout << "Salary: " << S << endl;
    }
    static void total_emp()
    {
        cout << "The Total number of employees: " << count << endl;
    }
};
int Employee::count = 0;
main()
{
    Employee::total_emp();
    Employee e1("Kumar""AssistantM"45000);
    e1.showData();
    Employee e2("Vishvas""Clerk"35000);
    e2.showData();
    Employee::total_emp();
}

Output:

The Total number of employees: 0
Employee no: 1
Employee name Kumar
Job: AssistantM
Salary: 45000
Employee no: 2
Employee name Vishvas
Job: Clerk
Salary: 35000
The Total number of employees: 2








Comments

Popular posts from this blog

C++ 38: Visibility Modes Public, Private and Protected