Thread: Class rectangle/need help thanks!!!

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    10

    Question Class rectangle/need help thanks!!!

    I need to create a rectangle class. The class stores coordinates of the four corners of a rectangle. The constructor calls a set function that accepts four sets of coordinates and verifies that each one is in the first quandrant with no single x or y coordinate larger than 20.0. Any idea of how I should go aabout this? I stuck on the accepting the coordinates part.

  2. #2
    Registered User
    Join Date
    Aug 2001
    Posts
    154
    Unless I misunderstand, in the constructor just call the set function and pass it the co-ordinates, e.g., setcoord(10, 11, 8, 9,...);
    Normally when you create an object of the rectangle class, you'd pass it the co-ordinates, but the constructor should provide defaults in case none are provided. The set function can validate that the co-ordinates are correct, or the constructor can do that first.

  3. #3
    Registered User
    Join Date
    Nov 2001
    Posts
    10
    I don't have to worry about the coordinates being something like
    (2,1) thats how I remember coordinates. Would single digit coordinates be ok for this?

  4. #4
    Registered User Strider's Avatar
    Join Date
    Aug 2001
    Posts
    149
    the constructor should provide defaults in case none are provided
    Actually, if this is attempted, the compiler will not know if you are attempting to use the default constructor or the parameterized constructor.
    Code:
    // EXAMPLE
    #ifndef RECTANGLE_H
    #define RECTANGLE_H
    
    class Rectangle
    {
    public:
        Rectangle();  // default constructor
    
        // parameterized
        // must require at least one value to be entered
        Rectangle(int x1, int y1, int x2, int y2);
    
        /* this would not work:
        Rectangle(int x1 = 1, int y1 = 1, int x2 = 1, int y2 = 1);
        */
        virtual ~Rectangle();  // destructor
    
        // accessor functions
        int GetTop() { return Top; }
        int GetBottom() { return Bottom; }
        int GetLeft() { return Left; }
        int GetRight() { return Right; }
    
        // mutator functions
        void voSetTop(int value = 1) {Top = value; }
        void voSetBottom(int value = 1) {Bottom = value; }
        void voSetLeft(int value = 1) {Left = value; }
        void voSetRight(int value = 1) {Right = value; }
    
    private:
        int Top,
            Bottom,
            Left,
            Right;
    }
    
    #endif  // RECTANGLE_H
    You will probably want to create a specialized function, or functions, for checking the values entered (value > 0). This should give you a better idea though.

    **edit - of course I realize you are looking for the coordinates, nvoigt posted a good example of this. I was mostly touching on the constructor issue. Sorry for the confusion.

    David
    Last edited by Strider; 11-15-2001 at 10:57 AM.
    One Ring to rule them all, One Ring to find them,
    One Ring to bring them all and in the darkness bind them
    In the Land of Mordor where the Shadows lie.

  5. #5
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    Try this...

    PHP Code:
    struct Coordinate
    {
        
    Coordinateint _x 0int _y )
        {
            
    _x;
            
    _y;
        }

        
    int x;
        
    int y;
    };

    class 
    Rectangle
    {
        public:

            
    RectangleCoordinate aCoordinate bCoordinate cCoordinate d )
            {
                
    m_bValid Seta,b,c,);
            }

            
    bool SetCoordinate aCoordinate bCoordinate cCoordinate d )
            {
                
    // check a
                // check b
                // check c
                // check d
                // if one check failed, return false;

                
    m_xyCorner[0] = a;
                
    m_xyCorner[1] = b;
                
    m_xyCorner[2] = c;
                
    m_xyCorner[3] = d;

                return 
    true;
            }

            
    bool IsValid()
            {
                return 
    m_bValid;
            }
        private:
            
    bool       m_bValid;     
            
    Coordinate m_xyCorner[4];
    };

    int main()
    {
        
    Rectangle rect(  Coordinate1,), Coordinate2,), Coordinate1,), Coordinate2,) );

        return 
    0;

    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  6. #6
    Registered User
    Join Date
    Aug 2001
    Posts
    154
    Originally posted by Strider

    Actually, if this is attempted, the compiler will not know if you are attempting to use the default constructor or the parameterized constructor.
    Code:
    // EXAMPLE
    #ifndef RECTANGLE_H
    #define RECTANGLE_H
    
    class Rectangle
    {
    public:
        Rectangle();  // default constructor
    
        // parameterized
        // must require at least one value to be entered
        Rectangle(int x1, int y1, int x2, int y2);
    
        /* this would not work:
        Rectangle(int x1 = 1, int y1 = 1, int x2 = 1, int y2 = 1);
        */
    I was mostly touching on the constructor issue. Sorry for the confusion.

    David
    Well, I guess I was unclear, either that or I'm all mixed up. What I meant was default values are provided in the class constructor declaration. These are passed into the constructor definition if none are provided when an object of the class is created. An example from Deitel:
    Code:
    // Header file with class declaration time.h
    class Time{  
    public:
      Time(int = 0; int = 0, int = 0);
    .
    .
    .
    };
    
    // Implementation file time.cpp
    #include "time.h"
    Time::Time(int hr, int min, int sec)
      {setTime(hr, min, sec);}
    .
    .
    .
    This example provides default initialization. The validity check is done in another function in this example, but could be done in the constructor itself.
    If this is wrong, please tell me, otherwise sorry if I wasn't clear.

  7. #7
    Registered User Strider's Avatar
    Join Date
    Aug 2001
    Posts
    149
    Yes. Sorry, I have not been thinking very clearly lately. It will work. I always tend to create a default without any parameters and build from there and I guess I seemed to be stuck in that train of thought. Disregard my previous post.
    Rule number three.....there is NO rule number three!
    David
    One Ring to rule them all, One Ring to find them,
    One Ring to bring them all and in the darkness bind them
    In the Land of Mordor where the Shadows lie.

  8. #8
    Registered User
    Join Date
    Nov 2001
    Posts
    10

    Cool Rectangle

    Ok,
    I will try to build from your info.
    But does this only satisfy for two positions my class has to store all 4 positions.

    // must require at least one value to be entered
    Rectangle(int x1, int y1, int x2, int y2);





  9. #9
    Registered User
    Join Date
    Nov 2001
    Posts
    10

    Unhappy

    what I mean is don't i need something like

    Coordinate one----- ( x, y)
    coordinate two------(x, y)
    coordinate three------(x, y)
    coordinate four------(x, y)

    I can't see it being a single digit like

    Coordinate one----- 20
    coordinate two------12
    coordinate three----5
    coordinate four------16

    this coordinate thing is driving me bonkers

  10. #10
    mov.w #$1337,D0 Jeremy G's Avatar
    Join Date
    Nov 2001
    Posts
    704

    This?

    class myRectangle
    {
    public:

    // accessor functions

    int GetTL_x() {return TL_x; }
    int GetTL_y() {return TL_y; }
    int GetTR_x() {return TR_x; }
    int GetTR_y() {return TL_y; }
    int GetBL_x() {return BL_x; }
    int GetBL_y() {return BL_y; }
    int GetBR_x() {return BR_x; }
    int GetBR_y() {return BR_y; }

    // mutator functions
    void SetTL_x_y(int x = 1, int y = 1) {TL_x = x; TL_y = y;}
    void SetTR_x_y(int x = 1, int y = 1) {TR_x = x; TR_y = y;}
    void SetBL_x_y(int x = 1, int y = 1) {BL_x = x; BL_y = y;}
    void SetBR_x_y(int x = 1, int y = 1) {BR_x = x; BR_y = y;}

    private:
    int TL_x; // top left x
    int TL_y; // top left y
    int TR_x; // top right x
    int TR_y; // top right y
    int BL_x; // bottom left x
    int BL_y; // bottom left y
    int BR_x; // bottom right x
    int BR_y; // bottom right y

    };





    myRectangle Box; // rectangle class Box

    int main(void)
    {
    Box.SetTL_x_y(1,2);
    Box.SetTR_x_y(3,2);
    Box.SetBL_x_y(1,6);
    Box.SetBR_x_y(3,6);

    cout << Box.GetTL_x() << endl;
    cout << Box.GetTL_y() << endl;
    cout << Box.GetTR_x() << endl;
    cout << Box.GetTR_y() << endl;
    cout << Box.GetBL_x() << endl;
    cout << Box.GetBL_y() << endl;
    cout << Box.GetBR_x() << endl;
    cout << Box.GetBR_y() << endl;
    return 0;
    }
    c++->visualc++->directx->opengl->c++;
    (it should be realized my posts are all in a light hearted manner. And should not be taken offense to.)

  11. #11
    Registered User Strider's Avatar
    Join Date
    Aug 2001
    Posts
    149
    The constructor calls a set function that accepts four sets of coordinates and verifies that each one is in the first quandrant with no single x or y coordinate larger than 20.0
    Ok. So you must have the constructor make a call to a set function for verifying and initiating the coordinates. Each coordinate must be in quadrant 1 and no greater than 20.0. From this we know that we are looking for an unsigned float or double value ( 0.0 <= x <= 20.0); float is best here as we only go to 20.0.

    The constructor will still need to read in the coordinates, grouped like in nvoight's example ( 4 structs of floats ), or ungrouped ( 8 floats ). Your choice.

    Within the constructor, the values will be passed on to a set function for validation before being initialized.

    Ok.
    Code:
    /*This code uses recursion so I am not certain if you follow 
    everything I am doing.  This can be rewritten in another fashion 
    to accomplish the same thing, this is just more efficient. */
    
    ]// using a coordinate struct based on nvoight's example but with floats
    Rectangle( Coordinate a, Coordinate b, Coordinate c, Coordinate d )
    {
         voSet_And_Validate_Coordinates( a,b,c,d );
    }
    
    
    void voSet_And_Validate_Coordinates(Coordinate xy1,
                                        Coordinate xy2,
                                        Coordinate xy3,
                                        Coordinate xy4)
    {
        Coordinate xy[4] = {xy1, xy2, xy3, xy4};
    
        for ( int i = 0; i < 4; i++)
        {
            if ((xy[i].x >= 0.0) && (xy[i].x <= 20.0))
            {
                if ((xy[i].y >= 0.0) && (xy[i].y <= 20.0))
                {
                    m_xyCorner[i] = xy[i];
                }
                else
                {
                    cout << "Coordinate " << xy[i].y << " is out of range.\n"
                         << "Enter the new value: ";
                    cin >> xy[i].y;
    
                    // recursive call
                    voSet_And_Validate_Coordinates(xy[0], xy[1], xy[2], xy[3]);
                    return;
                }
            }
            else
            {
                cout << "Coordinate " << xy[i].x << " is out of range.\n"
                     << "Enter the new value: ";
                cin >> xy[i].x;
    
                // recursive call
                voSet_And_Validate_Coordinates(xy[0], xy[1], xy[2], xy[3]);
                return;
            }
        }
        return;
    }
    The recursive calls allow for "on the fly" initialization. The function will continue to call itself until all coordinates are in the range of 0.0 to 20.0. This could probably be accomplished using another function call with a while loop to input data until the condition is met. This is more code though.

    Hope this helps some. Again sorry for my earlier post.

    David
    One Ring to rule them all, One Ring to find them,
    One Ring to bring them all and in the darkness bind them
    In the Land of Mordor where the Shadows lie.

  12. #12
    Registered User
    Join Date
    Nov 2001
    Posts
    10

    Unhappy Rectangle

    // Declaration of the Rectangle class.
    // Member functions defined

    // preprocessor directives that
    // prevent multiple inclusions of header file
    #ifndef 6_13_H
    #define 6_13_H

    class Rectangle {
    public:
    Rectangle( int = 0, int = 0, int = 0,
    int = 0, int = 0, int = 0,
    int = 0, int = 0 ); // constructor


    setRectangle( int, int, int, int, int, int, int, int ); // SET COORDINATES

    private:

    int X1;
    int Y1;
    int X2;
    int Y2;
    int X3;
    int Y3;
    int X4;
    int Y4;

    };

    #endif





    #include <iostream>
    using std::cin;
    using std::cout;
    using std::endl;
    #include "6_13.h"






    //
    Rectangle::Rectangle(int X1, int Y1, int X2, int Y2,
    int X3, int Y3, int X4, int Y4 ){
    setRectangle(X1, Y1, X2, Y2, X3, Y3, X4, Y4);}





    //Are we in the range of 0 to 20 with no values signed
    Rectangle::setRectangle(int X1, int Y1, int X2, int Y2,
    int X3, int Y3, int X4, int Y4 ){

    int Pos[8]={X1, Y1, X2, Y2, X3, Y3, X4, Y4};


    for (int i=0; i<=7; ++i){
    if (Pos[i]<0){
    cout<<Pos[i]<<" is not in our range\n"
    <<"input a value from 0 to 20 only"<<endl;
    cin>>Pos[i];}


    if (Pos[i]>20 ){
    cout<<Pos[i]<<" is not in our range\n"
    <<"input a value from 0 to 20 only"<<endl;
    cin>>Pos[i];}


    }

    //Do the values specify a rectangle?
    //not sure if this section checks the values correctly.
    if ((Pos[0] = Pos[2])&&
    (Pos[4] = Pos[6])&&
    (Pos[1] = Pos[3])&&
    (Pos[5] = Pos[7]))

    cout<<"This is a Rectangle!;-)"<<endl;
    }


    Guys this is as far as I have been able to get.
    still have to set up member functions for perimeter, area, length
    and width. Not to mention a predicate function SQUARE to see if the rectangle is a square.

    My set function for some reason doen't like zero's

    I took this route because I think it will be easier to make calculations with my other member functions I have not made yet.
    but I felt that I could'nt go any further untill I figure out my range issue.

    thanks for all of your help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Class design problem
    By h3ro in forum C++ Programming
    Replies: 10
    Last Post: 12-19-2008, 09:10 AM
  2. Specializing class
    By Elysia in forum C++ Programming
    Replies: 6
    Last Post: 09-28-2008, 04:30 AM
  3. matrix class
    By shuo in forum C++ Programming
    Replies: 2
    Last Post: 07-13-2007, 01:03 AM
  4. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM