Friend Function and Friend Class
Friend Function and Friend Class
04/02/11
Local Classes
A class may be declared inside a function, in which it is called local class. The scope of a local class is up to the function definition.
Contd..
class Y { private : int y ; public : Y(int b ) { y = b ;} int gety () { return y ; } } y_object (10) ; return (x_object.getx() * (y_object.gety() ) ) ; }
Contd..
void main () { cout << Function () << endl ; }
Syntax
It starts with the keyword friend followed by the type of function and identifier(name) for the function which is followed by a list of types of parameters of the function enclosed in parentheses(). friend type identifier (type_parameter1, type_parameter2,) ;
Example
#include<iostream> using namespace std; class Rect { friend int Area(const Rect &a); int x,y ; public : Rect(int L,int W) { x = L, y = W ; } };
Contd..
int Area (const Rect &b) { return b.x*b.y ; }; int main() { Rect R1(5,6), R2(3,4) ; cout << Area of R1 = << Area(R1)<<\n ; cout << Area of R2 = << Area (R2) <<\n ; return 0 ; }
Contd..
It can be declared in public as well as private section of a class. It can be declared as a friend to several classes It can be a member of one class and friend of another. It cannot be inherited by a derived class. This means that a friend function of a base class is not the friend of the derived classes.
Example
#include<iostream.h> class width ;// forward declaration class length { int len ; public : length() { } length(int a) { len = a ; } friend void area (length, width);// declaration of friend // function };
Contd..
class width { int wid ; public : width() { } width (int b) { wid = b ; } friend void area(length, width) ; };
Contd..
void area (length a, width b) { int area ; area = a.len*b.wid ; cout << \n The area of the rectangle is <<area ; } void main() { length a(5); width b(3) ; area(a,b) ; return ; }
Contd..
friend double cost(const Rect &a, double); }; int Area (const Rect &b) { return b.x*b.y ; } double cost(const Rect &b, double s) { return b.x*b.y*s ; }
Contd..
int main() { double A = 4.5, B = 5.2; Rect R1(10,5), R2(20,5); cout<< Area of R1 =<<Area(R1)<<\n ; cout<< Area of R2 =<<Area(R2)<<\n ; cout<< cost =<<cost(R1,A)<<\n ; cout<< cost =<<cost(R1, B)<<\n ; return 0; }
Contd..
class Sqr { int side ; public : Sqr (int C) { side = C ; } int Area () { return side * side ; } friend void Display (Rect R, Sqr S) ; };
Contd..
void Display (Rect R, Sqr S) { cout << Area of rect = << R.area()<<endl ; cout << Area of Square = <<S.Area()<<endl ; } int main () { Rect R1(10,5) ; Sqr S1(10) ; Display (R1, S1) ; return 0 ; }
Friend Class
When all or most of the functions of a class need access to data members and function members of another class, the whole class may be declared friend to the class. E.g: if the functions of a class A need to access the public, private and protected members of another class B, the class A may be declared as friend class A in the body of class B.
Declaration is as below:
class B { friend class A; private : statements friend class C ; public : other statements ; };
Contd..
The C++ friendship is neither transmitted nor it is reciprocal. If class A is friend of class B is friend of class C, it does not mean that class A is also friend of class C or class B is friend of class A or class C is friend of class B or class A. There is no limit on the number of friends of a class.
Example
#include<iostream.h> class Cuboid { friend class paint ; public : void sides (int, int, int); int Area (); int volume (); int x,y,z ; };
Contd..
void Cuboid :: sides (int L, int W, int H) { x = L , y = W, z = H ; } int Cuboid :: Area () { return 2*(x*y + y*z + z*x) ; }
Contd..
int Cuboid :: volume () { return x*y*z ; } class paint { private : int R ; public : paint () { R = 1; } paint (int S) { R = S ; } Cuboid C;
Contd..
int area() { return C.Area(); } int cost(int R, Cuboid C) { return R*C.Area() ; } }; int main() { Cuboid C1 ; C1.sides(5,6,5) ; paint P1 ; int k = 4 ; cout << volume of C1 = <<C1.volume()<<\n ; cout << surface area of C1 = << C1.Area()<<\n ; cout << cost of painting P1= << P1.cost(k,C1)<<\n ; return 0 ; }