pure abstract classes

Hii
I found this code in a website but i didn't understand the explaination

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
class DrawableObject 
 {
  public:
    virtual void Draw(GraphicalDrawingBoard&) const = 0; //draw to GraphicalDrawingBoard
 };
 
 class Triangle : public DrawableObject
 {
 public:
   void Draw(GraphicalDrawingBoard&) const; //draw a triangle
 };
 
 class Rectangle : public DrawableObject
 {
 public:
   void Draw(GraphicalDrawingBoard&) const; //draw a rectangle
 };
 
 class Circle : public DrawableObject
 {
 public:
   void Draw(GraphicalDrawingBoard&) const; //draw a circle
 };
 
 typedef std::list<DrawableObject*> DrawableList;
 
 DrawableList drawableList;
 GraphicalDrawingBoard drawingBoard; //where GraphicalDrawingBoard is defined? why is this legal? 
 
 drawableList.pushback(new Triangle());
 drawableList.pushback(new Rectangle());
 drawableList.pushback(new Circle());
 
 for(DrawableList::const_iterator iter = drawableList.begin(), 
    endIter = drawableList.end();
    iter != endIter;
    ++iter)
 { 
   DrawableObject *object = *iter;
   object->Draw(drawingBoard);
 }


after typedef std::list<DrawableObject*> DrawableList; the use of DrawableList and drawableList is confusing me, can someone explain to me please this part of the code?

Thank you in advance
Basically, with all three of those objects- Triangle, Circle, Rectangle- they all hold the function Draw in common. In other words, when the for loop iterates through the list of objects to call that specific function, it will do so for all three. This would normally not work if the function in the parent class was not abstract.
I don't get why line 28 is legal, where GraphicalDrawingBoard is defined? (it is not a class..)
This would normally not work if the function in the parent class was not abstract.

This is not true. DrawableObject::Draw() is declared as virtual. This means that, even if DrawableObject::Draw() were defined, then the actual type of the object pointed to by a DrawableObject* will be correctly resolved at runtime, and if Draw() is overridden in the derived class, it will be called.

You don't need the base class method to be abstract, for polymorphism to work. The method just has to be virtual.
Aibsr wrote:
I don't get why line 28 is legal, where GraphicalDrawingBoard is defined? (it is not a class..)
It is not defined in this code snippet, though it is referred to on lines 4, 10, 16, 22, and 28. Maybe you're looking in the wrong place?
This is where i got the example from http://en.wikibooks.org/wiki/C%2B%2B_Programming/Classes/Abstract_Classes apparently this is just a part of the code.

MikeyBoy do you mean that in the first time the loop runs, Triangle::Draw() will be called and the second time Rectangle and so on..? because this is what i think will happen.
The function called depends on the actual runtime type of the object, so yes.
Last edited on
MikeyBoy do you mean that in the first time the loop runs, Triangle::Draw() will be called and the second time Rectangle and so on..? because this is what i think will happen.

Yes, exactly. This happens because DrawableObject::Draw() is declared as virtual.

If DrawableObject::Draw() wasn't declared as virtual, then only DrawableObject::Draw() would be called. And, in this example, that's an abstract method, so doesn't exist.
Last edited on
Thank you all. I got it:)
Topic archived. No new replies allowed.