0% found this document useful (0 votes)
171 views

Friend Function and Friend Class

- A local class is a class declared inside a function, with its scope limited to that function. Two examples of local classes X and Y are given. - A friend function has access to all members of a class, whether public, private, or protected. The class declaration must specify which functions are friends. Syntax and an example are provided. - Features of friend functions are that they are not member functions, cannot directly access data members, and are not inherited by derived classes. Multiple examples demonstrate friend functions and friend classes.

Uploaded by

Abhishek Modi
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
171 views

Friend Function and Friend Class

- A local class is a class declared inside a function, with its scope limited to that function. Two examples of local classes X and Y are given. - A friend function has access to all members of a class, whether public, private, or protected. The class declaration must specify which functions are friends. Syntax and an example are provided. - Features of friend functions are that they are not member functions, cannot directly access data members, and are not inherited by derived classes. Multiple examples demonstrate friend functions and friend classes.

Uploaded by

Abhishek Modi
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 29

Classes and Objects-2

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.

Classes local to a function


#include<iostream> using namespace std ; int Function () { class X { private : int x ; public : X ( int a ) { x = a ; } int getx() { return x ; } } x_object(5);

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 ; }

Friend function to a class


A friend function to a class has access to all the members of the class whether these are private, protected or public. The class declaration should contain the declaration that the function is friend of the class. Also a friend function is not a member function of the class in which it is declared as friend.

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,) ;

Declaring Friend function


class class-name { public : ... // friend declaration friend void function-name(args); };

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 ; }

Features of a friend function


It is not a member of a class and therefore you can access it directly without using an object and dot operator. It cannot access the data member directly. You need to create objects of a class and then access the data members using a dot operator with the object name.

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 ; }

Class with more than one friend functions


#include<iostream.h> class Rect { friend int Area (const Rect &a); int x, y ; public : Rect (int L, int W) { x = L, y = W; }

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; }

A friend function to many classes


#include<iostream.h> class Sqr ; class Rect { int x,y ; public : Rect (int A, int B) { x = A, y = B; } int area() { return x*y ; } friend void Display (Rect R, Sqr S) ; };

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 ; };

Characteristics of Friend Class


If a class A is friend of class B, then functions of A can access all the members of B. If class A is friend of class B, it does not mean that class B is also friend of class A. The c++ friendship is not reciprocal. Friendship is granted by the class whose members are to be accessed. If a class A is friend of class B, it has to be so declared in the definition of class B.

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 ; }

You might also like