0% found this document useful (0 votes)
87 views13 pages

Static Member Function

Uploaded by

julee9800
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
87 views13 pages

Static Member Function

Uploaded by

julee9800
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

2 Chapter

Static data members


and function
Static data members
Following are the characteristics of static member
● It is initialized to zero when the first object of its class is created. No other
initialization is permitted
● Only one copy of that member is crated for the entire class and is shared by all
the object of that class, no matter how many objects are created
● It is visible only within class, but its lifetime is
the entire program.
● Static variables are used to maintain values
common to the entire class.
Eg. This can be used as a counter that records the
occcurences of all the objects
Syntax:
static datatype variblename;
Eg:
static int a;
Example
#include <iostream>
using namespace std;
class Demo
{
public:
static int ABC;
};

//defining
int Demo :: ABC =10;

int main()
{

cout<<"\nValue of ABC: "<<Demo::ABC;


return 0;
}
Static Member function
• Like a static member variable we can also have static member function. A
member function declared with static keyword is called as static member
function. static member function has following characteristics:
1.Static function can have access to only other static members/functions
declared in same class
2.Static member function can be called using class name(instead of object) as
Class_name:: function_name;
SSS

Class_name::Function_name()
Example
#include <iostream>
using namespace std;class
Demo
{
private:
static int X; public:
static void fun()
{
cout <<"Value of X: " << X <<
endl;
}
};//defining
int Demo :: X =10;
int main()
{
Demo X; X.fun();

return 0;
}
Friend Function
• We have seen that private members can't be accessed from outside class.
Non member function cannot have access to the private data of a class.
• There would be situation where we would like two classes to share
particular function.
Eg consider two classes manager and scientist.
We like to use a function income_tax() to operate on object of both d classes.
• C++ allows the common function to be made friendly with the both the
classes to have access to the private data of these classes
• To make an outside function “friendly” to a class, we have to simply declare
this function as a friend function of that class.
Syntax:
Friend returntype function_name(arguements);
Eg:
Friend void add();
• The function that is declared with the keyword friend is known as friend
function
• This function can be defined else where in the program.
• The function definition does not use either keyword friend or the scope
operator::
• A function can be declared as a friend in any number of classes.
• A friend function although not a member function, has full access right to
the private members of the class
A friend function has certain special characteristics:
1.It is not in the scope of class to which it has been declared as friend
2.Since it is not in the scope of class, it cannot be us called using object of that
class
3.It can be invoked like a normal function without the help of any object.
4.Unlike member function it can’t access member names directly and has to use
an object name and dot membership operator with each member name
• It can be declared either in the public or private part of a class without
affecting its meaning

You might also like