// 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