-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclassExamples.cpp
More file actions
180 lines (173 loc) · 5.04 KB
/
classExamples.cpp
File metadata and controls
180 lines (173 loc) · 5.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#include <iostream>
#include <string>
using namespace std;
class Superhero
{
public:
string name; // Property: Name of the superhero
string superpower; // Property: Special power of the superhero
int strength; // Property: Strength of the superhero
// Constructor
Superhero(string heroName, string heroPower, int heroStrength)
{
name = heroName;
superpower = heroPower;
strength = heroStrength;
}
void useSuperpower()
{
cout << name << " uses " << superpower << "!" << endl;
}
// Method to display values
void displayInfo()
{
cout << "Superhero Name: " << name << endl;
cout << "Superpower: " << superpower << endl;
cout << "Strength Level: " << strength << endl;
}
};
class Spaceship
{
public:
string color; // Property: Color of the spaceship
int speed; // Property: Speed of the spaceship
int fuel; // Property: Available fuel
// Constructor
Spaceship(string shipColor, int shipSpeed, int shipFuel)
{
color = shipColor;
speed = shipSpeed;
fuel = shipFuel;
}
void fly()
{
cout << "The " << color << " spaceship zooms at " << speed << " light-years per hour!" << endl;
}
void refuel()
{
fuel = 100; // Refill fuel to maximum
cout << "Spaceship refueled! Ready for more adventures!" << endl;
}
// Method to display values
void displayInfo()
{
cout << "Spaceship Color: " << color << endl;
cout << "Speed: " << speed << " light-years/hour" << endl;
cout << "Fuel Level: " << fuel << "%" << endl;
}
};
class Monster
{
public:
string type; // Property: Type of monster (e.g., "Fluffy")
int scariness; // Property: How scary the monster is
bool isFriendly; // Property: Is it friendly or not?
// Constructor
Monster(string monsterType, int monsterScariness, bool friendly)
{
type = monsterType;
scariness = monsterScariness;
isFriendly = friendly;
}
void roar()
{
if (isFriendly)
{
cout << "The " << type << " monster makes a friendly growl!" << endl;
}
else
{
cout << "The " << type << " monster roars scarily!" << endl;
}
}
// Method to display values
void displayInfo()
{
cout << "Monster Type: " << type << endl;
cout << "Scariness Level: " << scariness << endl;
cout << "Is Friendly: " << (isFriendly ? "Yes" : "No") << endl;
}
};
class MagicWand
{
public:
string wood; // Property: Type of wood the wand is made of
string core; // Property: Magical core of the wand
int power; // Property: Level of magical power
// Constructor
MagicWand(string wandWood, string wandCore, int wandPower)
{
wood = wandWood;
core = wandCore;
power = wandPower;
}
void castSpell(string spell)
{
cout << "The " << wood << " wand with " << core << " core casts: " << spell << "!" << endl;
}
// Method to display values
void displayInfo()
{
cout << "Wand Wood: " << wood << endl;
cout << "Wand Core: " << core << endl;
cout << "Power Level: " << power << endl;
}
};
class Treasure
{
public:
string item; // Property: Type of treasure (e.g., "golden chest")
int value; // Property: Value of the treasure in gold coins
bool isLocked; // Property: Is it locked?
// Constructor
Treasure(string treasureItem, int treasureValue, bool treasureLocked)
{
item = treasureItem;
value = treasureValue;
isLocked = treasureLocked;
}
void open()
{
if (isLocked)
{
cout << "The " << item << " is locked! Find the key first!" << endl;
}
else
{
cout << "You open the " << item << " and find " << value << " gold coins!"
<< endl;
}
}
// Method to display values
void displayInfo()
{
cout << "Treasure Item: " << item << endl;
cout << "Value: " << value << " gold coins" << endl;
cout << "Is Locked: " << (isLocked ? "Yes" : "No") << endl;
}
};
int main()
{
Superhero myHero("Captain Awesome", "super strength", 100);
myHero.useSuperpower(); // The hero uses their power!
Spaceship playerShip("blue", 1000, 50);
playerShip.fly(); // The ship flies!
playerShip.refuel(); // We refuel
Monster friendlyMonster("Charles", 2, true);
friendlyMonster.roar(); // The monster roars friendly
MagicWand playerWand("oak", "dragon heartstring", 75);
playerWand.castSpell("Lumos"); // We cast a spell
Treasure hiddenChest("golden chest", 500, true);
hiddenChest.open(); // We try to open the chest
cout << "\n--- Displaying All Object Info ---\n" << endl;
myHero.displayInfo();
cout << endl;
playerShip.displayInfo();
cout << endl;
friendlyMonster.displayInfo();
cout << endl;
playerWand.displayInfo();
cout << endl;
hiddenChest.displayInfo();
return 0;
}