boyracer38
Dec 20th, 2001, 03:23 PM
I have this assignment to do:: can u help??
Tutorial Exercise
Set up a class called Point which contains two Members x and y which represent a point on the Screen. Write functions to return the values associated with x and y.
Now write a class called Rectangle which contains two Points. The Points represent the top left screen position and the bottom Right screen position for the rectangle.
Write functions to return the values associated with the points In the rectangle. You will obviously need to provide suitable Constructors. Experiment with the pointer and non pointer members solutions
Here is the code for the Rectangle class
Class Rectangle
{
Point* p1;
Point* p2;
public:
Rectangle(int x, int y, int p, int q);
Point* getp1();
Point* getp2();
}
any help would be appreciated
thanx
Jason
:confused: :confused: :confused:
kedaman
Dec 20th, 2001, 04:20 PM
Does the rectangle own it's pointers? For there to be any use of pointers it has to be dynamical which rectangle obviously isn't
CornedBee
Dec 22nd, 2001, 02:11 PM
// strict data hiding and encapsulation - does not make sense for such a simple class
class SenselessPoint
{
// data (private)
int m_x;
int m_y;
public:
SenselessPoint() {m_x = 0; m_y = 0; };
SenselessPoint(int x, int y) {m_x = x; m_y = y; };
int GetX() {return m_x; };
int GetY() {return m_y; };
void SetX(int x) {m_x = x; };
void SetY(int y) {m_y = y; };
// define operators etc, e.g. operator +=
SenselessPoint operator +=(SenselessPoint& p) {m_x += p.GetX(); m_y += p.GetY(); };
};
// this version does make more sense
class Point
{
public:
int x;
int y;
Point() {x = y = 0; };
Point(int _x, int _y) { x = _x; y = _y; };
// operators are still useful
Point operator +=(const Point& p) {x += p.x; y += p.x; };
};
// only a rectangle that makes sense:
class Rect
{
public:
// data
Point ptUpLeft;
Point ptBotRight;
// constructors
Rect() {}; // rely on the Point constructor to set it's members to 0, this is absolutely ok in OOP
Rect(const Point& ptUL, const Point& ptBR) : ptUpLeft(ptUL), ptBotRight(ptBR) // constructor list: directly call constructors of contained objects.
// although you don't see it, the compiler automatically defines a constructor taking a Point& argument.
{};
Rect(int x1, int y1, int x2, int y2) : ptUpLeft(x1, y1), ptBotRight(x2, y2)
{};
};
For your assignement you would pobably use the SenselessPoint class and a SenselessRectangle class made by the same scheme.
The thing with the pointers: I don't see any use for it, but here is a complete Rectange class that uses pointers:
class PtrRect
{
// Since pointers are dangerous, it is a good idea to make them private
Point *pptUL, *pptBR;
// makes the constructors somewhat more complicated
// I'll define only the standard constructor, the others are
// easy then.
public:
PtrRect(); // too complicated for inlining
// we also need a destructor to ensure the space is freed.
~PtrRect();
// and secured data access:
const Point& GetUL() { return *pptUL; };
// etc.
}
PtrRect::PtrRect()
{
pptUL = new Point(0,0);
if(pptUL == NULL)
{ // memory allocation failed: do something!
}
pptBR = new Point(0,0);
if(pptBR == NULL)
{ // maybe throw an exception...
}
}
PtrRect::~PtrRect()
{
delete pptUL;
delete pptBR;
}
You could also always check for valid pointers, but it shouldn't be necessary.
All in all this solution does not make any sense, but it is a good exercise.