What is static member function? 

A member function that is declared as static in a program is known as static functions.
The static function has the following properties:
• A static function in a program can access to only other static member declared in the same program or in the same class. 
• A static member function of a class can be called using the class_name as follows: 
class_name :: function_name;


C++ program which explains the working of static member function in a class

Program Code:
#include <iostream>
using namespace std;
class First
{
static int a;
static int b;
public:
static void display()
{
cout <<"Addition of a and b is : " << a+b << endl;
}
};
//static data members initializations
int First :: a =10;
int First :: b =20;
int main()
{
First c;
//accessing class name with object name
cout<<"Printing through object name:"<<endl;
c.display();
//accessing class name with class name
cout<<"Printing through class name:"<<endl;
First::display();
return 0;
}


The program output is tested on www.jdoodle.com

Output:
Printing through object name:
Value of a : 10
Value of b : 20
Printing through class name:
Value of a : 10
Value of b : 20


Thanks
Mukesh Rajput
Mukesh Rajput

Mukesh Rajput

I am a Computer Engineer, a small amount of the programming tips as it’s my hobby, I love to travel and meet people so little about travel, a fashion lover and love to eat food, I am investing a good time to keep the body fit so little about fitness also..

Post A Comment:

0 comments: