How to find the area of a rectangle using friend function in c++?
Zaarm Tech Changed status to publish November 20, 2021
#include<iostream.h> #include<conio.h> 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<<"Enter values for width and height"<<endl; cout<<"Width: "; cin>>w; cout<<"Height: "; cin>>h; rect.set_values (w,h); rectb = duplicate (rect); cout<<endl<<"Area of rectangle: "<<rectb.area(); getch(); }
Zaarm Tech Changed status to publish November 20, 2021