Methode onCollision() überschreiben :)
class StopAmoeba : public virtual GenericAmoeba {
bool continue_moving = false;
public:
StopAmoeba(const Point &pos,
const Point &speed,
unsigned radius,
Color color)
:
GenericAmoeba(pos, speed, radius, color)
{}
void onCollision(GenericAmoeba& other) override {
std::cout << std::boolalpha << continue_moving << "\n";
if (continue_moving) {
speed.x = base_speed;
speed.y = base_speed * -1;
}
else {
speed.x = 0;
speed.y = 0;
}
continue_moving = !continue_moving;
}
};
// Setup Scene
std::vector<GenericAmoeba*> v;
GenericAmoeba a( {100,100}, { 50, 50}, 30, GREEN);
RandomAmoeba b( {200,100}, {-50, 50}, 30, RED);
ControllableAmoeba c({400,300}, {100, -100}, 30, ORANGE);
StopAmoeba d( {500,300}, {100, -100}, 30, ORANGE);
v.push_back(&a);
v.push_back(&b);
v.push_back(&c);
v.push_back(&d);
// Setup Scene
std::vector<std::unique_ptr<GenericAmoeba>> v;
v.push_back(std::make_unique<GenericAmoeba>( Point{100,100}, Point{ 50, 50}, 30, GREEN));
v.push_back(std::make_unique<RandomAmoeba>( Point{200,100}, Point{-50, 50}, 30, RED));
v.push_back(std::make_unique<ControllableAmoeba>(Point{400,300}, Point{100, -100}, 30, ORANGE));
v.push_back(std::make_unique<StopAmoeba>( Point{500,300}, Point{100, -100}, 30, ORANGE));
// also change some for-Loops
wird zu
Was unterscheidet die Codebeispiele noch?
Genauer: In welchem Speichersegmet sind z.B. die Variablen a-d jeweils?
#include <vector>
class A{};
int main(int argc, char *argv[]) {
std::vector<A*> v;
for (int i = 0; i < 10; ++i) {
v.push_back(new A());
}
// do something with the elements of v
for(auto& element : v)
delete element;
return 0;
}
#include <vector>
#include <memory>
class A{};
int main(int argc, char *argv[]) {
std::vector<std::unique_ptr<A>> v;
for (int i = 0; i < 10; ++i) {
v.emplace_back(new A());
}
// do something with the elements of v
return 0;
}
#include <vector>
#include <memory>
class A{};
int main(int argc, char *argv[]) {
std::vector<std::unique_ptr<A>> v;
for (int i = 0; i < 10; ++i) {
v.push_back(std::make_unique<A>());
}
// do something with the elements of v
return 0;
}
class GenericAmoeba {
public:
// ...
virtual ~GenericAmoeba() {};
};
class ControllableAmoeba : public virtual GenericAmoeba{
public:
// ...
virtual ~ControllableAmoeba() {};
};
class RandomAmoeba : public virtual GenericAmoeba {
public:
// ...
virtual ~ControllableAmoeba() {};
};
class StopAmoeba : public virtual GenericAmoeba {
public:
// ...
virtual ~StopAmoeba() = default;
};
Wozu brauchen wir virtuelle Destruktoren überhaupt?
Sonst gerne auch gleich in der Übungsphase!
auto squared = [](double i) {
return i * i;
};
struct some_unknown_type {
auto operator()(double i) const {
return i * i;
}
};
Member sind nur aus der Klasse selbst zugänglich
Member sind nur aus der Klasse und Ableitenden zugänglich
Member / Typen / Funktionen sind von überall zugänglich
class A {};
int main() {
A a{}; // default construction
A b{a}; // default copy construction
A c = b; // default copy assignment
A d{A()}; // default move constructor
d = A(); // default move assignment
// default destruction
}
class A {
int i = 0;
public:
virtual int get_int() = 0;
};
class B : public A {
unsigned int get_int() override {
return i;
};
};
int main(int argc, char *argv[]) {
A a{};
return 0;
}
Der folgende Code enthält drei Fehler:
class A {
int foo;
};
class B : public A {
int bar;
};
int main() {
B b{};
A a = b;
// int foo = a.bar
return 0;
}
Der folgende Code ist widerum korrekt. Bleibt er korrekt, wenn man den auskommentierten Codeschnipsel wieder einkommentiert?
Klausurvorbereitung / Übungsblatt 10 im Moodlekurs
Christian Heusel c.heusel@stud.uni-heidelberg.de https://discord.mathphys.info