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.
There would be no reason to define class Shape at all, it we did not intend to derive classes from it.
Shape *S[3] = {
new Circle (7, Point(0, 2)),
new Rectangle(3, 6, Point(8, 4)),
new Rectangle(2, 3, Point(0, 1))};
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?
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?
virtual int f f( int a) const = 0;
virtual void dummy () = 0;
What is the effect of designating a virtual member function of a class A as pure virtual?
For an abstract base class, which virtual function should be declared pure virtual? Which should not?
virtual bool straightBoundary ( ) const { return true; }
virtual bool straightBoundary ( ) const { return false; }
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.
// Virtual, but not pure virtual.
char *Shape::description ( ) const {
ostrtream os ( desc, 100 ) ;
os << colorStr[ clr ] << " " << name ( ) << " at " << posn << ends; return desc; }
char *Circle::description ( ) const
{ shape::description ( ); ostrstream os ( desc, 100, os::app) ; os << " , radius = " << rad << ends; return desc;}