Posted by Daniel Longest on April 06, 2001 at 02:32:58:
In Reply to: What's an abstract class? posted by Matt Green on April 06, 2001 at 01:34:12:
I don't claim to be a guru but I'll jump in and answer anyway. An abstract base class (sometimes called ABC) is a class that you cannot instantiate a type for. This is done by providing at least one pure virtual function for the class. For example, if I want class ABC to be an ABC, i can do something like virtual void func()=0; in the class defintiion. This looks odd but it means func is pure virtual and if I try to declare a varaible of type ABC, I'll get an error about being unable to create a variable of that type. You can see the comp.lang.c++ FAQ at http://www.parashift.com/cpp-faq-lite/abcs.html for more discussion about this (the rest of the FAQ is excellent, btw). In the FAQ, Cline uses the example that you can't really tell someone to draw a shape of size 7, each shape (like rectangle, circle, triangle) is drawn differently. But you could make a base class called Shape, and then give it a pure virtual function called draw. Then implement draw for each of the classes you derive and there you go, the approrpriate draw will be done based on the type of variable you have. The use of an ABC is done to separate the interface from the implemenation.
: In the spec, it mentions that the creature inheritance hierarchy must have an abstract class at it's ultimate base, and I know we haven't covered it in class yet, but I'm a bit eager to get started on the project because the days leading up to the 22nd are going to be very dicey.
: Plus I know some C++ guru will be happy to chime in.
Post a Followup