Object Oriented Programming: Chapter 2: Classes and Objects

Static Members: Static Data and Static Function

C++ Programming | Object Oriented Programming

Question: Write about static member variable and static member functions.

Static Members: Static Data and Static Function

•  A class contains two types of static membersMember variables and functions.


•  The members of the class must be declared with the keyword static. The static data members have the same properties as that of global variables. When we declare regular variables then multiple copies are created for accessing them by each corresponding object but when the keyword static is associated with any class variable then that means only single copy of that variable must be created and all other objects must share that copy. The static data members can be accessed without can be accessed without any instantiation of class as an object. The static member functions are not used very frequently in programs. All static variables are initialized to zero before first object is created.

•  The syntax of static data declaration and definition is as given below –

 class test {

    private:

            static int count; //declaration

            

};

int test::count=100; //definition without instantiation of class as object

The static function can not refer to any non static member data in its class. The static function can access only static and class‒specific data. The syntax of static function is as follows ‒

class test()

{

    private:

   

    public:

        static int fun();//static function definition

{

:

}

};

void main()

{

test::fun(); //function call

...

}

The program having static class members is as given below ‒

C++ Program

#include<iostream>

using namespace std;

class count

{

private:

    int number;

    static int total;

        // declaration:static data

public:

    count();      // initialize

     int get_number();// get a number

    static int get_total(); // get total count

};

count::count() //initialize one count

{

    number = 100+total++;

}

int count::get_number()     // get number

{

    return number;

}

int count::get_total()    // get total counts

{

    return total;

}

int count::total = 0;   // definition of static data

void main()

{

    clrscr();

    //will print initial count

    cout << "Total count = " << count::get_total() << endl;

    count a, b, c;

    //will increment the count 3 times

    cout << a=" << a.get_number() << endl;

    cout << " b=" << b.get_number() << endl;

    cout << c="<< c.get_number() << endl;

    //will print the final incremented count

    cout << "Total count = " << count::get_total() << endl;

     getch();

}

Output

Total count = 0

a=100

b=101

c=102

Total count = 3

 

Review Question

1. Write about static member variable and static member functions.

 

Object Oriented Programming: Chapter 2: Classes and Objects : Tag: Oops, Computer Programming : C++ Programming | Object Oriented Programming - Static Members: Static Data and Static Function


Object Oriented Programming: Chapter 2: Classes and Objects



Under Subject


Object Oriented Programming (OOPs)

CS25C07 2nd Semester CSE, CSE(CY) Depts | 2025 Regulation | 2nd Semester 2025 Regulation



Related Subjects


English Essentials II

EN25C02 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation



Linear Algebra

MA25C02 2nd Semester | 2025 Regulation


Applied Physics (CSIE) II

PH25C03 2nd Semester AIDS, CSE, IT, CSE(CY) Dept | 2025 Regulation | 2nd Semester 2025 Regulation


Digital Principles and Computer Organization

CS25C06 2nd Semester AIDS, CSE, IT, CSE(CY) Dept | 2025 Regulation | 2nd Semester 2025 Regulation


Basic Electrical and Electronics Engineering

EE25C01 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation


Object Oriented Programming (OOPs)

CS25C07 2nd Semester CSE, CSE(CY) Depts | 2025 Regulation | 2nd Semester 2025 Regulation


Re-Engineering for Innovation

ME25C05 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation


Object Oriented Programming (OOPs) - Laboratory

CS25C07 2nd Semester CSE, CSE(CY) Depts | 2025 Regulation | 2nd Semester 2025 Regulation