1 #include <iostream>
2 #include <string.h>
3 #include <typeinfo.h>
4 using std::cout;
5 using std::endl;
6
7 class graphicimage
8 {
9 protected:
10 char name[80];
11 public:
12 graphicimage()
13 {
14 strcpy(name, "graphicimage");
15 }
16
17 virtual void display()
18 {
19 cout << "display a generic image." << endl;
20 }
21
22 char* getName()
23 {
24 return name;
25 }
26 };
27
28 class GIFImage : public graphicimage
29 {
30 public:
31 GIFImage()
32 {
33 strcpy(name, "GIFImage");
34 }
35
36 void display()
37 {
38 cout << "display a GIF image." << endl;
39 }
40 };
41
42 class PICIamge : public graphicimage
43 {
44 public:
45 PICIamge()
46 {
47 strcpy(name, "PICImage");
48 }
49 void display()
50 {
51 cout << "display a PIC image." << endl;
52 }
53 };
54
55 void process(graphicimage* type)
56 {
57 if(typeid(GIFImage) == typeid(*type))
58 {
59 ((GIFImage*)type)->display();
60 }
61 else if(typeid(PICIamge) == typeid(*type))
62 {
63 ((PICIamge*)type)->display();
64 }
65 else
66 {
67 cout << "Unknow type! " << (typeid(*type)).name() << endl;
68 }
69 }
70
71 int main()
72 {
73 graphicimage *gImage = new GIFImage;
74 graphicimage *pImage = new PICIamge;
75 graphicimage *uImage = new graphicimage;
76
77 process(gImage);
78 process(pImage);
79 process(uImage);
80 system("pause");
81 return 0;
82 }
83