Oop Friend Function

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 17

Q1

<----------------------------------------------------------->
//Q.Assume the following class
/* class charactor { char ch; }
write a friend function greatest that have two parameter of
charactor type and return charactor with greatest ASCII code;
*/
#include<iostream>
using namespace std;
class charactor
{
char ch;
public:
void set_ch()
{
cout<<"\nenter ch ";
cin>>ch;
}
friend charactor greatest(charactor c1,charactor c2);
void print()
{
cout<<"\nThe greatest ASCII "<<ch;
//cout<<"\nASCII in Decimal "<<int(ch);
}
};
charactor greatest(charactor c1,charactor c2)
{
if(c1.ch>c2.ch)
return c1;
else
return c2;
}
main()
{
charactor a,b,c;
a.set_ch();
b.set_ch();
c=greatest(a,b);
c.print();
return 0;
}
Q2
<------------------------------------------------------------>
//Q2.
/*class volume{int h,l,w,v;
int compute_vol() public:
return v; } {v=h*l*w;
friend void max_vol(volume v1,volume v2);
};
void max_vol(volume v1,volume v2)
{
if(v1.v()>v2.v)
cout<<v1.h<<v1.l<<v1.w<<v1.v;
else
cout<<v2.h<<v2.l<<v2.w<<v2.v;
}
Rewrtie max_vol function as non friend function */
#include<iostream>
using namespace std;
class volume
{
int h,l,w,v;
public:
int compute_vol()
{
v=h*l*w;
return v;
}
void set_read()
{
cout<<"\nenter the h & l & w \n";
cin>>h>>l>>w;
}
int get_h()
{return h;}
int get_l()
{return l;}
int get_w()
{return w;}
};
void max_vol(volume v1,volume v2)
{
if(v1.compute_vol()>v2.compute_vol())

cout<<v1.get_h()<<v1.get_l()<<v1.get_w()<<v1.compute_vol();
else

cout<<v2.get_h()<<v2.get_l()<<v2.get_w()<<v2.compute_vol();

}
main()
{
volume a,b;
a.set_read();
b.set_read();
max_vol(a,b);
return 0;
}

Q3
<------------------------------------------------------------>
//Q3.Assume the following segment
/* class computer {char type[10];int model_no,price;
public: void print_info()
{ cout<<type; cout<<price; }
};
1- Add max price as afriend function that have two parameters
of computer
type and return computer with maximum price
2- Add a class Lenovo that have an integer value
model_no,speed,price,
as a private data member,the following function will giving
the value to
void read_val()
{ cin>>model_no; cin>>speed; }
Add check function as a friend function to the class(s) this
function have two parameter c1 as computer type and L1 as
Lenovo type check if model_no in object c1 and model_no in
object L1 is equal it will make price of L1 equal to price of
c1,change what you need in computer class. */
#include<iostream>
using namespace std;
class lenovo;
class computer
{
private:char type[10];int model_no,price;
public:
void read_com()
{
cout<<"\nenter the model no & price \n";
cin>>model_no>>price;
}

friend computer max_price(computer c1,computer c2);


void print_info()
{
cout<<"\nmodel no "<<model_no;
cout<<"\nprice "<<price;
}
friend void check(computer c1,lenovo l1);
};
class lenovo
{
private:int model_no,price,speed;
public:
void read_val()
{
cout<<"\nenter model_no & price & speed \n";
cin>>model_no>>price>>speed;
}
friend void check(computer c1,lenovo l1);
};
computer max_price(computer c1,computer c2)
{
if (c1.price>c2.price)
return c1;
else
return c2;
}
void check(computer c1,lenovo l1)
{
if(c1.model_no==l1.model_no)
{
cout<<"\nequal ";
c1.price=l1.price;
cout<<"\n new price "<<c1.price;
}
}
main()
{
computer a,b,c;
lenovo x;
a.read_com();
b.read_com();
c=max_price(a,b);
c.print_info();
x.read_val();
check(c,x);
return 0;
}
Q4
<------------------------------------------------------------>
//Q.
/* class distance { int meter,cm;
int check(int m)
{ if(meter*100+cm>m) return 1;
else return 0; }
friend check_dist(int,distance); };
check_dist(int a,distance b)
{ if(d.check(a)==1) cout<<"greater"; }
Rewrite function check_dist as non-friend function. */
#include<iostream>
using namespace std;
class distanc
{
int meter,cm;
int check(int m)
{
if(meter*100+cm>m)
return 1;
else
return 0;
}
public:
int get_check(int s)
{
return check(s);
}
};
void check_dist(int a,distanc d)
{
if(d.get_check(a)==1)
{
cout<<"\nis greater ";
}
}
main()
{
distanc d1;
check_dist(4,d1);
return 0;
}
Q5
<----------------------------------------------------------->
//Q.Consider the following classes definitions:
/* class circle;
class square {int side;
public:square create(circle); };
class circle {int r;
public: friend square::create(circle); };
square square::create(circle c)
{square s; s.side=c.r+side; return s;}
Rewrite create function as a member in class circle and
a friend to class square. */
#include<iostream>
using namespace std;
class square;
class circle
{
int r;
public:
void set_c()
{
cout<<"\nenter r ";
cin>>r;
}
square create(square);
};
class square
{
int side;
public:
void set_s()
{
cout<<"\nenter side ";
cin>>side;
}
void print ()
{
cout<<"\n side "<<side;
}
friend square circle::create(square s);
};

square circle::create(square s)
{
s.side=r+s.side;
return s;
}
main()
{
square s1,s2;
circle c1;
s1.set_s();
c1.set_c();
s2=c1.create(s1);
s2.print();
return 0;
}

Q6
<---------------------------------------------------------->
//Q.Assume the following program segment:
/* class first;
class second {int num;
public second() {num=3;}
first move(first a); };
class first {int num;
public:first(){num=6;}
first(int i){num=i;}
void print() {cout<<num;}
friend first second::move(); };
first second::move(first a)
{ first b; b.num=num+a.num; b.print();
return b; }
1. Descirbe how to call function move.
(Add the required objects declarationns)
2. Determine the output resultant of the function call. */
#include<iostream>
using namespace std;
class first;
class second
{
int num;
public:
second()
{
num=3;
}
first move(first a);
};
class first
{
int num;
public:
first()
{
num=6;
}
first(int i)
{
num=i;
}
void print()
{
cout<<"\n num is "<<num;
}
friend first second::move(first a);
};
first second ::move(first a)
{
first b;
b.num=num+a.num;
b.print();
return b;
}
int main()
{
first f1,f2;
f1.print();//num is 6
first(20).print();//declaration any value ex 20
second s1;
f2=s1.move(f1);// 3 + 6 =9
return 0;
}
Q7
<----------------------------------------------------------->
//Q.
/* class subject2;
class subject1 {int mark,id;
int compare(subject2); };
class subject2{int mark,id;
friend int subject1::compare(subject2); };
int subject1::compare(subject2 s2)
{ if(mark>s2.mark)return 1;
else return 0; }
Rewrite function compare as non-friend function. */
#include<iostream>
using namespace std;
class subject2;
class subject1
{
int mark,id;
public:
int compare(subject2);
};
class subject2
{
int mark,id;
public:
int get_mark()
{
return mark;
}
};
int subject1::compare(subject2 s2)
{
if(mark>s2.get_mark())
return 1;
else
return 0;
}
main()
{
subject1 a;
subject2 b;
a.compare(b);
return 0;
}
Q8
<----------------------------------------------------------->
//Q.Assume the following class definition
/* class distance { int meter,cent;
public:void setdate() { cin>>meter>>cent; };
Write a friend function create_large that returns
an object of type distance which contains the
large two distance objects. */
#include<iostream>
using namespace std;
class distanc
{
private:int meter,cent;
public:void setdata()
{
cout<<"\nenter meter & cent \n";
cin>>meter>>cent;
}
friend distanc create_large(distanc d1,distanc d2);
void print()
{
cout<<"\nmeter "<<meter<<"\ncent "<<cent;
cout<<"\n\n( meter * 100 + cent ) : "<<meter*100+cent;
}
};
distanc create_large(distanc d1,distanc d2)
{
if (d1.meter*100+d1.cent>d2.meter*100+d2.cent)
return d1;
else
return d2;
}
main()
{
distanc a1,a2,a3;
a1.setdata();
a2.setdata();
a3=create_large(a1,a2);
a3.print();
return 0;
}
Q9
<----------------------------------------------------------->
//Q.Assume the following program segment
/* class square;
class rectangle {int length,width;
public:
square move();
};
class square { int side;
public:
friend square rectangle::move(); };
square rectangle::move() {square s;
if(length<=width)
s.side=length;
else s.side=width;
return s; }
1. Describe how to call function move.
(Add the required objects declarations)
2. change the move function to be a member
of class square and a friend to class rectangle */
#include<iostream>
using namespace std;
class rectangle;
class square
{
int side;
public:
void print()
{
cout<<"\n side "<<side;
}
void move();
};
class rectangle
{
int length,width;
public:
rectangle()
{
cout<<"\nenter the L &W ";
cin>>length>>width;
}
friend void square::move();
};
void square::move()
{
rectangle r;
if(r.length<=r.width)
side=r.length;
else
side=r.width;
}
main()
{
square s1;
s1.move();
s1.print();
return 0;
}
Q10
<---------------------------------------------------------->
//Q.Assume the following class
/* class mathematics { int no; }
Write a friend function multiply that return an int value
and have two parameters of type mathematics to compute
the result of multiply of two no of the objects. */
#include<iostream>
using namespace std;
class mathematics
{
private: int no;
public:
friend int multiply(mathematics m1,mathematics m2);
mathematics()
{
cout<<"\nenter the no : ";
cin>>no;
}
};
int multiply(mathematics m1,mathematics m2)
{
return (m1.no*m2.no);
}
main()
{
mathematics a,b;
cout<<multiply(a,b);
}
Q11
<----------------------------------------------------------->
//Q.
/* class distance {int meter,cm;
int conv_cm(int m){return m*100;}
friend distance sum_dist(distance d1,distance d2); };
distance sum_dist(distance d1,distance d2)
{distance d3;
d3.cm=d1.cm+d2.cm;
d3.meter=d1.meter+d2.meter;
return d3; }
Rewrite sum_dist fuction as non-friend function. */
#include<iostream>
using namespace std;
class distanc
{
private:int meter,cm;
public:
int conv_cm(int m)
{
return m*100;
}
int get_m()
{
return meter;
}
int get_cm()
{
return cm;
}
void set_cm(int a)
{
cm=a;
}
void set_meter(int a)
{
meter=a;
}
};
distanc sum_dist(distanc d1,distanc d2)
{
distanc d3;int c,m;
c=d1.get_cm()+d2.get_cm();
m=d1.get_m()+d2.get_m();
d3.set_cm(c);
d3.set_meter(m);
return d3;
}
main()
{
distanc a,b,c;
a.set_meter(3);
a.set_cm(40);
b.set_meter(2);
b.set_cm(30);
c=sum_dist(a,b);
cout<<"\nmeter "<<c.get_m();
cout<<"\ncm "<<c.get_cm();
return 0;
}
Q12
<--------------------------------------------------------->
//Q.Assume the following segment
/* class circle {int r;float area;
void print_info(){area=r*r*3.14;
cout<<r; cout<<area; } };
1- Add new_circle as a friend function that have two
parameters of circle type and return a new circle which is
summation of circle 1 and circle 2
2- Add a class square that have an integer value side as a
private data member.the following function will giving the
value to
void give_val(circle c1,square s1)
{ s1.side=c1.r; }
Add this function as a friend function to the class(s)as
you think and change what you need in circle class. */
#include<iostream>
using namespace std;
class square;
class circle
{
private:int r;float area;
public:
void read_cir()
{
cout<<"\nenter r ";
cin>>r;
}
void print_info()
{
area=(r*r*3.14);
cout<<"\nr : "<<r;
cout<<"\narea is "<<area;
}
friend circle summation(circle c1,circle c2);
void set_r(int a)
{ r=a; }
friend void give_val(circle c1,square s1);
};
class square
{
private:int side;
public:
friend void give_val(circle c1,square s1);
};
circle summation(circle c1,circle c2)
{
circle c3;int s=0;
s=c1.r+c2.r;
c3.set_r(s);
return c3;
}
void give_val(circle c1,square s1)
{
s1.side=c1.r;
cout<<"\nside : "<<s1.side;
}
main()
{
circle a,b,c;
a.read_cir();
b.read_cir();
c=summation(a,b);
c.print_info(); //print new circle
square s;
give_val(c,s);
return 0;
}

You might also like