Repository navigation
Expand file tree
/
Copy path13_dynamic_cast.cpp
More file actions
78 lines (61 loc) · 1.88 KB
/
Copy path13_dynamic_cast.cpp
File metadata and controls
78 lines (61 loc) · 1.88 KB
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include "13_dynamic_cast.h"
namespace DynamicCast {
void Animal::makeSound() {
std::cout << "Animal sound\n";
}
void Dog::makeSound() {
std::cout << "Woof!\n";
}
void Dog::bark() {
std::cout << "Bark bark!\n";
}
void Dog::wagTail() {
std::cout << "Wagging tail\n";
}
void Cat::makeSound() {
std::cout << "Meow!\n";
}
void Cat::meow() {
std::cout << "Meow meow!\n";
}
void Cat::purr() {
std::cout << "Purr purr\n";
}
void Bird::makeSound() {
std::cout << "Tweet!\n";
}
void Bird::fly() {
std::cout << "Flying high\n";
}
void execute() {
std::cout << "\n=== 13: DYNAMIC CAST ===\n";
std::cout << "Safe Type Conversion:\n";
Animal* animals[] = {new Dog(), new Cat(), new Bird()};
for (int i = 0; i < 3; i++) {
std::cout << "\n--- Animal " << (i+1) << " ---\n";
animals[i]->makeSound();
// Safe downcasting with dynamic_cast
Dog* dog = dynamic_cast<Dog*>(animals[i]);
if (dog) {
std::cout << "This is a dog! ";
dog->wagTail();
}
Cat* cat = dynamic_cast<Cat*>(animals[i]);
if (cat) {
std::cout << "This is a cat! ";
cat->purr();
}
Bird* bird = dynamic_cast<Bird*>(animals[i]);
if (bird) {
std::cout << "This is a bird! ";
bird->fly();
}
}
// Cleanup
for (int i = 0; i < 3; i++) {
delete animals[i];
}
std::cout << "\n• Dynamic cast returns nullptr for failed casts\n";
std::cout << "• Safe runtime type identification\n";
}
}