0% found this document useful (0 votes)
9 views5 pages

Friend Function Lab 02122024 011233pm

Uploaded by

ridasaman47
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views5 pages

Friend Function Lab 02122024 011233pm

Uploaded by

ridasaman47
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

EXPERIMENT 09

Friend Functions and Friend Classes


Objectives:
• To learn friend functions and friend classes
• To apply friendship while designing software solutions
Equipment /Tool:
PC, Microsoft Visual Studio 2022
Background:

Friendship:
Friendship allows users to access private or protected attributes of a class. In principle, private
and protected members of a class cannot be accessed from outside the same class in which they
are declared. However, this rule does not apply to "friends".

Friend classes and non-member functions can be friend by adding a ‘friend’ keyword.

Friend Functions:
Friend functions are the non-member functions which can access the private and protected
members of a class if it is declared a friend of that class. That is done by including a declaration
of this external function within the class, and preceding it with the keyword friend.
Example
// friend functions
#include <iostream>
using namespace std;

class Rectangle {
int width, height;
public:
Rectangle() {}
Rectangle (int x, int y) : width(x), height(y) {}
int area() {return width * height;}
friend Rectangle duplicate (const Rectangle&);
};
Rectangle duplicate (const Rectangle& param)
{
Rectangle res;
res.width = param.width*2;
res.height = param.height*2;
return res;
}

88
int main () {
Rectangle foo;
Rectangle bar (2,3);
foo = duplicate (bar);
cout << foo.area() << '\n';
return 0;
}

The duplicate function is a friend of class Rectangle. Therefore, function duplicate is able to
access the member’s width and height (which are private) of different objects of type Rectangle.
Notice though that neither in the declaration of duplicate nor in its later use in main, function
duplicate is considered a member of class Rectangle. It isn't! It simply has access to its private
and protected members without being a member.
Typical use cases of friend functions are operations that are conducted between two different
classes accessing private or protected members of both.

Friend Classes:
Like friend functions, a friend class is a class whose members have access to the private or
protected members of another class:
Example
// friend class
#include <iostream>
using namespace std;
class Square;
class Rectangle {
int width, height;
public:
int area ()
{return (width * height);}
void convert (Square a);
};
class Square {
friend class Rectangle;
private:
int side;
public:
Square (int a) : side(a) {}
};
void Rectangle::convert (Square a) {
width = a.side;

89
height = a.side;
}
int main () {
Rectangle rect;
Square sqr (4);
rect.convert(sqr);
cout << rect.area();
return 0;
}

In this example, class Rectangle is a friend of class Square allowing Rectangle's member
functions to access private and protected members of Square. More concretely, Rectangle
accesses the member variable Square::side, which describes the side of the square.
There is something else new in this example: at the beginning of the program, there is an empty
declaration of class Square. This is necessary because class Rectangle uses Square (as a
parameter in member convert), and Square uses Rectangle (declaring it a friend).
Friendships are never corresponded unless specified: In our example, Rectangle is considered a
friend class by Square, but Square is not considered a friend by Rectangle. Therefore, the
member functions of Rectangle can access the protected and private members of Square but not
the other way around. Of course, Square could also be declared friend of Rectangle, if needed,
granting such an access.
Another property of friendships is that they are not transitive: The friend of a friend is not
considered a friend unless explicitly specified.
Procedure:

Perform all the tasks and provide your code and result in the space below the questions.
Lab Tasks

Q 1:
a. Declare a class c1 having two data members, both of integer type. It should have a
constructor for giving the values to the members. It should have a friend function name
max() who can calculate maximum of the two data members.
Solution

90
b. Make a Distance class, having two data members float feet and float inches. It should
have a friend function square(), that initially converts total distance into feet and then
takes its square and return it to main where it is displayed.
Solution

Q 2:
a. Make a class named as Box. It should have three private data members corresponding to
its width, height and length. It should have two friend functions known as area() and
volume() which calculates its area and volume respectively out of the class
Solution

91
b. Define a class named as DATABASE that includes marks for all the subjects of a
semester. Declare its friend class STUDENT that calculates the sum of marks of all
subjects and stores the name, age and blood_group of every student and displays it.
Solution

92

You might also like