Read OnLine Cpp Program Example



12 FRIEND
PRAC 1 PRAC 2

// friend example
// friend functions
#include<iostream.h>
#include<conio.h>
class crectangle
{
int width,height;
public:
void setvalues (int,int);
int area()
{
return(width*height);
}
friend crectangle duplicate (crectangle);
};
void crectangle::setvalues (int a,int b)
{
width=a;
height=b;
}
crectangle duplicate (crectangle rectparam)
{
crectangle rectres;
rectres.width=rectparam.width*2;
rectres.height=rectparam.height*2;
return (rectres);
}
void main()
{
clrscr();
crectangle rect,rectb;
rect.setvalues(2,3);
rectb=duplicate(rect);
cout<<rectb.area();
getch();
}
----OUTPUT----
24

// friend example
// friend class
#include<iostream.h>
#include<conio.h>
class csquare;
class crectangle
{
int width,height;
public:
int area()
{
return(width*height);
}
void convert (csquare a);
};
class csquare
{
private:
int side;
public:
void set_side (int a)
{
side=a;
}
friend class crectangle;
};
void crectangle::convert (csquare a)
{
width=a.side;
height=a.side;
}
void main()
{
clrscr();
csquare sqr;
crectangle rect;
sqr.set_side (4);
rect.convert(sqr);
cout<<rect.area();
getch();
}
----OUTPUT----
16

Previous Next
Download Download