Hi, my name is Joseph Guangsheng He. Welcome to this page.

This page is under heavy construction. I will cover some interest topic for OOP here.


Part I Abstract Base Classes and Pure Vrtual Functions

Some classes, e. g. class Shape, are not particularly useful by itself.

Our class Shape should have been declared to be an abtract base class.

What is the effect of declaring class A an abstract base class?

  1. We canot creat objects of A.
  2. We can creat pointers or references to A.
  3. We may not need to supply definitions for some of the virtual functions of A.

    Suppose we declare Shape to be an abstract base class.

    Shape S; //Error

    Circle C; //OK

    Shape *ps1 = new Circle; // OK

    Shape *ps2 = new Shape; //Error

    Shape &S = C; //OK

    int f (Shape S); // Error

    int g (Shape &t) ; //OK, t will refer to a Circle or Rectangle.

    Shape h(int i); //Error.

    Shape &k (int i); //OK as long as the restrictions on ref return are met.

How do we declare a base class as abstract?

What is the effect of designating a virtual member function of a class A as pure virtual?

  1. It makes A as abstract base class.
  2. It means that the class A need not supply a definition for the virtual function ( and usually doesn't).
  3. It means that every class directly derived from A must override the virtual member function.

For an abstract base class, which virtual function should be declared pure virtual? Which should not?

If the base class virtual function can perform a portion of the computation common to all the derived class member functions that override it, then we might choose not to make the base class function pure virtual.