AIR UNIVERSITY
DEPARTMENT OF ELECTRICAL AND COMPUTER
ENGINEERING
LAB TITTLE: Friend Classes and Friend Functions
Student Name: RIZWAN AHMAD Reg.No: 190450.
Objective:
⮚ To understand the concept of making classes and functions friend to other classes for
data sharing.
LAB ASSESSMENT:
Excellent Good Average Satisfactory Unsatisfactory
Attributes
(5) (4) (3) (2) (1)
Ability to Conduct
Experiment
Ability to assimilate the
results
Effective use of lab
equipment and follows
the lab safety rules
Total Marks: Obtained Marks:
LAB REPORT ASSESSMENT:
Excellent Good Average Satisfactory Unsatisfactory
Attributes
(5) (4) (3) (2) (1)
Data presentation
Experimental results
Conclusion
Total Marks: Obtained Marks:
Date: Signature:
Task1:
I. Define a TV class representing a television and a Remote class representing a remote
control. You can represent a television with a set of state members—that is, variables
that describe various aspects of the television. Here are some of the possible states:
• On/off
•Channel setting
•Volume setting
•Cable or antenna tuning mode
• TV input (TV or AV)
The input selection chooses between TV,
which could be either cable or
broadcast TV, and a VCR. Some sets may
offer more choices, such as multiple VCR/DVD inputs, but this list is
enough for the purposes of this example. Also, a television has some parameters that
aren’t state variables. For example, televisions vary in the number of channels they can
receive, and you can include a member to track that value. i.e. Max Channels and Max
volume. Viewer can also be able to see the following settings if TV is on
“Volume setting = “
“Channel setting = “
“Mode = “
“Input = “
Next, you must provide the class with methods for altering the settings. Many
televisions these days hide their controls behind panels, but it’s still possible with most
televisions to change channels, and so on, without a remote control. However, often
you can go up or down one channel at a time but can’t select a channel at random.
Similarly, there’s usually a button for increasing the volume and one for decreasing the
volume.
A remote control should duplicate the controls built in to the television. A single remote
of a company control can control several televisions of the same company. Many of its
methods can be implemented by using TV methods. In addition, a remote control
typically provides random access channel selection. That is, you can go directly from
channel 2 to channel 20 without going through all the intervening channels. Also, many
remotes can work in two modes—as a television controller and as a VCR controller.
Code:
Output:
Task2
Write a program to find Maximum out of two numbers using friend function. Here one
member is of one class and second belongs to another class.
Code:
Output:
Task3:
Write a program to swap the values of private data members of classes names class_1 and
class_2 using friend keyword.
Code:
OUTPUT
Task 4.
Create two classes DM and DB which stores the value in distances. DM stores distance in
meters and centimeters and DB in feet and inches. Write a program that can read values
for the class objects and add one object of DM with another object of DB. Use a friend
function to carry out the addition operation. The object that stores the results may be a
DM object or DB object, depending on the units in which the results are required. The
display should be in the format of feet and inches or meters and centimeters depending
on the object on display.
Code:
OUTPUT:
Conclusion:
In this Lab we have learned about the concept of making classes and functions friend to
other classes for data sharing. We get familiared with friend class and its applications
and programming.
THINK:-
i. Why friendship is needed?
ANS. Friendship helps in accessing the private data members of class. Thus it enables
a class to grant the access to any other class without inheriting that class.
ii. Inheritance vs. friends, what do you prefer? Justify?
ANS. Inheritance and friendship are very different. With friendship, class C is NOT an
instance of class A or class B. With friendship, class A and B can touch all the private
member data and functions of C. With inheritance, class C can touch the public and
protected members of A and B.
iii. Why do we make friend functions?
ANS. In object-oriented programming, a friend function, that is a "friend" of a given
class, is a function that is given the same access as methods to private and protected
data. A friend function is declared by the class that is granting access, so friend
functions are part of the class interface, like methods.
iv. Are friend functions parts of a class?
ANS. A friend function of a class is defined outside that class' scope but it has the
right to access all private and protected members of the class. Even though the
prototypes for friend functions appear in the class definition.
v. Does friendship effects the privacy of class?
ANS. A friend function is a function that is not a member of a class but has access to
the class's private and protected members. ... The friend declaration can be placed
anywhere in the class declaration. It is not affected by the access control keywords.
THE END