CPP Language / Static Data Members and Static Member Functions

Static data members are accessed using Class name or object name.
Static members Functions are accessed using Class name or object name.

Members Used
Static data members objectname.membername=value;
Static Member Functions Classname:: Memberfunctionname(Parameters);
objectname. Memberfunctionname(Parameters);

Program Output

#include
using namespace std;
class a
{
private:
static int X;
static int Y;
public:
static void Print()
{
cout <<"Value of X: " << X << endl;
cout <<"Value of Y: " << Y << endl;
}
};

int a :: X =10;
int a :: Y =20;
int main()
{
a aobj;
cout<<"Printing through object name:"< aobj.Print();

cout<<"Printing through class name:"< a::Print();
return 0;
}


Printing through object name:
Value of X: 10
Value of Y: 20
Printing through class name:
Value of X: 10
Value of Y: 20



Home     Back