-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdowncasting.cpp
51 lines (41 loc) · 974 Bytes
/
downcasting.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <iostream>
#include <string>
#include <typeinfo>
using namespace std;
class base{
public:
virtual void show() { cout << "I am base class object" << endl; }
};
class derived: public base{
public:
void show() { cout << "I am dervied class object" << endl; }
};
int main(int argc, char *argv[])
{
base *b1 = new base();
base *b2 = new derived();
b1->show();
b2->show();
try{
derived *bd1 = dynamic_cast<derived *>( b1 );
derived *bd2 = dynamic_cast<derived *>( b2 );
if( bd1 ){
cout << "bd1 downcasted into its derived class object" << endl;
bd1->show();
}
else{
cout << "b1 can't be downcasting to its derived class object" << endl;
}
if( bd2 ){
cout << "bd2 downcasted into its derived class object" << endl;
bd2->show();
}
else{
cout << "b2 can't be downcasting to its derived class object" << endl;
}
}
catch(bad_cast){
cout << "downcasting failed" << endl;
}
return 0;
}