Can you find the bug? 'Cause we can't

This is a discussion on Can you find the bug? 'Cause we can't within the C++ Programming forums, part of the General Programming Boards category; // Begin Rect.hpp #include <iostream> class Point // holds x,y coordinates { // no constructor, use default public: void SetX(int ...

  1. #1
    left crog... back when? incognito's Avatar
    Join Date
    Oct 2001
    Posts
    1,427

    Question Can you find the bug? 'Cause we can't

    // Begin Rect.hpp

    #include <iostream>
    class Point // holds x,y coordinates
    {
    // no constructor, use default
    public:
    void SetX(int x)
    { itsX = x; }
    void SetY(int y)
    { itsY = y; }
    int GetX()const
    { return itsX;}
    int GetY()const { return itsY;}

    private:
    int itsX;
    int itsY;

    }; // end of Point class declaration


    class Rectangle
    {
    public:
    Rectangle (int top, int left, int bottom, int right);
    ~Rectangle () {}

    int GetTop()const { return itsTop; }
    int GetLeft()const { return itsLeft; }
    int GetBottom()const { return itsBottom; }
    int GetRight()const { return itsRight; }

    Point GetUpperLeft() const { return itsUpperLeft; }
    Point GetLowerLeft() const { return itsLowerLeft; }
    Point GetUpperRight() const { return itsUpperRight; }
    Point GetLowerRight() const { return itsLowerRight; }

    void SetUpperLeft(Point Location) {itsUpperLeft = Location;}
    void SetLowerLeft(Point Location) {itsLowerLeft = Location;}
    void SetUpperRight(Point Location) {itsUpperRight = Location;}
    void SetLowerRight(Point Location) {itsLowerRight = Location;}


    void SetTop(int top) { itsTop = top; }
    void SetLeft (int left) { itsLeft = left; }
    void SetBottom (int bottom) { itsBottom = bottom; }
    void SetRight (int right) { itsRight = right; }

    int GetArea() const;


    private:
    Point itsUpperLeft;
    Point itsUpperRight;
    Point itsLowerLeft;
    Point itsLowerRight;
    int itsTop;
    int itsLeft;
    int itsBottom;
    int itsRight;
    };
    // end Rect.hpp


    // Begin rect.cpp

    #include "rect.hpp"
    Rectangle::Rectangle(int top, int left, int bottom, int right)
    {
    itsTop = top;
    itsLeft = left;
    itsBottom = bottom;
    itsRight = right;

    itsUpperLeft.SetX(left);
    itsUpperLeft.SetY(top);

    itsUpperRight.SetX(right);
    itsUpperRight.SetY(top);

    itsLowerLeft.SetX(left);
    itsLowerLeft.SetY(bottom);

    itsLowerRight.SetX(right);
    itsLowerRight.SetY(bottom);


    }


    // compute area of the rectangle by finding cornerssides,
    // establish width and height and then multiply
    int Rectangle::GetArea() const
    {
    int Width = itsRight-itsLeft;
    int Height = itsTop - itsBottom;
    return (Width * Height);
    }

    using namespace std;// this should solve the endl problem
    int main()
    {
    //initialize a local Rectangle variable
    Rectangle MyRectangle (100, 20, 50, 80 );

    // this prints out itsTop, itsLeft, itsBottom and itsRight of MyRectangle.
    std::cout << "Top: " << "\n";
    std::cout << MyRectangle.GetTop() << "\n";
    std::cout << "Left: " << endl;
    std::cout << MyRectangle.GetLeft() << "\n";
    std::cout << "Bottom: " << endl;
    std::cout << MyRectangle.GetBottom() << "\n";
    std::cout << "Right: " << endl;
    std::cout << MyRectangle.GetRight() << "\n";

    // this should print out 100,20,50,80

    Point MyPoint;

    MyPoint.SetX(25);
    MyPoint.SetY(45);

    // here we set the upperleft point to 25,45
    MyRectangle.SetUpperLeft(MyPoint);


    // this prints out itsTop, itsLeft, itsBottom and itsRight of MyRectangle once again
    std::cout << "Top: " << "\n";
    std::cout << MyRectangle.GetTop() << "\n";
    std::cout << "Left: " << endl;
    std::cout << MyRectangle.GetLeft() << "\n";
    std::cout << "Bottom: " << endl;
    std::cout << MyRectangle.GetBottom() << "\n";
    std::cout << "Right: " << endl;
    std::cout << MyRectangle.GetRight() << "\n";

    /* Since we changed the upper-left point of MyRectangle using SetUpperLeft, you think that the itsTop (aka upper) and itsLeft would have changed too, eh? I mean, really... Rectangle.itsLeft is SUPPOSED to always be the same value as Rectangle.itsUpperLeft.itsX, right? */

    // However, the above code still prints out 100,20,50,80
    // instead of 45,25,50,80

    std::cout << "Upper Left X Coordinate: ";
    std::cout << MyRectangle.GetUpperLeft().GetX();
    // But yet, this still prints out what we know should be the correct setting (25)
    int x;
    std::cin>>x;
    return 0;
    }

    /*#######################################*/

    Any help would be appreciated.
    Obviously we know that this code has a bug we just can't seem to find it.
    /*######################################*/
    OUTPUT:
    Top:
    100
    Left:
    20
    Bottom:
    50
    Right:
    80
    Top:
    100//see what it says after you changed the values
    Left:
    20
    Bottom:
    50
    Right:
    80
    Upper Left X Coordinate: 25// but then look here
    Last edited by incognito; 11-29-2001 at 03:54 PM.
    There are some real morons in this world please do not become one of them, do not become a victim of moronitis. PROGRAMMING IS THE FUTURE...THE FUTURE IS NOW!!!!!!!!!

    "...The only real game I thank in the world is baseball..." --Babe Ruth

    "Life is beautiful"-Don Corleone right before he died.

    "The expert on anything was once a beginner" -Baseball poster I own.


    Left cprog on 1-3-2005. Don't know when I am coming back. Thanks to those who helped me over the years.

  2. #2
    zen
    zen is offline
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    1. Move MyPoint.SetX(25); and MyPoint.SetY(45); out of the Recatangle constructor and after the declaration of MyPoint in main(). MyPoint can't be seen in the constructor and there is no reason for it to be there.

    2. If you're going to use prefix the namespace for objects contained within it then endl should be std::endl.
    zen

  3. #3
    left crog... back when? incognito's Avatar
    Join Date
    Oct 2001
    Posts
    1,427

    Mistake

    That was just a mistake that I made I already fixed it but still get the same results.
    There are some real morons in this world please do not become one of them, do not become a victim of moronitis. PROGRAMMING IS THE FUTURE...THE FUTURE IS NOW!!!!!!!!!

    "...The only real game I thank in the world is baseball..." --Babe Ruth

    "Life is beautiful"-Don Corleone right before he died.

    "The expert on anything was once a beginner" -Baseball poster I own.


    Left cprog on 1-3-2005. Don't know when I am coming back. Thanks to those who helped me over the years.

  4. #4
    zen
    zen is offline
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    You'll have to modify the SetUpper* and SetLower* functions call the relevent Set* function if you want the individual co-ordinates to change. However, I can see no point in storing the points individually aswell as in Point structures. You should choose one or the other, otherwise whenever one gets updated you'll have to call functions to modifiy the other.

    You could scrap -

    int itsTop;
    int itsLeft;
    int itsBottom;
    int itsRight;

    and just store all the co-ordinates in -

    Point itsUpperLeft;
    Point itsUpperRight;
    Point itsLowerLeft;
    Point itsLowerRight;

    and then access all the co-ordinates through these Points.
    zen

  5. #5
    left crog... back when? incognito's Avatar
    Join Date
    Oct 2001
    Posts
    1,427
    I think that the only reason why these points are declared like this is because we need it to find area. We these points.
    There are some real morons in this world please do not become one of them, do not become a victim of moronitis. PROGRAMMING IS THE FUTURE...THE FUTURE IS NOW!!!!!!!!!

    "...The only real game I thank in the world is baseball..." --Babe Ruth

    "Life is beautiful"-Don Corleone right before he died.

    "The expert on anything was once a beginner" -Baseball poster I own.


    Left cprog on 1-3-2005. Don't know when I am coming back. Thanks to those who helped me over the years.

  6. #6
    zen
    zen is offline
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    but the points are still available, you could still have a GetTop() function that works like -

    int GetTop()const { return itsUpperLeft.GetY(); }

    and do something similar for the other Get and Set functions.
    zen

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. ATL bug of CComPtr?
    By George2 in forum Windows Programming
    Replies: 6
    Last Post: 04-07-2008, 08:52 AM
  2. Can't Find Bug in basic MP3 Sorter
    By trickeastcs in forum C++ Programming
    Replies: 12
    Last Post: 12-14-2007, 05:31 PM
  3. Replies: 5
    Last Post: 04-16-2004, 02:29 AM
  4. Time for another round of: FIND THAT BUG!
    By DeepBlackMagic in forum C++ Programming
    Replies: 0
    Last Post: 10-29-2003, 05:29 AM
  5. linked list recursive function spaghetti
    By ... in forum C++ Programming
    Replies: 4
    Last Post: 09-02-2003, 03:53 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21