Thread: Rectangle class

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    5

    Question Rectangle class

    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;
    }
    Last edited by blackant; 04-23-2009 at 07:54 AM.

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    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.
    It can't take an array alone since you won't know how large the array is (you just get the pointer to the first item in the array). Therefore you must also pass the count of items in the array (or another pointer showing the end of the range).

    Code:
    double TotalArea(const Rectangle* rect_array, int count) const;
    It is strange design, though: why would one need a Rectangle instance to be able to sum the areas of a bunch of completely unrelated Rectangles? A free function would be just fine.

    You could post what you have tried, though. Also note that the current Area function is buggy, and the result should be double, if the rectangle works with doubles.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Inherite nonvirtual class functionality
    By DrSnuggles in forum C++ Programming
    Replies: 2
    Last Post: 04-30-2009, 01:52 PM
  3. class composition constructor question...
    By andrea72 in forum C++ Programming
    Replies: 3
    Last Post: 04-03-2008, 05:11 PM
  4. deriving classes
    By l2u in forum C++ Programming
    Replies: 12
    Last Post: 01-15-2007, 05:01 PM
  5. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM