Professional Documents
Culture Documents
int main()
{
foo f1, f2, f3;
cout << “f1 count is " << f1.getcount() << endl;
cout << “f2 count is " << f2.getcount() << endl;
cout << “f3 count is " << f3.getcount() << endl;
return 0;
}
Separate Declaration and Definition
Static member data requires an unusual format
Ordinary variables are usually declared (the compiler is told
about their name and type) and defined (the compiler sets aside memory to hold
12
Friend Functions
A friend is a function that is not a member of a
class, but has access to the private members
of the class.
A friend function of a class can be a global
function.
Access specifier do not apply to the friends of
a class.
This friendship is unidirectional.
13
Creating Friends
Step-1: Place a prototype in the class
declaration, prefixing the declaration with the
keyword friend.
Step-2: Write the function definition.
14
Example
15
16
Note
A friend keyword should appear in the prototype only
and not in the definition.
Since it is a non-member function of the class of which
it is a friend, it can be prototyped in either the private
or public section of the class.
A friend function takes one extra parameter as
compared to a member function that performs the
same task. This is because it cannot be called with
respect to any object. Instead, the object itself
appears as the parameter in the function call.
17
Example 2
18
Friend
Function
19
20
Out Put
21
Example-3
#include <iostream.h>
class Point
{
int x,y; A friend function
of Point class
public:
void print();
22
void Point::print()
{
cout<<"X="<<x<<endl;
cout<<"Y="<<y<<endl;
} Not a member of
any class. Global
void setValue(Point &p) Function. But it can
{ access private data
p.x = 5; of Point class.
p.y = 2;
}
int main()
{
Point obj;
setValue(obj);
obj.print();
return 0;
}
23
Example-4
#include <iostream.h>
class Beta;
class Alpha
{
int data;
public: A friend function
Alpha() of Alpha class
{
data=3;
}
24
class Beta
A friend function
{ int data; of Beta class
public:
Beta()
{
data=7;
}
25
Not a member of
any class. Global
int friFunc(Alpha a, Beta b) Function. But it can
{ access private data
of Alpha & Beta
return a.data+b.data; class.
}
int main()
{
Alpha obja;
Beta objb;
cout<<"Sum="<<friFunc(obja,objb)<<endl;
return 0;
}
26
Summery
In principle, private and protected members of a class
cannot be accessed from outside the same class in
which they are declared.
However, this rule does not apply to "friends“
A non-member function can access the private and
protected members of a class if it is declared
a friend of that class.
That is done by including a declaration of this external
function within the class, and preceding it with the
keyword friend:
friend Classes
friend Classes
30
Rectangle class
values can be
accessed in
square
31
Friend Class
class Square;
class Rectangle
{ class Square
int width, height; {
public: friend class Rectangle;
private:
int area ()
int side;
{ public:
return (width * height); Square (int a) : side(a) {}
} };
void convert (Square a);
};
32
Friend Class
void Rectangle::convert (Square a)
{
width = a.side;
height = a.side;
}
int main ()
{
Rectangle rect;
Square sqr (4);
rect.convert(sqr);
cout << rect.area();
return 0;
}
33
In this example, class Rectangle is a friend of
class Square allowing Rectangle's member functions to access
private and protected members of Square. More
concretely, Rectangle accesses the member
variable Square::side, which describes the side of the square
There is something else new in this example: at the beginning
of the program, there is an empty declaration of class Square.
This is necessary because class Rectangle uses Square (as a
parameter in member convert),
andSquare uses Rectangle (declaring it a friend).
Friendships are never corresponded unless specified: In our
example, Rectangle is considered a friend class bySquare, but
Square is not considered a friend by Rectangle. Therefore, the
member functions of Rectangle can access the protected and
private members of Square but not the other way around. Of
course, Square could also be declared friend of Rectangle, if
needed, granting such an access.
Example-2 (cont’d)
We want friFunc() to have access to both
classes’ private data members, so we make it a
friend function.
Class Alpha and Beta are referred to in the
declaration of the function friFunc(), so we need to
declare these two classes:
– class Beta; //declaration in alpha.h
– class Alpha; //declaration in beta.h
35
Friend Class
We can also declare a whole class to be a friend of
another class.
All the member functions of the friend class will have
unrestricted access to the members of the class of
which it has been declare a friend.
36
Example
/* Header File (Alpha.h) */
class Beta;
class Alpha
{ A friend class of
Alpha class
int data;
public:
Alpha() { data=3; }
friend Beta;
};
37
Example (cont’d)
/* Header File (Beta.h) */
#include <iostream.h>
class Alpha;
Can access private
class Beta data of Alpha class
{
public:
void func1(Alpha a) {cout<<a.data; }
};
38
Example (cont’d)
/* Test Driver Program for Point class (driver.cpp) */
#include <iostream>
#include "Alpha.h "
#include "Beta.h"
using namespace std;
int main() {
Alpha obja;
Beta objb;
objb.func1(obja);
return 0;
}
39
friend class
class alpha {
private:
int data1;
public:
alpha() : data1(99) { } //constructor
friend class beta; //beta is a friend class
};
class beta {
public:
void func1(alpha a){ cout << "\ndata1=" << a.data1; }
void func2(alpha a){ cout << "\ndata1=" << a.data1; }
};
friend class
int main()
{
alpha a;
beta b;
b.func1(a);
b.func2(a);
cout << endl;
return 0;
}
friend class
In class alpha the entire class beta is
proclaimed a friend. Now all the member
functions of beta can access the private data
of alpha