An example that uses typeid on a polymorphic class hierarchy : Polymorphism « Class « C++






An example that uses typeid on a polymorphic class hierarchy

An example that uses typeid on a polymorphic class hierarchy
 


#include <iostream>
#include <typeinfo>
using namespace std;
class Mammal {
public:
  virtual bool laysEggs() { 
    return false; 
  }
};
class Cat: public Mammal {
public:
};
class Platypus: public Mammal {
public:
  bool laysEggs() { 
    return true; 
  }
};
int main()
{
  Mammal *p, AnyMammal;
  Cat cat;
  Platypus platypus;
  p = &AnyMammal;
  cout << "p is pointing to an object of type ";
  cout << typeid(*p).name() << endl;
  p = &cat;
  cout << "p is pointing to an object of type ";
  cout << typeid(*p).name() << endl;
  p = &platypus;
  cout << "p is pointing to an object of type ";
  cout << typeid(*p).name() << endl;
  return 0;
}

           
         
  








Related examples in the same category

1.Use virtual functions and polymorphism. Use virtual functions and polymorphism.
2.Object array: polymorphism. Object array: polymorphism.
3.Polymorphism with base class pointer
4.dynamic allocation and polymorphism