Demonstrate runtime type id. : typeid « Development « C++






Demonstrate runtime type id.

  
#include <iostream>
#include <cstdlib>

using namespace std;

class two_d_shape {
protected:
  double x, y;
public:
  two_d_shape(double i, double j) {
    x = i;
    y = j;
  }

  double getx() { return x; }
  double gety() { return y; }

  virtual double area() = 0;
};

// Create a subclass of two_d_shape for triangles.
class triangle : public two_d_shape {
  public:
    triangle(double i, double j) : two_d_shape(i, j) { }

    double area() {
      return x * 0.5 * y;
    }
};

// Create a subclass of two_d_shape for rectangles.
class rectangle : public two_d_shape {
  public:
    rectangle(double i, double j) : two_d_shape(i, j) { }

    double area() {
      return x * y;
    }
};

// Create a subclass of two_d_shape for circles.
class circle : public two_d_shape {
  public:
    circle(double i, double j=0) : two_d_shape(i, j) { }

    double area() {
      return 3.14 * x * x;
    }
};

// A factory for objects derived from two_d_shape.
two_d_shape *factory() {
  static double i = (rand() % 100) / 3.0, j = (rand() % 100) / 3.0;

  i += rand() % 10;
  j += rand() % 12;

  cout << "Generating object.\n";

  switch(rand() % 3 ) {
    case 0: return new circle(i);
    case 1: return new triangle(i, j);
    case 2: return new rectangle(i, j);
}
  return 0;
}

// Compare two shapes for equality. This means that their
// types and dimensions must be the same.
bool sameshape(two_d_shape *alpha, two_d_shape *beta) {
  cout << "Comparing a " << typeid(*alpha).name()
       << " object to a " << typeid(*beta).name()
       << " object\n";

  if(typeid(*alpha) != typeid(*beta)) return false;

  if(alpha->getx() != beta->getx() &&
     alpha->gety() != beta->gety()) return false;

  return true;
}

int main()
{
  // Create a base class pointer to two_d_shape.
  two_d_shape *p;

  // Generate two_d_shape objects.
  for(int i=0; i < 6; i++) {
    // Generate an object.
    p = factory();

    // Display the name of the object.
    cout << "Object is " << typeid(*p).name() << endl;

    // Display its area.
    cout << "    Area is " << p->area() << endl;

    // Keep a count of the object types that have been generated.
    if(typeid(*p) == typeid(triangle))
      cout << "    Base is " << p->getx() << " Height is "
           << p->gety() << endl;

    else if(typeid(*p) == typeid(rectangle))
      cout << "    Length is " << p->getx() << " Height is "
           << p->gety() << endl;

    else if(typeid(*p) == typeid(circle))
      cout << "    Diameter is " << p->getx() << endl;

    cout << endl;
  }

  cout << endl;

  // Make some objects to compare.
  triangle t(2, 3);
  triangle t2(2, 3);
  triangle t3(3, 2);
  rectangle r(2, 3);

  // Compare two two_d_objects.
  if(sameshape(&t, &t2))
    cout << "t and t2 are the same.\n";

  if(!sameshape(&t, &t3))
    cout << "t and t3 differ.\n";

  if(!sameshape(&t, &r))
    cout << "t and r differ.\n";

  cout << endl;

  return 0;
}
  
    
  








Related examples in the same category

1.Here is a simple example that uses typeidHere is a simple example that uses typeid
2.An example that uses typeid on a polymorphic class hierarchyAn example that uses typeid on a polymorphic class hierarchy
3.An example that uses typeid for base and derived classesAn example that uses typeid for base and derived classes
4.typeid for polymorphic class
5.A simple example that uses typeid.
6.Use a reference with typeid.
7.Demonstrating run-time type id.
8.typeid Can Be Applied to Template Classestypeid Can Be Applied to Template Classes
9.Demonstrate == and != relative to typeid.Demonstrate == and != relative to typeid.