Im learning about run-time Type IDs and casting operators at the moment in C++. I got this sample code from one of the books I am learning off. What the program output should be producing is the EXACT type that each object belongs to. Commenting out the f() function just makes all objects return the type as Base

i.e. sample output should be

p is pointing to an object of type class Base
p is pointing to an object of type class Derived1
p is pointing to an object of type class Derived2


The program has no problems compiling ( i get no warnings either), however upon run-time it generates a debugging error and has to be terminated. Im using VC++ 5 that supports visual studio 97.


The code has been shown below. The book I am using is C++ from the ground up by Herbert Schildt



#include <iostream>
#include <typeinfo>
#include <stdio.h>

using namespace std;

/* create base class */

class Base{
virtual void f() {};
//.......
};

/* create first derived class */

class Derived1 : public Base {
//.....

};


/* create second derived class */

class Derived2 : public Base {

};


int main(){
Base *p,baseob;
Derived1 ob1;
Derived2 ob2;

/* Point to base class */

p = &baseob;
cout<<"P is pointing to an object of type ";
cout<<typeid(*p).name()<<"\n";

/* point to first derived class */


p = &ob1;
cout<<"P is pointing to an object of type ";
cout<<typeid(*p).name()<<"\n";

/* point to second derived class */

p = &ob2;
cout<<"P is pointing to an object of type ";
cout<<typeid(*p).name()<<"\n";


return 0;
}


Is there something wrong with my syntax or am I using the incorrect headers ??
Many thanks in advance