-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCpp1_8.html
More file actions
92 lines (78 loc) · 3.26 KB
/
Copy pathCpp1_8.html
File metadata and controls
92 lines (78 loc) · 3.26 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
<article>
<h1>C++ I: Page 8 - OOP: Classes & Objects</h1>
<section>
<h2>The Blueprint for Reality</h2>
<p>Classes in C++ are the templates for creating objects, combining data (members) and functionality (methods) into a cohesive unit.</p>
<h3>The Code implementation</h3>
<pre><code class="language-cpp">/*
* File: classes.cpp
* Demonstration of class definition, constructors, const methods, and static members
*/
#include <iostream>
#include <string>
using namespace std;
class Vehicle {
private:
string brand;
int year;
static int vehicleCount; // Static member: Shared by all instances
public:
// 1. Constructor: Initialize with initializer list
Vehicle(string b, int y) : brand(b), year(y) {
vehicleCount++;
cout << "Vehicle constructed: " << brand << endl;
}
// 2. Destructor
~Vehicle() {
cout << "Vehicle destroyed." << endl;
}
// 3. Const Method: Cannot modify object members
void displayInfo() const {
cout << "Brand: " << brand << ", Year: " << year << endl;
}
// 4. Static Method: Belongs to class, not instance
static int getVehicleCount() {
return vehicleCount;
}
};
// Initialize static member
int Vehicle::vehicleCount = 0;
int main() {
// Stack instantiation
Vehicle car("Toyota", 2020);
car.displayInfo();
// Heap instantiation (requires manual delete)
Vehicle* truck = new Vehicle("Ford", 2022);
truck->displayInfo();
delete truck; // Critical to prevent memory leak
cout << "Total vehicles: " << Vehicle::getVehicleCount() << endl;
return 0;
}
/*
* OOP Nuances:
* 1. Initializer lists (e.g., : brand(b)) are faster than assignment in body.
* 2. 'const' method: Ensures the method doesn't modify object members,
* essential for const objects.
* 3. Static members: Shared across all instances of a class.
* 4. Access Modifiers: 'private' protects internal state, 'public' exposes interface.
* 5. Destructors: Automatic clean up for stack objects, manual for heap objects.
* 6. Always initialize your members; undefined state can lead to bugs.
*/
</code></pre>
<div class="interactive-sim">
<h3>Object Creation Simulator</h3>
<button class="sim-button" onclick="
const out = document.getElementById('sim-output-cpp1_8');
out.innerHTML = 'Vehicle car("Toyota", 2020);<br>';
setTimeout(() => out.innerHTML += '>> Car instance created!', 800);
">Run Simulation</button>
<div id="sim-output-cpp1_8" 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_7.html" style="text-decoration: none; color: #6c757d;">← Previous: Pointers & References</a>
<a href="#" data-file="Cpp1_9.html" style="font-weight: bold; text-decoration: none; color: #007bff;">Next: OOP: Inheritance & Polymorphism →</a>
</div>
</footer>
</article>