I have to implement a complete class called Rectangle with 5 methods: Constructor, Show, Area, Merge and TotalArea.
I've done Constructor, Show, Area and Merge methods, but I stuck in TotalArea function. In this function, it takes an array of rectangle as a parameter. This method returns the sum of the areas of the rectangle in the array.
and this is my source code, the first is rectangle.h, and the second is rectangle.cpp
Code:
//rectangle.h
#include <string.h>
#include <stdlib.h>
#include <cstdlib>
class Rectangle
{
public:
Rectangle(double x, double y);
~Rectangle() {};
private:
double len;
double wid;
public:
void Show(void);
int Area(void);
Rectangle Merge(Rectangle r);
void TotalArea(void);
};
Code:
//rectangle.cpp
#include <cstdlib>
#include <iostream>
#include "rectangle.h"
using namespace std;
Rectangle::Rectangle(double x, double y)
{
len=x;
wid=y;
}
void Rectangle::Show(void)
{
cout<<"Leghth: "<<len <<"Width: "<<wid<<endl;
}
int Rectangle::Area(void)
{
double area;
area=len*wid;
}
Rectangle Rectangle::Merge(Rectangle r)
{
Rectangle d1(0.0,0.0);
d1.len=len+r.len;
d1.wid=wid+r.wid;
return d1;
};
int main(int argc, char *argv[])
{
Rectangle r1(2.0, 3.0);
Rectangle r2(7.0, 5.0);
Rectangle r3(1.0, 1.0);
r1.Show();
cout <<"Area of r2: "<<r2.Area()<<endl;
r3=r1.Merge(r2);
r3.Show();
/*
Retangle allRects[2];
allRects[0]=r1;
allRects[1]=r2;
cout<<"Total Area: "<<r2.TotalArea(allRects)<<endl;
*/
system("PAUSE");
return EXIT_SUCCESS;
}