|
-
Dec 20th, 2001, 04:23 PM
#1
Thread Starter
Lively Member
Help !!
-
Dec 20th, 2001, 05:20 PM
#2
transcendental analytic
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
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Dec 22nd, 2001, 03:11 PM
#3
Code:
// 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:
Code:
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.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|