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

Friend Functions Cpp

The document explains the concept of friend functions and friend classes in C++, which allow non-member functions or other classes to access private members of a class. It includes code examples demonstrating how to declare friend functions and classes, as well as how to use them to manipulate private data. Additionally, it presents an example of a shopping list application that utilizes these concepts.

Uploaded by

dsp01097
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

Friend Functions Cpp

The document explains the concept of friend functions and friend classes in C++, which allow non-member functions or other classes to access private members of a class. It includes code examples demonstrating how to declare friend functions and classes, as well as how to use them to manipulate private data. Additionally, it presents an example of a shopping list application that utilizes these concepts.

Uploaded by

dsp01097
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Alphabet Computer Education, Narayangaon, 9960552002

Friend functions in C++

Friend functions are actually not class member function. Friend functions are made to
give private access to non-class functions. You can declare a global function as friend, or a
member function of other class as friend.

Hence, friend functions can access private data members by creating object of the class.
Similarly we can also make function of some other class as friend, or we can also make an
entire class as friend class.

When we make a class as friend, all its member functions automatically become friend
functions.

Function Class Example

In this example we have two classes XYZ and ABC. The XYZ class has two private data
members ch and num, this class declares ABC as friend class. This means that ABC can access
the private members of XYZ, the same has been demonstrated in the example where the
function disp() of ABC class accesses the private members num and ch. In this example we
are passing object as an argument to the function.

#include <iostream.h>

class XYZ

private:

char ch;

int num;

public:

void read()

cout<<”enter the character and number”;

cin>>ch>>num;

friend class ABC;


Alphabet Computer Education, Narayangaon, 9960552002

};

class ABC

public:

void disp(XYZ obj)

cout<<obj.ch<<endl;

cout<<obj.num<<endl;

};

int main() {

ABC obj;

XYZ obj2;

Obj2.read();

obj.disp(obj2);

return 0;

Friend Function:

Similar to friend class, this function can access the private and protected members of
another class. A global function can also be declared as friend as shown in the example below:

Friend Function Example

#include <iostream.h>

#include<conio.h>
Alphabet Computer Education, Narayangaon, 9960552002

class XYZ

private:

int num;

char ch;

public:

void read()

cout<<”enter the character and number”;

cin>>ch>>num;

friend void disp(XYZ obj);

};

void disp(XYZ obj)

cout<<obj.num<<endl;

cout<<obj.ch<<endl;

int main()

XYZ obj;

Obj.read();

disp(obj);

getch();
Alphabet Computer Education, Narayangaon, 9960552002

return 0;

Addition of members of two different classes using friend Function

#include <iostream.h>

#include<conio.h>

class B;

class A

private:

int numA;

public:

read(int n)

numA=n;

// friend function declaration

friend int add(A, B);

};

class B {

private:

int numB;

public:

put(int m)

{
Alphabet Computer Education, Narayangaon, 9960552002

numB=m;

friend int add(A , B);

};

int add(A objectA, B objectB)

return (objectA.numA + objectB.numB);

int main()

A objectA;

B objectB;

A.read(6);

A.read(43);

cout<<"Sum: "<< add(objectA, objectB);

getch();

return 0;

#include <iostream.h>

#include<conio.h>

class Box

double width;

public:
Alphabet Computer Education, Narayangaon, 9960552002

friend void printWidth( Box box );

void setWidth( double wid );

};

void Box::setWidth( double wid )

width = wid;

void printWidth( Box box )

cout << "Width of box : " << box.width <<endl;

int main()

Box box;

box.setWidth(10.0);

printWidth( box );

getch();

return 0;

Processing Shopping List

#include<iostream.h>

#include<conio.h>

const m=50;

class ITEMS

int itemCode[m];
Alphabet Computer Education, Narayangaon, 9960552002

float itemPrice[m];

int count;

public:

void CNT (void)

count=0;

void getitem(void);

void displaysum(void);

void remove(void);

void displayitems(void);

};

//===================================

void ITEMS :: getitem(void)

cout<< “Enter item code :”;

cin>>itemCode[count];

cout<< “Enter item cost :”;

cin>>itemPrice[count];

count++;

void ITEMS :: displaysum(void)

float sum=0;

for(int i=0;i<count;i++)

{
Alphabet Computer Education, Narayangaon, 9960552002

sum=sum+itemPrice[i];

cout<<”\n Total value : “<<sum<<”\n”;

void ITEMS :: remove(void)

int a;

cout<<”Enter itemcode : “;

cin>>a;

for(int i=0;i<count;i++)

if(itemCode[i] == a)

itemPrice[i] = 0;

void ITEMS :: displayitems(void)

cout<<”\n Code Price\n”;

for(int i=0;i<count;i++)

cout<<”\n”<<itemCode[i];

cout<<”\n”<<itemPrice[i];

cout<<”\n”;
Alphabet Computer Education, Narayangaon, 9960552002

int main()

ITEMS order;

order.CNT();

int x;

do

cout <<”\n You can do the following”;

cout <<”\n Enter appropriate number”;

cout <<”\n1 : Add an item”;

cout <<”\n2 : Display total value”;

cout <<”\n3 : Delete an item”;

cout <<”\n4 : Display all items”;

cout <<”\n5 : Quit”;

cout <<”\n\nWhat is your option”;

cin>>x;

switch(x)

case 1 : order.getitem();break;

case 2 : order.displaysum();break;

case 3 : order.remove();break;

case 4 : order.displayitems();break;

case 5 : break;

default: cout<<”Error in input, try again\n”;

}
Alphabet Computer Education, Narayangaon, 9960552002

while(x != 5);

getch();

return 0;

You might also like