Results 1 to 3 of 3

Thread: Help !!

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Nov 2001
    Posts
    117

    Exclamation Help !!

    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
    Code:
    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

  2. #2
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    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.

  3. #3
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    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
  •  



Click Here to Expand Forum to Full Width