Abstract Classes
Abstract Classes
An abstract class is a class that is designed to be specifically used as a base class. An abstract class contains at least one pure virtual function. You declare a pure virtual function by using a pure specifier (= 0) in the declaration of a virtual member function in the class declaration. The following is an example of an abstract class:
, nor can
struct A { virtual void f() = 0; }; struct B : A { virtual void f() { } }; // Error: // Class A is an abstract class // A g(); // // // A& Error: Class A is an abstract class void h(A); i(A&);
int main() { // Error: // Class A is an abstract class // A a; A* pa; B b; // Error: // Class A is an abstract class // static_cast<A>(b); }
Class A is an abstract class. The compiler would not allow the function declarations A g() or void h(A), declaration of object a, nor the static cast of b to type A.
Virtual member functions are inherited. A class derived from an abstract base class will also be abstract unless you override each pure virtual function in the derived class. For example:
class AB { public: virtual void f() = 0; }; class D2 : public AB { void g(); }; int main() { D2 d; }
The compiler will not allow the declaration of object d because D2 is an abstract class; it inherited the pure virtual function f()from AB. The compiler will allow the declaration of object d if you define function D2::g(). Note that you can derive an abstract class from a nonabstract class, and you can override a non-pure virtual function with a pure virtual function. You can call member functions from a constructor or destructor of an abstract class. However, the results of calling (directly or indirectly) a pure virtual function from its constructor are undefined. The following example demonstrates this:
struct A { A() { direct(); indirect(); } virtual void direct() = 0; virtual void indirect() { direct(); } };
The default constructor of A calls the pure virtual function direct() both directly and indirectly (through indirect()). The compiler issues a warning for the direct call to the pure virtual function, but not for the indirect call. Related information