Zaarm TechZaarm Tech

C++: To find the area of a rectangle using friend function

How to find the area of a rectangle using friend function in c++?

1 Answer

<pre>#include&lt;iostream.h&gt; #include&lt;conio.h&gt; class Area_Rectangle { int width, height; public: void set_values (int,int); int area() { return (width*height); } friend Area_Rectangle duplicate (Area_Rectangle); }; void Area_Rectangle::set_values (int a, int b) { width = a; height = b; } Area_Rectangle duplicate (Area_Rectangle r) { Area_Rectangle rec; rec.width = r.width*2; rec.height = r.height*2; return (rec); } void main() { Area_Rectangle rect, rectb; int w, h; clrscr(); cout&lt;&lt;"Enter values for width and height"&lt;&lt;endl; cout&lt;&lt;"Width: "; cin&gt;&gt;w; cout&lt;&lt;"Height: "; cin&gt;&gt;h; rect.set_values (w,h); rectb = duplicate (rect); cout&lt;&lt;endl&lt;&lt;"Area of rectangle: "&lt;&lt;rectb.area(); getch(); }</pre>

Zaarm Tech

Post an answer