Questions: 1. Define friend class and specify its importance. Explain with suitable example. 2. List the characteristics of friend function.
Friend
Function
The
friend function is a function that is not a member function of the class but it
can access the private and protected members of the class.
The
friend function is given by a keyword friend.
These
are special functions which are declared anywhere in the class but have given
special permission to access the private members of the class.
#include<iostream>
using namespace std;
class test
{
int data;
friend
int fun(int x);//declaration of friend function
public:
test()//constructor
{
data = 5;
}
};
int fun(int x)
{
test obj;
//accessing
private data by friend function
return
obj.data + x;
}
int main()
{
cout
<< "Result is = "<< fun(4)<<endl;
}
Output
Result is = 9
1)
The friend functions are not part of the class and they are external.
2)
The friend function is not called using the object of that class. Infact, it is
called like a normal function without object.
3)
The friend function can be declared in public or private part without affecting
its meaning.
4)
The friend function can be friend of more than one class, hence they can be
used for message passing between the classes.
5)
The friend of one class can be friend of another class.
Friend
class : Similar to a friend function one can declare a
class as a friend to another class. This allows the friend class to access the
private data members of the another class. In the following program class B is
declared as friend of class A. Therefore class B can access the variable data,
and variable is private member of class A.
C++
Program
#include<iostream>
using namespace std;
class A
{
private:
int data;
friend
class B;//class B is friend of class A
public:
A()//constructor
{
data = 5;
}
};
class B
{
public:
int
sub(int x)
{
A obj1;
//object of class A
//the
private data of class A is accessed in class B
//data
contains 5 and x contains 2
return
obj1.data – x;
}
};
void main()
{
B obj2;
clrscr();
cout
<< "Result is = "<< obj2.sub(2):
}
Output
Result is = 3
For
certain specific application one can declare either a friend function or a
friend class. But if all the functions or classes are declared as friend then
it will lose the purpose of data encapsulation and data hiding.
Write a C++ program to
assign 'n' projects to 'm' programmers based on skill set of programmers using
friend function. Use static variable to count number of assignments.
Solution :
#include <iostream>
using namespace std;
class Programmers;
class Projects
{
static
int n;
public:
void
setassignments (int i){n=i;}
friend
void display (Projects,Programmers);
};
class Programmers
{
static
int m;
public:
void
setassignments (int i) {m=i;}
friend
void display(Projects,Programmers);
};
void display(Projects N,Programmers M)
{
cout<<"\n
number of assignments in projects are: ";
cout<<N.n;
cout<<"\n
number assignments allocated to programmers are: ";
cout<<M.m;
}
int Projects::n;
int Programmers::m;
void main()
{
Projects
prj;
prj.setassignments(10);
Programmers
prm;
prm.setassignments(5);
display(prj,prm);
}
Write a C++ program to
solve eight queens problem with friend function
Solution :
/**************************************************************************
This program is to implement n‒queens problem using backtracking
and friend function
**************************************************************************/
#include<iostream>
#include<cmath>
using namespace std;
class Backtrack
{
private:
int
x[20];
public:
Backtrack(){}
void
print board (int);
friend
void Queen(int,int,Backtrack); // Declaring the Queen function
as Friend function
int
place(int,int);
};
/* This function is for printing the solution to n‒queens
problem */
void Backtrack:: print board(int n)
{
cout<<"\n\nSolution....\n";
for(int
i=1;i<=n;i++)
{
cout<<<<" "<<i;
}
for(i=1;i<=n;i++)
{
cout<<"\n\n"<<i;
for(int
j=1;j<=n;j++)// for nXn board
{
if(x[i]= =j)
cout<<"
Q";//Queen at i,j position
else
cout<<" ‒";// empty slot
}
}
cout<<"\n
Press any key to continue...";
}
/* This function is for checking for the conflicts. If there is no conflict for the desired
position it returns 1 otherwise it
returns 0 */
int Backtrack::place (int row,int column)
int i;
for(i=1;i<=row‒1;i++)
{ //checking for column and diagonal conflicts
if(x[i] =
= column)
return 0;
else
if(abs(x[i]
‒ column) = = abs(i ‒ row))
return 0;
}
//no conflicts hence Queen can be placed
return 1;
}
/* By this function we try the next free slot and check for proper positioning of queen */
void Queen(int row,int n,Backtrack obj)
{
int
column;
for(column=1;column<=n;column++)
if(obj.place(row,column))
{
obj.x[row]
= column;
if(row= =
n)
{
obj.print_board(n);
//printing the board configuration
}
else
//try next queen with next position
Queen(row+1,n,obj);
}
}
}
void main()
{
int n,
Backtrack
obj;
cout<<"\n\t
Program for n‒Queens Using Backtracking";
cout<<"\nEnter
Number of Queens";
cin>>n;
Queen(1,n,obj);//trace
using backtrack
}
Note :
For the sake of simplicity giving the solution of 4 queeens problem. The above
program also works for 8 queens
Output
Solution....
1 2
3 4
1 ‒ Q ‒ ‒
2 ‒ ‒ ‒ Q
3 Q ‒ ‒ ‒
4 ‒ ‒ Q ‒
Press any key to
continue...
Solution....
1 2 3 4
1 ‒ ‒ Q ‒
2 Q ‒ ‒
‒
3 ‒ ‒ ‒ Q
4 ‒ Q ‒ ‒
Press any key to
continue...
Write a member function
and friend function to subtract two complex numbers in C++.
Solution :
#include<iostream>
#include<cmath>
using namespace std;
class complex
{
public:
float
real,img:
complex(){real=0;img=0;}
void
getData()
{
cout<<"\n
Enter real value: ";
cin>>real;
cout<<"\n
Enter imaginary value:" ;
cin>>img;
}
friend
complex complex::sub(complex obj1,complex obj2)
{
complex
temp;
temp.real=obj1.real‒obj2.real;
temp.img=obj1.img‒obj2.img;
return
temp;
}
void
display()
{
if(img<0)
cout<<real<<"‒i"<<abs(img);
else
cout<<real<<"+i"<<img;
}
};
void main()
{
complex
a,b;
a.getData();
b.getData();
complex
c=sub(a,b);
cout<<"\n
First Number: ";
a.display();
cout<<"\n
Second Number: ";
b.display();
cout<<"\n
The subtraction of two complex numbers is...";
c.display();
}
Output
Enter real value: 10
Enter imaginary value:
4
Enter real value: 3
Enter imaginary value:
2
First Number: 10+i4
Second Number: 3+i2
The subtraction of two
complex numbers is... 7+i2
Write a program for
swapping two numbers using friend function.
Solution :
#include<iostream>
using namespace std;
class Test
{
private
int x, y,
public:
void
setdata(int a, int b)
{
x= a;
y = b;
}
void display()
{
cout
<< "x="<<x<< "y="<<y;
}
friend
void swap(Test &t);
};
void swap(Test &t)
{
int temp;
temp = t.x;
t.x =
t.y;
t.y = temp;
}
void main()
{
Test obj;
int x1,
x2;
cout
<< "\nInput two Numbers:";
cin
>> x1 >> x2;
obj.setdata(x1,
x2);
cout
<< "\nBefore Swapping\n";
obj.display();
swap(obj);
cout
<< "\nAfter Swapping\n";
obj.display();
}
Input two Numbers:10
20
Before Swapping
x=10 y=20
After Swapping
x=20 y=10
1. Define friend class
and specify its importance. Explain with suitable example.
2. List the
characteristics of friend function.
Data Structures using C PlusPlus: Chapter 1: Data Abstraction and Overloading : Tag: Data Structure, C++, C Programing : Data Structures using C ++ Program - Friend Function
Data Structures using CPlusPlus
CS25C05 2nd Semester ECE Dept | 2025 Regulation | 2nd Semester 2025 Regulation
English Essentials II
EN25C02 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation
Tamils and Technology தமிழர்களும் தொழில்நுட்பமும்
UC25H02 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation
Linear Algebra
MA25C02 2nd Semester | 2025 Regulation
Electron Devices
EC25C01 2nd Semester ECE Dept | 2025 Regulation | 2nd Semester 2025 Regulation
Data Structures using CPlusPlus
CS25C05 2nd Semester ECE Dept | 2025 Regulation | 2nd Semester 2025 Regulation
Circuits and Network Analysis
EC25C02 2nd Semester ECE Dept | 2025 Regulation | 2nd Semester 2025 Regulation
Re-Engineering for Innovation
ME25C05 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation
Engineering Drawing - Laboratory
ME25C01 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation
Data Structures using CPlusPlus - Laboratory
CS25C05 2nd Semester ECE Dept | 2025 Regulation | 2nd Semester 2025 Regulation
Devices and Circuits Laboratory
EC25C03 2nd Semester ECE Dept | 2025 Regulation | 2nd Semester 2025 Regulation