Ask question, find answer on any topic in real time from people around the world. Have a question ? Ask now. Know an Answer share your Knowledge to world.PrepJunk is the online community that has Junk of answers!
UNKNOWN
1
UNITED STATES
1
2
online

What is a ‘pure virtual’ member function?

0

What is a ‘pure virtual’ member function?

asked Apr 7, 2012 in C++ by mohan (541 points)
    

1 Answer

0

Some member functions exist in concept, but can’t have any actual defn. Ex: Suppose I asked you to draw a Shape at location (x,y) that has size 7.2. You’d ask me ‘what kind of shape should I draw’, since circles, squares, hexagons, etc, are drawn differently. In C++, we indicate the existence of the ‘draw()’ method, but we recognize it can only be defined in subclasses:

class Shape {
public:
virtual void draw() const = 0;
//... ^^^--- ‘=0’ means it is ‘pure virtual’
};

This pure virtual makes ‘Shape’ an ABC. The ‘const’ says that invoking the
‘draw()’ method won’t change the Shape object (ie: it won’t move around on the
screen, change sizes, etc). If you want, you can think of it as if the code
were at the nil pointer.

Pure virtuals allow you to express the idea that any actual object created from a [concrete] class derived from the ABC will have the indicated member fn, but we simply don’t have enough information to actually define it yet. They allow separation of interface from implementation, which ultimately allows functionally equivalent subclasses to be produced that can ‘compete’ in a free market sense (a technical version of ‘market driven economics’).

answered Apr 7, 2012 by mohan (541 points)