LAB #06: Friend Classes: Description

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

Name of Student: _________________________________ Section: _____________________

Student’s Signature: _______________________________Date: ________________________


Name of Instructor: _______________________________ Instructor’s Signature: __________

LAB #06:

Friend Classes

Description:

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. If two classes do not fulfill “is-a”
relationship and are required to access the private member function of any class it is declared
as friend class of that class. Friend functions are also meant for the same purpose.

Friend classes

All member functions of a class become friends of the friendship-granting class. Friendship-
granting class allows the other class to access all the private, protected, public members
directly using (.) operator with Friendship-granting class object.

Friend Class Example


class alpha {
int data;
public:
alpha(): data(99){}
friend class beta; // granting friendship
};
class beta {
public:
void func1(alpha a) {cout<<a.data;}
void func2(alpha a) {cout<<a.data;}
};
Friendship-grant is not commutative. It is not dual side relationship until or unless
declared in both the classes separately.

LAB TASK

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.

TEST PLAN

Commands Output
Television t;
Remote r;
r.setChannel(t,25);
r.VolumeUp();
r.VolumeDown();
r.viewSettings(t);
r.setMode(t);
Television g=t;
r.setChannel(g,1);
r.channelDown();
r.channelDown();
r.viewSettings(g);

II. Overload >> operator using operator overloading and fiend function concept for
Complex class

You might also like