In this article static methods we give the information about we can invoke a that member function using object and dot operator (.) but we should use class name and scope resolution operator :: to invoke static members.

Static Methods:

Static Member Function in C++:

We can access any Static Data Member by any Static Member Function in C++. But to access a that Data variable, a special type of Function should be used. This special type of Member Function is called Static Member Function.

This function does not apply to any object, but like static variables, this function is applied to the whole class.

To create a static member function, we have to use the static keyword while declaring the function.

Whenever a member function is declared as static, it does not depend on the objects of the class.

We can invoke a that member function using object and dot operator (.) but we should use class name and scope resolution operator :: to invoke static members.

static member functions can access only static data members and other static member functions. They cannot access non-static data members and member functions of the class.

class data

{

private:

public:

static int stafunc() // function definition

{

// can access only static member data

}

};

main()

{

data::stafunc(); // function call

}

Program based on Static Member Function in C++:

// Static member Function
#include<iostream.h>
#include<conio.h>
class test
{
int code; // auto data member
static int count; // static data member
public:
void setcode() // member function
{
code=++count;
}
void showcode() // member function
{
cout<<“\n\n Object Number: “<<code;
}
static void showcount() // static member function
{
cout<<“\n\n Count Value: ” <<count;
}
};
int test::count; // define static data member
void main()
{
test p,q; // create 2 objects
clrscr();
p.setcode(); // function call
q.setcode();

test::showcount(); /* accessing static function using class name and scope resolution operator*/
test r;
r.setcode();

test::showcount();

p.showcode();
q.showcode();
r.showcode();

getch();
}

OUTPUT:

Count Value: 2

Count Value: 3

The Object Number: 1

Object Number: 2

Object Number: 3

Some More: 

POP- Introduction to Programming Using ‘C’

DS – Data structure Using C

OOP – Object Oriented Programming 

Java Programming

DBMS – Database Management System

RDBMS – Relational Database Management System

Join Now: Data Warehousing and Data Mining 

Leave a Reply

Your email address will not be published. Required fields are marked *