Polymorphism
- What is polymorphism?
No answers here :)
-
How does
dynamic_castdiffer fromstatic_cast?
No answers here :)
-
Compare the outputs of the below program using
Base::print()then a virtually-markedBase::print(). Explain how thevirtualkeyword creates the discrepancy and enables polymorphism.
#include <iostream>
struct Base {
void print() { std::cout << "Base\n"; }
virtual void print() { std::cout << "Base\n"; }
};
struct Derived : public Base {
void print() { std::cout << "Derived\n"; }
};
int main() {
Derived d;
Base& b{dynamic_cast<Base&>(d)};
b.print();
}
No answers here :)
- What compiler error is given here? How can this be fixed (if we do expect the override)?
#include <iostream>
class Base {
public:
void print() { std::cout << "Base\n"; }
};
class Derived : public Base {
public:
void print() override { std::cout << "Derived\n"; }
};
int main() {
Derived d;
d.print();
}
No answers here :)
-
Why does this code throw
bad_castexception when this downcast happens?
#include <iostream>
class Base {
public:
virtual void print() { std::cout << "Base\n"; }
};
class Derived : public Base {
public:
void print() override { std::cout << "Derived\n"; }
};
int main() {
Base b;
Derived& d{dynamic_cast<Derived&>(b)};
}
No answers here :)
- When defining an abstract class, which methods should be marked as pure virtual? Why are destructors typically pure virtual?
No answers here :)
Webots II
The starter code for this tutorial can be found here.
-
It is possible for Webots controllers to utilise multiple external source and header files. Edit the
Makefilein the controller “Files” so that the controller can build without any error. You should not need to edit any other file.
No answers here :)
-
Open the Webots controller “KeyPress”. Implement this controller so that it reads in any key presses via the
Keyboarddevice and writes it directly to the console.
No answers here :)
a. Edit the controller so that only “WASD” is printed to the console if these keys are pressed.
No answers here :)
- Open two Webots controllers “Emitter” and “Receiver” which when assigned to two separate robots will have a one-way communication with each other.
a. Edit “Emitter” so that its Emitter device simply sends some message over and over.
No answers here :)
b. Edit “Receiver” so that its Receiver device reads the message from “Emitter” and prints it to console.
No answers here :)