-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsummry.cpp
More file actions
122 lines (99 loc) · 3.67 KB
/
Copy pathsummry.cpp
File metadata and controls
122 lines (99 loc) · 3.67 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include <iostream>
// 1. CLASS: The blueprint or user-defined data type
class Robot {
private:
int batteryLevel;
public:
// 2. CONSTRUCTOR: Automatically called when the object is created
Robot() {
batteryLevel = 100;
std::cout << "Robot initialized with 100% battery." << std::endl;
}
// 3. DESTRUCTOR: Automatically called when the object is destroyed
~Robot() {
std::cout << "Robot is powering down." << std::endl;
}
// 4. METHOD: A function belonging to the class that defines behavior
void charge() {
batteryLevel = 100;
std::cout << "Battery recharged." << std::endl;
}
};
int main() {
// 5. OBJECT: An instance of the Robot class (myBot)
Robot myBot;
// Calling the method using the object
myBot.charge();
// 6. LABEL: Used for flow control (e.g., jump points)
goto end_of_program;
end_of_program:
std::cout << "Program complete." << std::endl;
// myBot is automatically destroyed here as it goes out of scope
return 0;
}
#include <iostream>
#include <string>
// A dependency class without a default constructor
class Processor {
private:
int speed;
public:
Processor(int speed) : speed(speed) {} // Resolves naming conflict directly
int getSpeed() const { return speed; }
};
// Main Class containing various member types
class Robot {
private:
std::string name; // Standard object
const int modelYear; // Constant field (Requires Initializer List)
int& energyGridRef; // Reference field (Requires Initializer List)
Processor proc; // Object without default constructor (Requires Initializer List)
public:
// MEMBER INITIALIZER LIST
// Resolves identical names cleanly: field(parameter)
Robot(std::string name, int modelYear, int& energyGridRef, int procSpeed)
: name(name),
modelYear(modelYear),
energyGridRef(energyGridRef),
proc(procSpeed) // Explicitly builds the dependency object
{
// CONSTRUCTOR BODY
std::cout << "[Constructor] " << this->name << " built successfully." << std::endl;
}
// DESTRUCTOR
~Robot() {
std::cout << "[Destructor] " << name << " has been powered down." << std::endl;
}
// METHOD (Demonstrating name shadowing and in-body 'this->' resolution)
void updateName(std::string name) {
// name = name; // ❌ Failure: Assigns parameter to itself
this->name = name; // Success: 'this->name' target fields specifically
std::cout << "[Method] Robot name changed to: " << this->name << std::endl;
}
void status() const {
std::cout << "Robot Name: " << name
<< " | Model: " << modelYear
<< " | CPU: " << proc.getSpeed() << "MHz"
<< " | Grid Level: " << energyGridRef << "%" << std::endl;
}
};
int main() {
int mainGrid = 95; // Variable to back our reference field
std::cout << "--- Creating Object ---" << std::endl;
// OBJECT creation (Triggers Constructor and Initializer List)
Robot myBot("Alpha-1", 2026, mainGrid, 3200);
myBot.status();
std::cout << "\n--- Testing Method & Shadowing ---" << std::endl;
// METHOD Call
myBot.updateName("Omega-9");
myBot.status();
std::cout << "\n--- Testing Flow Control ---" << std::endl;
// LABEL and GOTO flow control
goto skip_block;
std::cout << "This line will be skipped." << std::endl;
skip_block: // LABEL
std::cout << "Jumped directly to 'skip_block' label." << std::endl;
std::cout << "\n--- Ending Main Scope ---" << std::endl;
// Object 'myBot' leaves scope here, implicitly triggering the DESTRUCTOR
return 0;
}