Saturday, 31 March 2018

Static Member Function

Member function may also be declared static by prefixing the static keyword in the function definition header.

A static member function can have access to only other static members declared in the same class.

A static member function can be called by using its class name.
  class_name::function_name( );

A static member function can not be virtual.

There should not be member function does not have 'this' pointer.

Write a program to demonstrate static member function.

#include<iostream.h>
#include<conio.h>
class test
{
int code;
static int count;
public:
void setdata( )
{
 code=++count;
}
void show( )
{
 cout<<"\n\n\tObject Number--->"<<code<<endl;
}
static void showcount( )
{
 cout<<"\n\n\tCount is--->"<<count<<endl;
}
};
int test::count;
void main( )
{
test t1,t2,t3;
clrscr( );
t1.setdata( );
t2.setdata( );
test::showcount( );
t3.setdata( );
test::showcount( );
t1.show( );
t2.show( );
t3.show( );
getch( );


Output:-
    Count is--->2
    Count is--->3
    Object Number--->1
    Object Number--->2
    Object Number--->3

0 Comments:

Post a Comment

Popular Posts

Categories

Android (21) AngularJS (1) Books (3) C (75) C++ (81) Data Strucures (4) Engineering (13) FPL (17) HTML&CSS (38) IS (25) Java (85) PHP (20) Python (85) R (69) Selenium Webdriver (2) Software (13) SQL (27)