Problem description:
Write a program to calculate the area of two given rectangles, in which the object is used as the parameter when designing the member function addarea () (used to calculate the total area of the two rectangles.
Program code:
#include<iostream>using namespace std;class Rectangular{public:Rectangular(double w,double l){width = w;length = l;}double getc(){circumference = width + length;return circumference;}double adddata(Rectangular &r){return (circumference + r.getc());}private:double width;double length;double circumference;}; int main(){Rectangular r1(2.5,3.5);cout<<"Circumference of r1 ="<<r1.getc()<<endl; Rectangular r2(2,3);cout<<"Circumference of r2 ="<<r2.getc()<<endl;cout<<"Circumference of r1+r2 ="<<r1.adddata(r2)<<endl;return 0; }
Result output:
Circumference of r1 =6Circumference of r2 =5Circumference of r1+r2 =11