Object Oriented
Programming
Static & Friend Functions
1
Objectives
• Static as Keyword
• Static Members
o Member Variable / Data Member
o Member functions
• Friend Functions
• Friend Classes
2
Static as keyword
• Unluckily, the keyword static, though the word
means "unchanging“ has multiple and
apparently unrelated uses
• static can be used in three major contexts:
o inside a function or a block
o inside a class
o in front of a global variable
3
Static keyword inside a
function
• The use of static inside a function is the
simplest that once the variable has been
initialized, it remains in memory until the end of
the program.
• Means the variable sticks around, maintaining its
value, until the program completely ends.
void func(){
static int a;
}
4
Guess the output?
void func(){
static int a;
cout<<a++<<endl;
}
Int main(){
func();
func();
func();
}
5
Static inside a loop
• Static keyword can be used inside a loop to
prevent a variable from being reinitialized.
for(int x=0; x<10; x++)
{
for(int y=0; y<10; y++)
{
static int number_of_times = 0;
number_of_times++;
}
}
Guess What will be the output
6
Static inside a loop (Cont.)
• The trick is that the keyword static prevents re-
initialization of the variable.
• One feature of using a static keyword is that it
automatically initialized a variable to zero
o but don't rely on this behavior (it makes your
intention unclear).
7
Static inside a Class
Both member (variables and methods)
can be static inside a class
8
Static variables inside a
Class
• All other variables declared inside a class are
basically the property of the object of that class
o That means for each instance of a class, the variable can
have a different value
• While a static member variable has the same
value in any instance (object) of the class
• And doesn't even require an instance (object) of
the class to exist
9
Class
(Cont.)
• Important detail to keep in mind:
o when debugging or implementing a program using a static
class member is that you cannot initialize the static
class member inside of the class.
o Means declare inside the class and initialize outside
the class similar to the way we define function outside of
class.
Scope Resolution Operator
o Syntax:
• type class_name::static_variable = value;
10
variable
In main.cpp file
class Box{
private:
static int count; //correct syntax
public:
static int count2; //correct syntax
};
//correct syntax
int Box::count = 0; int Box::count2;// we will not use
static here
main(){
cout<<Box::count; //Error because it is private
cout<<Box::count2; //Ok because it is public
} 11
Try it now
Lets See
12
Static methods inside a
Class
• You can also have static member functions of a
class
o By declaring a function member as static, you make it
independent of any particular object of the class.
• Static member functions are functions that do not
require an instance of the class
• You access static member function with the class
name rather than an instance name using the scope
resolution operator
• However the static member can be accessed with
instance name.
13
Example Static methods
class Box
{
public:
static int objectCount; // static variable
declaration
// Constructor definition
Box(double, double, double);
double Volume();
static int getCount(); // static method
declaration
private:
double length; // Length of a box
double breadth; // Breadth of a box
double height; // Height of a box
};
14
Example Static methods
(cont.)
// Static variable initialization out of the class
int Box::objectCount = 0; // we will not use static
here
// Constructor definition (a non static member)
Box::Box(double l=2.0, double b=2.0, double h=2.0)
{
cout <<"Constructor called." << endl;
length = l;
breadth = b;
height = h;
// Increase every time object is created
objectCount++;
}
15
Example Static methods
(cont.)
double Box::Volume()
{
return length * breadth * height;
}
int Box::getCount() // we will not use
static here
{
return objectCount;
}
16
Example Static methods
(cont.)
int main(){
Box b1, b2;
//Static method didn’t required the object to access it
cout<<"Object
count"<<Box::getCount();
return 0;
}
17
Try it now
Lets See
18
Your Turn
Assignment
• Write down a class with the name of “MyMath”
having your defined math functions.
• Right now just write two functions that you feel
easy to implement for you
• Conditions:
o All the functions should define out of the class.
o All functions shouldn’t require the instance of the
class to be called
19
“this” Operator in Class
• Every object in C++ has access to its own
address through an important pointer called this
operator / pointer.
• Alternatively we can say that, with this operator
we can access the objects own members
(Methods & Variables)
o Syntax: Arrow operator (Usually used with pointer)
• this member_Variable;
• this member_method(parameter);
20
Example “this” Operator
class Box{
double width, height, length;
public:
Box(double width, double height, double
length);
bool equal(Box b);
};
21
Example “this” Operator
//Constructor of class Box
Box::Box(double width, double height, double length){
//this pointing to the variable of the object of the class
this width = width;
this height = height;
this length = length;
}
//equal method of Box class
bool Box:: equal(Box b){
if(this width == b.width &&
this height == b.height &&
this length == b.length) return true;
else return false;
}
22
Try it now
Lets See
23
Friend Functions of
Classes
• A friend function is not the actual member
function of the class
But
• Friend function can even access the private
members of a class.
How? We will see it a bit latter
24
Friend Functions of
Classes
• A friend function may or may not be a member
of another class.
• Friend function can be just like a normal
function of any C / C++ program.
• Keyword: simply use the friend keyword in front
of the prototype of the function you wish to be
a friend of the class.
• It doesn’t matter whether you declare the friend
function in the private or public section of the
class.
25
Friend Functions
(Example)
class Accumulator{
private:
int mValue;
public:
Accumulator() { mValue = 0; }
void Add(int nValue) { mValue += nValue; }
// Make the Reset() function a friend of this
class
friend void Reset(Accumulator &cAccumulator);
int getAccumulator() { return mValue; }
};
// Reset() is now a friend of the Accumulator class
void Reset(Accumulator &cAccumulator){
// And can access the private data of Accumulator objects
cAccumulator.mValue = 0;
}
26
Friend Functions
(Example)
int main(){
Accumulator ax;
ax.Add(10);
ax.Add(10);
cout<<"Value is "<<ax.getAccumulator();
Reset(ax);
cout<<"Value is "<<ax.getAccumulator();
return 0;
}
27
Friend Functions of
Classes
A function can be a friend of more than one
class at the same time.
28
Friend Classes of a Class
• In the similar manner as friend function an entire
class can also be a friend of another class.
• In this way the members of the friend class can
have access to the members of the other class.
Read out and discuss the given handouts in
groups to know more about it and for the
benefits of friend functions
29
Reading Material
• Chapter 8
o Innovative’s Object Oriented programming Using C++
• Chapter 12
o C++ Programming: From Problem Analysis to Program
Design, Third Edition by D. S. Malik
30