Session Objectives Define Static data member Static member functions in a class Object as Function...

11

Transcript of Session Objectives Define Static data member Static member functions in a class Object as Function...

Page 1: Session Objectives Define Static data member Static member functions in a class Object as Function Argument Friend Function Friend Class.
Page 2: Session Objectives Define Static data member Static member functions in a class Object as Function Argument Friend Function Friend Class.

Session ObjectivesSession Objectives

Define Static data member

Static member functions in a class

Object as Function Argument

Friend Function

Friend Class

Page 3: Session Objectives Define Static data member Static member functions in a class Object as Function Argument Friend Function Friend Class.

FRIEND FUNCTIONS

Why it is used? - The concept behind the encapsulation and data hiding restrict the non-member function from accessing the private members of the class.

The best way to access a private data member by a non-member function is to change a private data member to a public. but it violates the whole concept of data hiding and encapsulation.

To solve this problem friend functions are used when we can access the private or protected members.

Page 4: Session Objectives Define Static data member Static member functions in a class Object as Function Argument Friend Function Friend Class.

Friend informs the compiler that the function is not a member func.Of the class.

Syntax : friend<ret.Type> func_name(arguments);

Member Function Friend Function

It is a part of class definition and is invoked by a particular object

It is no a part of a class

It can access the data depending upon whether the function is private or protected

It can access any data irrespective of the private or public or protected access

Page 5: Session Objectives Define Static data member Static member functions in a class Object as Function Argument Friend Function Friend Class.

#include<iostream.h>class book{private :

int bno; char bname[20];public: void getdata(); friend void show(book);};void book::getdata(){cin>>bno>>bname;}void show(book bk){cout<<bk.bno<<bk.bname;}

void main(){book b;clrscr();b.getdata();show(b);getch();}

Page 6: Session Objectives Define Static data member Static member functions in a class Object as Function Argument Friend Function Friend Class.

FRIEND FUNCTIONSTo access the private member of the class inside the

friend function the object of the class should be passed as an argument to the function

#include<iostream.h>class second;class first{private :

int no;public:

first(int n); friend int add(first,second);};class second{

Page 7: Session Objectives Define Static data member Static member functions in a class Object as Function Argument Friend Function Friend Class.

private :int n1;

public:second(int);

friend int add(first,second);};

first ::first(int n) { no=n; }second ::second(int n){n1=n;}

int add(first f,second s){cout<<"First class="<<f.no<<endl;cout<<"Second class=“ <<s.n1;return f.no+s.n1;}void main(){first f(10);second s(20);clrscr();cout<<"\n The Result is "<<add(f,s);}

Page 8: Session Objectives Define Static data member Static member functions in a class Object as Function Argument Friend Function Friend Class.

FRIEND CLASSThe private members of one class can be accessed

from the member functions Of another class by making them as friends

#include<iostream.h>#include<conio.h>class second;class first{private :

int no;public:

friend class second;first(int n){

Page 9: Session Objectives Define Static data member Static member functions in a class Object as Function Argument Friend Function Friend Class.

no=n;}};class second{public:

void show(first f){cout<<f.no;}

};void main(){first f(10);second s;s.show(f);getch();}

Page 10: Session Objectives Define Static data member Static member functions in a class Object as Function Argument Friend Function Friend Class.

The friend function acts as a bridge between two objects of

different classes which can access even the private members of

the class

A friend class is a class whose members functions are all

friend functions of another class

Page 11: Session Objectives Define Static data member Static member functions in a class Object as Function Argument Friend Function Friend Class.

EXERCISES

Define “friend “ keyword & its use?

Difference between friend function and member functions?

Explain the use of friend function?

Discuss about friend class?