-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMidterm.cpp
More file actions
197 lines (166 loc) · 3.57 KB
/
Copy pathMidterm.cpp
File metadata and controls
197 lines (166 loc) · 3.57 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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
// Given the following Music class, construct it in main and invoke it's print function
/*
#include <iostream>
class Music {
public:
Music(std::string track) {
this->track = track;
}
void play() {
std::cout << this->track << "\n";
}
std::string track;
};
int main()
{
// construct the class and call print here...
Music myMusic("Big Iron by Marty Robbins");
myMusic.play();
}
// Given the following vector of Pokemon, print out each of their names.
#include <iostream>
#include <vector>
struct Pokemon {
std::string name;
std::vector<std::string> weaknesses;
};
int main()
{
std::vector<Pokemon> myPokemon =
{
{"Pikachu", {"ground"}},
{"Charizard", {"water", "wind"}},
{"Gengar", {"light"}}
};
// print the names here...
for (const auto& pokemon : myPokemon) {
std::cout << pokemon.name << std::endl;
}
}
// Given the same vector of Pokemon, print out each of their weaknesses.
#include <iostream>
#include <vector>
struct Pokemon {
std::string name;
std::vector<std::string> weaknesses;
};
int main()
{
std::vector<Pokemon> myPokemon =
{
{"Pikachu", {"ground"}},
{"Charizard", {"water", "wind"}},
{"Gengar", {"light"}}
};
// print the weaknesses here...
for (const auto& pokemon : myPokemon) {
std::cout << pokemon.name << " Weaknesses: ";
for (const auto& weakness : pokemon.weaknesses) {
std::cout << weakness << " ";
}
std::cout << std::endl;
}
}
class Obfuscated {
double x;
int y;
float z;
public:
double a() {
return 0.0;
}
double b() {
return x;
}
void c(int y) {
this->y = y;
}
float d() {
return z;
}
void e(double x) {}
};
int main()
{
return 0;
}
// Give this class a default constructor and call the build function in main.
#include <stdio.h>
class Skyscraper {
public:
// create default constructor here
Skyscraper() : floors(3), sqft(9999) {}
void build() {
printf("Building a %0.2f sqft, %d floor skyscraper!\n",
this->sqft, this->floors);
}
private:
int floors;
double sqft;
};
int main()
{
// construct the class and call build here
Skyscraper mySkyscraper;
mySkyscraper.build();
}
// Why isn't this code working and how might we fix it?
// It should print "Hooray, it's working" once the error is fixed.
#include <iostream>
class MyPerfectClass {
public:
MyPerfectClass() {
this->favoriteNumber = 5;
}
void pleaseWork() {
std::cout << "Hooray, it's working!";
}
private:
int favoriteNumber;
};
int main()
{
MyPerfectClass mpc;
mpc.pleaseWork();
}
// Why isn't this code working and how can we fix it?
// Note the namespace being used.
namespace foo
{
namespace bar
{
struct Log { };
}
}
using namespace foo;
int main()
{
bar::Log l = {};
}
// Overload the plus(+) operator such that two points added together create a new point with the coordinates added together.
#include <stdio.h>
class Point {
public:
Point(int x, int y) {
this->x = x;
this->y = y;
};
void print() {
printf("(%d, %d)\n", this->x, this->y);
}
// overload the + operator here
Point operator+(const Point& other) {
return Point(this->x + other.x, this->y + other.y);
}
private:
int x;
int y;
};
int main()
{
Point point1(7, 10);
Point point2(3, -13);
Point point3 = point1 + point2;
point3.print(); // should print (10, -3)
}
*/