C++ static data members

C++ static member function

Like static member variable, we can have static member functions. A member functions that is declared as static has the following properties

1. A static function can have access to only other static members functions or static variables declared in the same class.
2. A static member function can be called using the class name (instead of its objects) as follows

class-name:: function-name;

Notice The following statement in the program

int items:: count; // definition of static data member

take the example of the static function displaycount() display the number of objects created till that moment. A count of number of objects created in maintained by the static variable count

in this example showcode() displays the code number of each object

#include <iostream>
using namespace std;
class test
{
int code;
static int count; // static variable
public :
void setcode()
{
count=++count;
}
void showcode()
{
std::cout<<"object number" <<code <<"\n";
}
static void displaycount() // static member function
{
std::cout<<"count:" <<count <<"\n";
}
};
int test::count;
int main()
{
test t1,t2;
t1.setcode();
t2.setcode();
test::displaycount(); // accessing static function
test t3;
t3.setcode();
test::displaycount();
t1.showcode();
t2.showcode();
t3.showcode();
return 0;
}

count:2
count:3
object number:1
object number:2
object number:3

Note :
code=++count;
is executed whenever setcode() function is invoked and the current value of count is assigned to code. since object has its own copy of code, the value contained in code represents a unique number of its object

Remember This below code not work

static void showcount()
{
cout << code; // because code is not static
}

in the below fig show

Static data members in c++

  • C++ Dynamic Initialization of Objects
  • C++ Copy Constructor
  • C++ Dynamic Constructor
  • C++ Destructors
  • C++ Exercise
  • C++ Operator Overloading
  • C++ Overloading Unary Operators
  • Const pointer in C
  • Void pointer in c
  • C++ Overloading Binary Operators
  • C++ Overloading Binary Operators Using Friends
  • C++ Manipulation String Using Operators
  • C++ Rules for overloading operators
  • C++ Exercise
  • C++ Basic To class Type
  • C++ Class TO Basic Type
  • C++ One class To another class type
  • C++ Exercise
  • C++ Inheritance introduction
  • C++ Single Inheritance
  • C++ Multiple Inheritance
  • C++ Ambiguity Resolution in inheritance
  • C++ Hierarchical Inheritance
  • C++ Hybrid Inheritance
  • C++ Virtual Base Classes
  • C++ Exercise
  • C++ abstract class
  • C++ nesting of classes
  • C++ Exercise
  • C++ polymorphism
  • C++ Exercise
  • C++ pointers
  • C++ Pointers TO object
  • C++ this pointer
  • C++ Pointer to Derived class
  • C++ Virtual functions
  • C++ Exercise
  • C++ streams
  • C++ unformatted I/O operations
  • C++ Put() and get()
  • C++ getline() and write()
  • C++ Formatted console I/O
  • C++ Manipulators
  • C++ Exercise
  • C++ file handling
  • C++ file stream classes
  • C++ Open and closing file
  • C++ open using constructor
  • C++ open using open()
  • C++ Detecting End of file
  • C++ File modes
  • C++ File pointers and Manipulators
  • C++ Sequential I/O
  • C++ Reading and Writing
  • C++ Updating a File
  • C++ Error handling In File
  • C++ Command Line Arguments
  • C++ Exercise
  • C++ Template introduction
  • C++ Class Templates with multiple Parameters
  • C++ Function templates
  • C++ Function templates with multiple parameters
  • C++ member function Template
  • C++ Exercise
  • C++ Exception handling
  • C++ Basics of Exception Handling
  • C++ Exception Handling Mechanism
  • C++ Throwing Mechanism
  • C++ Catch Mechanism
  • C++ Catch all Exceptions
  • C++ Re-Throwing An Exception
  • C++ Specifying Exceptions
  • C++ Exercise