Thread: Rectangle class

Threaded View

Previous Post Previous Post   Next Post Next Post
  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.

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