-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCpp1_9.html
More file actions
93 lines (81 loc) · 3.08 KB
/
Copy pathCpp1_9.html
File metadata and controls
93 lines (81 loc) · 3.08 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
<article>
<h1>C++ I: Page 9 - OOP: Inheritance & Polymorphism</h1>
<section>
<h2>Reusability and Dynamic Dispatch</h2>
<p>Inheritance allows you to create specialized classes from general ones, while Polymorphism (via <code>virtual</code> functions) allows you to treat different types through a common interface.</p>
<h3>The Code implementation</h3>
<pre><code class="language-cpp">
/*
* File: inheritance.cpp
* Demonstration of polymorphism, virtual functions, and final keyword
*/
#include <iostream>
#include <vector>
#include <string>
using namespace std;
// Base class
class Animal {
public:
// Virtual destructor is crucial for base classes
virtual ~Animal() { cout << "Animal destructor" << endl; }
// Virtual function enables polymorphism
virtual void speak() const {
cout << "Some animal sound" << endl;
}
};
// Derived class: final prevents further inheritance
class Dog final : public Animal {
public:
void speak() const override { // Explicit override
cout << "Woof!" << endl;
}
};
// Derived class
class Cat : public Animal {
public:
void speak() const override {
cout << "Meow!" << endl;
}
};
int main() {
// Dynamic dispatch using pointers to base class
vector<Animal*> zoo;
zoo.push_back(new Dog());
zoo.push_back(new Cat());
cout << "--- Polymorphism in action ---" << endl;
for (const auto& animal : zoo) {
animal->speak(); // Correct version called at runtime
}
// Cleanup
for (auto animal : zoo) {
delete animal;
}
return 0;
}
/*
* OOP Nuances:
* 1. Virtual destructor: Ensures proper cleanup of derived objects
* when deleted via base pointer.
* 2. 'virtual' keyword: Instructs compiler to use dynamic dispatch (vtable).
* 3. 'override': Strongly recommended to catch errors in signature mismatch.
* 4. 'final': Prevents further derivation of a class or overriding of a method.
* 5. Polymorphism enables flexible, extensible code architectures.
* 6. Slicing: Be careful not to pass objects by value when polymorphism
* is expected; always use pointers or references.
*/
</code></pre>
<button class="sim-button" onclick="
const out = document.getElementById('sim-output-cpp1_9');
out.innerHTML = 'Animal* a = new Dog();<br>';
setTimeout(() => out.innerHTML += '>> a->speak() calls Dog::speak()', 800);
">Run Simulation</button>
<div id="sim-output-cpp1_9" class="sim-output"></div>
</div>
</section>
<footer style="margin-top: 40px; padding: 20px; background: #f8f9fa; border-top: 1px solid #dee2e6;">
<div style="display: flex; justify-content: space-between;">
<a href="#" data-file="Cpp1_8.html" style="text-decoration: none; color: #6c757d;">← Previous: OOP: Classes & Objects</a>
<a href="#" data-file="Cpp1_10.html" style="font-weight: bold; text-decoration: none; color: #007bff;">Next: RAII & Destructors →</a>
</div>
</footer>
</article>