#include <iostream.h>

// When using virtual functions you must be careful about 
// methods that have no implementation.  If you define
// a method to have no implementation in the base class,
// it means the base class is an *abstract class*.  An abstract
// class has no direct instances.  A class with a method that
// has no implementation MUST be an abstract class.
//
// To enforce to the compiler that a class is abstract, on at
// least one of the methods you must use the "=0" (shown below)
// notation to indicate that the method has no implementation.
//
// Any class that will have direct instances must have implementations
// for all methods.  If not you will get strange linker errors.

class shape {
public:
  virtual void getDimensions()=0; // this is an abstract class, so 
  virtual float area()=0;         // there is no definition of these
  virtual char* namestring()=0;   // methods for this class.
};

class square : public shape {
public:
  void getDimensions()
    { 
      cout << "Enter length: ";
      cin >> side;
    }

  float area()
    { return (float) side*side; }

  char *namestring() 
    { return "Square"; }

private:
  int side;
};

class triangle : public shape {
public:
  void getDimensions()
    { 
      cout << "Enter base: ";
      cin >> base;
      cout << "Enter height: ";
      cin >> height;
    }

  float area()
    { return (float) (base * height) * .5; }

  char *namestring() 
    { return "Triangle"; }

private:
  int base;
  int height;
};

int main()
{
  shape *s;
  int type;
  
  cout << "Enter 1 for triangle, 2 for square, 0 to quit: ";
  cin >> type; 

  while (type) {
    switch (type) {
    case 1: s = new triangle; break;
    case 2: s = new square; break;
    }

    s->getDimensions();
    cout << "The area of the " << s->namestring() <<
      " is " << s->area() << endl;
    delete(s);

    cout << endl << "Enter 1 for triangle, 2 for square, 0 to quit: ";
    cin >> type; 
  }
}

