TIP: Click on subject to list as thread! ANSI
echo: c_plusplus
to: ANDERS WEGGE JAKOBSEN
from: DARIN MCBRIDE
date: 1997-04-22 21:15:00
subject: Rating of C++

 AWJ> Quoting Darin McBride to Anders Wegge Jakobsen, 18 Apr 97
 AWJ> about Rating of C++:
 DM> It's not quite the same effect... if I _need_ you 
 AWJ> to write func's A, B,
 DM> and C, I can with the abstract member functions.  
 DM> I can't do the same
 DM> thing withprotected constructors.  Your method would necessitate a
 DM> default behaviour on functions where they may not be implementable. 
 DM> The famous "draw()" function of Shape comes to mind.  I don't WANT a
 DM> default for it!
 AWJ>  Sorry, I seem to have jumped to a premature conclusion. 
 AWJ>  What i *thought* you were discussing, were the kludgy-
 AWJ> ness of defining a pure virtual class, or what the 
 AWJ> proper term is for a class that's un-instantiable(sp?) on it's own.
This is an abstract base class, and quite necessary at times.
For example:
class Shape
{
public:
  virtual void draw()=0; // abstract
};
Immediately, Shape cannot be instantiated because of this abstract method.
class Rectangle : public Shape
{
public:
  virtual void draw(); // done in rectangle.cc
};
We can instantiate Rectangle.  The advantage is being able to do something 
like:
Shape* pshapes;
//.. pshapes is given a slew of shapes, including Rectangle's, and other
// shapes derived from Shapes.  If we know how many, we can do:
for (int i = 0; i < nShapes, ++i)
  pshapes[i].draw();
Question, Anders... without the abstract base class, how would you do this?  
Let me guess:
class Shape
{
public:
  virtual void draw() {} // default of nothing
};
Why doesn't this work?  Simple - someone could do:
class Square : public Shape
{
  // woops, forgot about implementing draw!
};
It will compile just fine, but it won't work.  And you'll spend hours upon 
hours wondering why.  If we used the abstract version of Shape, the compiler 
would have complained the first time we tried to do a new Square or 
instantiate a Square on the stack... and would have told us that Square is 
abstract because it doesn't implement draw()!
Further, from an abstract point of view, Shape is not really a type.  It's a 
"class" of items that doesn't really exist.  I know what a square is, what a 
rectangle or circle are.  But what is a "shape"?  Can you describe it?  Not 
really - you should thus never instantiate it.  It is merely a convenient way 
of describing a set of objects.  All Shape objects can be drawn via draw().  
It's like the way that all objects made of class Glass can be broken via 
rock().  :-}
HTH
--- Maximus/2 3.01
---------------
* Origin: Tanktalus' Tower BBS (PVT) (1:342/708)

SOURCE: echomail via exec-pc

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.