Data Structures using C PlusPlus: Chapter 1: Data Abstraction and Overloading

Static Class Members

Data Structures using C ++ Program

Data Structures using C PlusPlus: Data Abstraction and Overloading: Static Class Members

Static Class Members

A class contains two types of static members ‒


Static members

* Member variable

* Member function

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 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 given below ‒

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();            // initializer

   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

 

Data Structures using C PlusPlus: Chapter 1: Data Abstraction and Overloading : Tag: Data Structure, C++, C Programing : Data Structures using C ++ Program - Static Class Members


Data Structures using C PlusPlus: Chapter 1: Data Abstraction and Overloading



Under Subject


Data Structures using CPlusPlus

CS25C05 2nd Semester ECE Dept | 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


Electron Devices

EC25C01 2nd Semester ECE Dept | 2025 Regulation | 2nd Semester 2025 Regulation


Data Structures using CPlusPlus

CS25C05 2nd Semester ECE Dept | 2025 Regulation | 2nd Semester 2025 Regulation


Circuits and Network Analysis

EC25C02 2nd Semester ECE Dept | 2025 Regulation | 2nd Semester 2025 Regulation


Re-Engineering for Innovation

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


Engineering Drawing - Laboratory

ME25C01 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation


Data Structures using CPlusPlus - Laboratory

CS25C05 2nd Semester ECE Dept | 2025 Regulation | 2nd Semester 2025 Regulation


Devices and Circuits Laboratory

EC25C03 2nd Semester ECE Dept | 2025 Regulation | 2nd Semester 2025 Regulation