You are on page 1of 20

AIR UNIVERSITY

DEPARTMENT OF ELECTRICAL AND COMPUTER


ENGINEERING
LAB TITTLE: Friend Classes and Friend Functions

Student Name: HAMZA YAQOOB. Reg.No: 190490 .

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:
Task 1
code
#include<iostream>
using namespace std;
class TV
{
private:
int onoff;
int channel;
int vol;
string mode;
string input;
public:
friend class remote;
};
class remote
{
private:
int sel;
int rchannel;
public:
TV b;
int getonoff()
{
cout<<"**..Welcome here u can have your tv and remote controls..**"<<endl;
int i;
cout<<"TV on or off press 1 for on and 0 for off:"<<endl;
cin>>i;
if(i==1)
{
cout<<"***TV on***"<<endl;
}
else if(i==0)
{
cout<<"TV off"<<endl;
}
else
{
cout<<"INVALID INPUT!"<<endl;
}
return b.onoff;
cout<<"*****"<<endl;
}
int getchannel()
{
cout<<"Which channel you would like to see:"<<endl;
cin>>b.channel;
cout<<"Channel:"<<b.channel<<endl;

return b.channel;

}
int getvolume()
{
cout<<"volume:"<<endl;
cin>>b.vol;
cout<<"Volume entered:"<<b.vol<<endl;
return b.vol;
}
string getmode()
{
cout<<"Select mode:"<<endl;
cin>>b.mode;
return b.mode;
cout<<"Mode:"<<b.mode<<endl;
}
string getinput()
{
int h;
cout<<"*********"<<endl;
cout<<"Enter your selection:"<<endl;
cout<<"press : 1- Cable"<<endl;
cout<<" 2 -Broadcast Tv"<<endl;
cout<<"3-VCR"<<endl;
cin>>h;
if(h==1)
{
cout<<"**Cable TV is on**"<<endl;
b.input="cable";
return b.input;
}
else if(h==2)
{
cout<<"**Broadcast TV is on**"<<endl;
b.input="broadcast";
return b.input;
}
else if(h==3)
{
cout<<"**VCR is on**"<<endl;
b.input="VCR";
return b.input;
}
else
{
cout<<"Invalid input!....Enter 1 ,2 or 3"<<endl;
}
}
int Display()
{
int y;
cout<<"****settings****"<<endl;
cout<<"Enter your selection:"<<endl;
cout<<"1-Volume settings:"<<endl;
cout<<"2-channel settings:"<<endl;
cout<<"3-mode settings:"<<endl;
cout<<"4-input settings:"<<endl;
cin>>y;
if(y==1)
{
getvolume();
int f;
cout<<"*press 1 for increasing volume and 2 for decreasing volume*"<<endl;
cin>>f;
if(f==1)
{
b.vol++;
cout<<"increased vol:"<<b.vol<<endl;
}
elseif(f==2)
{
b.vol--;
cout<<"decreased vol:"<<b.vol<<endl;
}
else
{
cout<<"Press 1 or 2 for increasing or decreasing volume:"<<endl;
}
}
else if(y==2)
{
getchannel();
int l;
cout<<"*press 1 for increasing channel and 2 for decreasing channel*"<<endl;
cin>>l;
if(l==1)
{
b.channel++;
cout<<"increased channel:"<<b.channel<<endl;
}
elseif(l==2)
{
b.vol--;
cout<<"decreased channel:"<<b.channel<<endl;
}
else
{
cout<<"Press 1 or 2 for increasing or decreasing channel:"<<endl;
}
cout<<"FOR RANDOM SELECTION:"<<endl;
int o;
cout<<"Enter your slected channel:"<<endl;
cin>>o;
if(o>200)
{
cout<<"Channel unavailabe !..max channels =200"<<endl;

}
else if (o==200)
{
cout<<"MAX channel limit reached!"<<endl;
}
b.channel=o;
}
else if(y==3)
{
getmode();
int g;
cout<<" press 1 for VCR control or 2 for TV control and 3 for both:"<<endl;
cin>>g;
if(g==1){
cout<<"VCR controller.. "<<endl;
b.mode="VCR controller .";
cout<<"MODE CONTROL:"<<b.mode<<endl;
}
if(g==2){
cout<<"TV controller.. "<<endl;
b.mode="TV controller .";
cout<<"MODE CONTROL:"<<b.mode<<endl;
}
else if(g==3){
cout<<"TV and VCR controller.. "<<endl;
b.mode="TV and VCR controller .";
cout<<"MODE CONTROL:"<<b.mode<<endl;
}
else
{
cout<<"Unauthorized input!"<<endl;
}

}
else if(y==4)
{
getinput();
}
else
{
cout<<"Enter 1 ,2 ,3 or 4"<<endl;
}
}
};
int main()
{
remote r;
r.getonoff();
r.Display();
}
TASK 2
CODE:-
#include<iostream>
using namespace std;
class a
{
private:
int fstvariable;
public:
friend class b;

};
class b
{
private:
int scndvariable;
public:
a r;

void getfstvariable()
{
cout<<"enter first number:";
cin>>r.fstvariable;
cout<<"First number:"<<r.fstvariable<<endl;
}

void getscndvariable()
{
cout<<"enter second number:";
cin>>scndvariable;
cout<<"Second number:"<<scndvariable<<endl;
}
void greaterthan()
{
if(r.fstvariable>scndvariable)
{
cout<<"First number is greater than second"<<endl;
}
else if(scndvariable>r.fstvariable)
{
cout<<"Second number is greater than first"<<endl;
}
else if(r.fstvariable==scndvariable)
{
cout<<"They are equal"<<endl;
}
else
{
cout<<" invalid number plz try another "<<endl;
}
}
};
int main()
{
system("color 2b");
a r;
b g;
g.getfstvariable();
g.getscndvariable();
g.greaterthan();
}

OUTPUT:-
TASK 3
CODE
#include<iostream>
using namespace std;
class _1
{
private:
int k;
friend class _2;
};
class _2
{
private:
int l;
public:
_1 i;
void getyvalue()
{
cout<<" FIRST NUMBER IS:";
cin>>i.k;
cout<<"First value:"<<i.k<<endl;
}
void lvalue()
{
cout<<" SECOND NUMBER IS:";
cin>>l;
cout<<"Second value:"<<l<<endl;
}
void swap()
{
int temp;
cout<<"swapping:"<<endl;
temp=i.k;
i.k=l;
l=temp;
cout<<"first number:"<<i.k<<endl;
cout<<"second number:"<<l<<endl;

}
};
int main()
{
system("color 2b");
_1 i;
_2 G;
G.getyvalue();
G.lvalue();
G.swap();

OUTPUT
TASK 4
CODE:-
#include<iostream>
using namespace std;
class DM
{
private:
float dmetr;
float dcmetr;
float convertdmetr;
float convertdcmetr;
public:
friend class DB;
};
class DB
{
private:
float feet;
float inch;
public:
DM u;
float getdmetr()
{
cout<<" M =";
cin>>u.dmetr;
cout<<"\t\t"<<endl;
return u.dmetr;
}
float getdcmetr()
{

cout<<" CM =";
cin>>u.dcmetr;
return u.dcmetr;
cout<<"\t\t"<<endl;
}
float feet1()
{
cout<<" feets = ";
cin>>feet;
return feet;
cout<<endl;

}
float inch1()
{
cout<<" inches =";
cin>>inch;
return inch;
cout<<endl;
}
float convertfeet()
{
cout<<"converting feet to meters:"<<endl;
float m;
m=feet;
m=m/(3.2821);
cout<<"processing ..."<<endl;
cout<<"converted:"<<m<<endl;
u.convertdmetr=m;
return u.convertdmetr;
cout<<endl;
}
float convertcentimeter()
{

cout<<" conversions are performed below::::::"<<endl;


cout<<"converting inches to centimeter"<<endl;
float cm;
cm=inch;
cm=cm*2.54;
cout<<"processing..."<<endl;
cout<<"converted:"<<cm<<endl;
u.convertdcmetr=cm;
return u.convertdcmetr;
}
float add()
{
cout<<" now we will add it as "<<endl;
cout<<"Addition: "<<endl;
float b,v;
v=u.dcmetr+u.convertdcmetr;
cout<<"sum of centimeters:"<<v<<endl;
cout<<endl;
b=u.dmetr+u.convertdmetr;
cout<<"Sum of meters:"<<b<<endl;
return v,b;

};
int main()
{
system("color 2b");
DM u;
DB g;
g.getdcmetr();
g.getdmetr();
g.feet1();
g.inch1();
g.convertcentimeter();
g.convertfeet();
g.add();
}

OUTPUT
THINK QUESTIONS
Q1) Why friendship is needed?
ANS.
It helps us in accessing the private data members of class. It enables one class to get
access of other class without inheriting it.
Q2) Inheritance vs. friends, what do you prefer? Justify?
ANS.
Inheritance and friendship are completely different . In friendship, class A and B
can touch all the private data members and functions of class C. With inheritance,
class C can touch the public and protected members of A and B.
Q3) Why do we make friend functions?
ANS
It helps us in accessing the private data members of class. It enables one class to get
access of other class without inheriting it.A friend function is declared by the class
that is granting access to other class.
Q4) 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.
Q5) 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.

You might also like