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

Home
C++
1.Bitset
2.Class
3.Console
4.Data Structure
5.Data Type
6.Deque
7.Development
8.File
9.Function
10.Generic
11.Language
12.List
13.Map Multimap
14.Overload
15.Pointer
16.Qt
17.Queue Stack
18.Set Multiset
19.STL Algorithms Binary search
20.STL Algorithms Heap
21.STL Algorithms Helper
22.STL Algorithms Iterator
23.STL Algorithms Merge
24.STL Algorithms Min Max
25.STL Algorithms Modifying sequence operations
26.STL Algorithms Non modifying sequence operations
27.STL Algorithms Sorting
28.STL Basics
29.String
30.Valarray
31.Vector
C / ANSI-C
C Tutorial
C++ Tutorial
Visual C++ .NET
C++ » Development » typeidScreenshots 
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() 1003.0, j = (rand() 1003.0;

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

  cout << "Generating object.\n";

  switch(rand() ) {
    case 0return new circle(i);
    case 1return new triangle(i, j);
    case 2return 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(23);
  triangle t2(23);
  triangle t3(32);
  rectangle r(23);

  // 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.
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.