You are on page 1of 6

Static member function

A static function can have access to only other static members declared in the class. A static member function can be called using the class name
class name :: function name

static void showcount (void) {

cout << count: << count << \n;


} static functions are very similar to non-member functions. The only difference is that it is now bound and known to our class only.

Remember that non-member functions are known to all the classes of the program.

Static member function #include<iostream.h> #include<conio.h> class test { int code; static int count; public: void setcode(void) { code=++count; } void showcode(void) { cout<< "Object number: "<<code<<"\n"; } static void showcount(void) { cout<<"count:"<<count<<"\n"; } };

int test::count; int main() { test t1,t2; clrscr(); t1.setcode(); t2.setcode(); test::showcount(); test t3; t3.setcode(); test::showcount(); t1.setcode(); t2.setcode(); t3.setcode(); getch(); return 0; }

Const member functions

If a member function does not alter any data in the class, then we declare it as a const member function

void display() const { cin>>number; cout<<number is<<number; }

Compiler will generate an error msg if such functions try to alter the data.
When a data member is declared mutable, it can be modified even by the const functions.
mutable int number;

Mutable data members:

Volatile functions
A member function can also be declared as volatile if it is invoked by a volatile object. A volatile objects values can be changed by external parameters which are not under the control of the program. ex, taking the input from a external devices like Network Interface Card (NIC).

class NICClass { public: void CheckValues() volatile // volatile function definition { // function body } }; volatile NICClass NICObject; // volatile object definition

You might also like