Thread: Need help on polymorphism

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    11

    Question Need help on polymorphism

    The project is to develop a basic graphics package. The following step is listed in my project:

    Write a polymorphic screen manager that walks through the array (preferably using an integrator) sending draw messages to each object in the array to form a screen image.

    Can anyone help me to understand what exactly that means?

    Thanks in advance

    Tony

  2. #2
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    "(preferably using an integrator)"

    I think you meant iterator.

    Basically, they want the ScreenManager class to store a bunch of drawable objects generically, not caring what specifically will be drawn to the screen, only caring that each shape can draw itself. In doing so, the only time the ScreenManager has to worry about each individual type of shape is when they are created... not in using them.

    A basic, incomplete version of what is wanted follows.

    Code:
    class Shape {
    public:
       virtual void draw()=0;
       virtual ~Shape() { }
    };
    
    class Triangle : public Shape {
    public:
       void draw(); // draws a triangle
    private:
    // whatever is needed
    };
    
    class Rectangle : public Shape {
    public:
       void draw(); // draws a square
    private:
    // whatever
    };
    
    class ScreenManager {
    public:
        ScreenManager();  // randomly fills shapeVec with Rects or Triangles
        ~ScreenManager();  // deletes stuff in shapeVec
        void drawScene();  // iterates through shapeVec, drawing each shape to screen
    private:
        std::vector<Shape*> shapeVec; // or whatever container you are comfortable with
    };
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    11

    repost

    I should have posted the whole description of my project in the first place. I've been working on this thing for almost four hours now and I have gotten nowhere!

    Develop a basic graphics package. Use the Shape class inheritance hierarchy from Chapter 9. Limit yourself to two-dimensional shapes such as squares, rectangles, triangles and circles. Interact with the user. Let the user specify many items of the same shape. As you create each shape, place a Shape * pointer to each new Shape object into an array. Each class has its own draw member function. Write a polymorphic screen manager that walks through the array (preferably using an integrator) sending draw messages to each object in the array to form a screen image. Redraw the screen image each time the user specifies an additional shape.

    The reply that I received initially says to create a class screenmanager. With the rest of the project description, does that still apply?

    Tony

  4. #4
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    "As you create each shape, place a Shape * pointer to each new Shape object into an array."

    The ScreenManager class basically performs that part right there.. by the assignment, it doesn't look like it's neccesary to encapsulate it into a class.

    I still don't have a class what an integrator (in this sense, not the calculus function) is though .
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Polymorphism - "pointers" or "references"?
    By Petike in forum C++ Programming
    Replies: 10
    Last Post: 06-04-2009, 05:06 PM
  2. A C++ program examples showing Polymorphism, please help.
    By MarkSquall in forum C++ Programming
    Replies: 19
    Last Post: 06-06-2008, 04:41 AM
  3. Question on polymorphism
    By 6tr6tr in forum C++ Programming
    Replies: 3
    Last Post: 05-06-2008, 09:05 AM
  4. change sorting method using polymorphism
    By Forever82 in forum C++ Programming
    Replies: 2
    Last Post: 07-31-2003, 01:21 PM
  5. Polymorphism & Overloaded Operators :: C++
    By kuphryn in forum C++ Programming
    Replies: 2
    Last Post: 09-13-2002, 08:40 PM