-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
100 lines (71 loc) · 2.04 KB
/
main.cpp
File metadata and controls
100 lines (71 loc) · 2.04 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
#include <iostream>
#include <string>
using namespace std;
// Step 1: Define the Robot class
class Robot {
private:
string name;
string model;
int batteryLife;
public:
// Constructor
// Getter Methods
// Setter Methods
// Display function
void displayRobot() {
cout << "Robot: " << name << " | Model: " << model << " | Battery Life: " << batteryLife << "%\n";
}
};
// Step 2: Function to modify robot (pass by value)
// Step 3: Function to modify robot (pass by reference)
// Step 4: Template class for a Fleet that stores multiple robots
template <typename T>
class Fleet {
private:
T* robots;
int capacity;
int count;
public:
// Constructor: Allocates space for 'capacity' robots
Fleet(int cap) {
capacity = cap;
count = 0;
robots = new T[capacity];
}
// Add robot to fleet
void addRobot(T robot) {
if (count < capacity) {
robots[count] = robot;
count++;
} else {
cout << "Fleet is full!\n";
}
}
// Display all robots
void showFleet() {
cout << "Fleet contains:\n";
for (int i = 0; i < count; i++) {
cout << "- " << robots[i] << "\n";
}
}
// Destructor: Free allocated memory
~Fleet() {
delete[] robots;
}
};
int main() {
// Step 5: Create a Robot object
// Step 6: Use pointers to access Robot object
// cout << "Updated Battery Life (using pointer): " << robotPtr->getBatteryLife() << "%\n";
// Step 7: Pass by value (no change outside function)
// cout << "After modifyRobotByValue, Battery Life: " << myRobot.getBatteryLife() << "%\n";
// Step 8: Pass by reference (changes persist)
// cout << "After modifyRobotByReference, Battery Life: " << myRobot.getBatteryLife() << "%\n";
// Step 9: Use the Fleet template class
Fleet<string> myFleet(3);
myFleet.addRobot("Autobot-X");
myFleet.addRobot("Cybertron-7");
myFleet.addRobot("NanoDroid-3");
myFleet.showFleet();
return 0;
}