Static Member Function In C++
A static
member function can be called even if no objects of the class exist and
the static functions are accessed using only the class name
and the scope resolution operator ::.
A static
member function can only access static data member, other static member
functions and any other functions from outside the class.
Static
member functions have a class scope and they do not have access to the this pointer
of the class.
Fig: Static Member Function In C++
Fig: Static Member Function In C++
Syntax:
class classname
{
data member;
public:
static return type
functionname(argument list member function);
};
Program:
#include<iostream.h>
#include<conio.h>
class MCA
{
static int
count;
int cd;
public:
void
getdata();
void
putdata();
static
void output()
{
Cout<<”count=”<<count;
}
};
void MCA::getdata()
{
cout<<”example
of static member function”;
cd=count++;
}
void MCA
:: putdata()
{
cout<<
cd;
}
int
MCA::count
{
void main()
{
MCA a1;
a1.putdata();
a1.output();
a1.getdata();
a1.getdata();
a1.putdata();
a1.output();
getch();
}
Output:
Count=0
Example of
static member function count=2
No comments:
Post a Comment