cpp static member function

STATIC MEMBER FUNCTIONS

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;

1. it is initialized to zero when first object of its class is created. No other initialization is permitted.
2. Only single copy of that member is created for the entire class and is shared by all the objects of that class, no matter how many objects are created.
3. it’s visible only within the class, but its lifetime is the entire program

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()
{
cout << “object number” << code << “\n”;
}
static void displaycount() // static member function
{
cout << “count:” << count << “\n”;
}
};
int test::count;
int main()
{
test t1,t2;
t1.setcode();
t2.setcode();
test::showcount(); // accessing static function
test t3;
t3.setcode();
test::showcount();
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
}

  • Basic OOPS Concepts
  • Benefits of OOPs
  • C++ History
  • C++ Applications
  • C++ features
  • C++ program structure
  • C++ Installation
  • C++ Comments
  • C++ output operator
  • C++ input operator
  • C++ Header files
  • C++ variables
  • C++ Compiling and linking
  • C++ Exercise
  • C++ Tokens
  • C++ Identifiers & constants
  • C++ Data types
  • C++ User defined data type
  • C++ derived Data types
  • C++ Symbolic constant
  • C++ Type compatibility
  • C++ Declaration of variable
  • C++ Reference Variables
  • C++ Exercise
  • C++ operators
  • C++ Scope Resolution operators
  • C++ Member Dereferencing Operator
  • C++ Memory Management operator
  • C++ Manipulators
  • C++ Type Cast Operator
  • C++ Exercise
  • C++ Expressions
  • C++ Special Assignment
  • C++ Implicit Conversions
  • C++ Exercise
  • C++ Operator Overloading
  • C++ Operator Precedence
  • C++ Exercise
  • C++ if statements
  • C++ switch statement
  • C++ Do-while statement
  • C++ while statement
  • C++ for statement
  • C++ Exercise
  • C++ function introduction
  • C++ function prototyping
  • C++ Call by reference
  • C++ Inline function
  • C++ Default Arguments
  • C++ function overloading
  • C++ Math Library Functions
  • C++ Exercise
  • C++ class Introduction
  • C++ structure Revisited
  • C++ Specifying a Class
  • C++ Creating Objects
  • C++ Accessing Class Members
  • C++ Defining Member Functions
  • C++ Nested OF Member Functions
  • C++ Private Member Functions
  • C++ Array with in a class
  • C++ Memory Allocation for objects
  • C++ Static Data Members
  • C++ Array of objects
  • C++ Friendly functions
  • C++ Returning Objects
  • C++ Member Functions
  • C++ Pointer to Members
  • C++ Local Classes
  • C++ Exercise
  • C++ Constructor Introduction
  • C++ Parameterized Constructor
  • C++ Multiple Constructor
  • 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