5 - Static & Friend Functions
5 - Static & Friend Functions
Programming
1
Objectives
• Static as Keyword
• Static Members
o Member Variable / Data Member
o Member functions
• Friend Functions
• Friend Classes
2
Static as keyword
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.
6
Static inside a loop (Cont.)
7
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
9
Class
(Cont.)
• Important detail to keep in mind:
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.
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.
19
“this” Operator in Class
• Every object in C++ has access to its own
address through an important pointer called this
operator / pointer.
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
24
Friend Functions of
Classes
• A friend function may or may not be a member
of another class.
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
28
Friend Classes of a Class
• In the similar manner as friend function an entire
class can also be a friend of another class.
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