Program:
#include <iostream>
using namespace std;
class Rectangle {
float len;
float bre;
public:
Rectangle() {cout << "Rectangle constructed!" << endl; len=0; bre=0;}
~Rectangle() {cout << "Rectangle destructed!" << endl;}
void setL(float L) {len=L;}
void setB(float B) {bre=B;}
friend float area(Rectangle rect);
};
float area(Rectangle rect) {
return rect.len*rect.bre;
}
int main(void) {
float _L, _B;
Rectangle obj;
cout << "Enter the length and breadth of the rectangle: ";
cin >> _L >> _B;
obj.setL(_L);
obj.setB(_B);
cout << "The area of the Rectangle = " << area(obj) << "." << endl;
return 0;
}
Output:
Rectangle constructed!
Enter the length and breadth of the rectangle: 1 3
The area of the Rectangle = 3.
Rectangle destructed!
Rectangle destructed!
Why is "Rectangle destructed!" printed twice?