Demonstrating run-time type id. : typeid « Development « C++






Demonstrating run-time type id.

   
#include <iostream>
using namespace std;
   
class Mammal {
public:
  virtual bool lays_eggs() { return false; } // Mammal is polymorphic
};
   
class Cat: public Mammal {
public:
};
   
class Platypus: public Mammal {
public:
  bool lays_eggs() { return true; }
};
   
class Dog: public Mammal {
public:
};
   
// A factory for objects derived from Mammal.
Mammal *factory(){
  switch(rand() % 3 ) {
    case 0: return new Dog;
    case 1: return new Cat;
    case 2: return new Platypus;
  }
  return 0;
}
 
int main(){
  Mammal *ptr; // pointer to base class
  int i;
  int c=0, d=0, p=0;
   
  // generate and count objects
  for(i=0; i<10; i++) {
    ptr = factory(); // generate an object
   
    cout << "Object is " << typeid(*ptr).name();
    cout << endl;
   
    // count it
    if(typeid(*ptr) == typeid(Dog)) d++;
    if(typeid(*ptr) == typeid(Cat)) c++;
    if(typeid(*ptr) == typeid(Platypus)) p++;
  }
   
  cout << "  Dogs: " << d << endl;
  cout << "  Cats: " << c << endl;
  cout << "  Platypuses: " << p << 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.Demonstrate runtime type id.
5.typeid for polymorphic class
6.A simple example that uses typeid.
7.Use a reference with typeid.
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.