Inheritance
- Define the following terminology:
- Inheritance
- Base class
- Derived class
- Consider the following simple inheritance program. What would be the expected output?
struct A {
A() { std::cout << "Creating A" << std::endl; }
~A() { std::cout << "Destroying A" << std::endl; }
};
struct B : public A {
B() { std::cout << "Creating B" << std::endl; }
~B() { std::cout << "Destroying B" << std::endl; }
};
int main() {
B b;
}
- What would be the new access specifiers for each of the strings within the derived classes?
class Base {
public:
std::string str1{"originally public"};
protected:
std::string str2{"originally protected"};
private:
std::string str3{"originally private"};
};
class PublicDerived : public Base {};
class ProtectedDerived : protected Base {};
class PrivateDerived : private Base {};
-
Suppose we are given a simple class
PersoninPerson.hppto represent a person with a name (std::string) and age (unsigned int).
class Person {
public:
Person(std::string name, unsigned int age) : mName(name), mAge(age) {}
std::string getName() const { return mName; }
std::string& getName() { return mName; }
unsigned int getAge() const { return mAge; }
unsigned int& getAge() { return mAge; }
private:
std::string mName;
unsigned int mAge;
};
a. Derive a class Student in Student.hpp from Person which includes a new field called zid (unsigned int). Include a constructor which initialises all class’ attributes and appropriate getters and setters.
b. In main.cpp, instantiate the Student object and print out the student’s name, age, and zid.
int main() {
Student s("Alex", 21, 5123123);
}
c. Derive a class Tutor in Tutor.hpp from Person which includes a list of students (std::vector<Student>) in the tutor’s class. Include:
- a constructor which initialises the tutor’s
Personattributes. - an adder to append a list of students to the tutor’s list.
- a getter to get students by their name. Throws an exception if the student cannot be found.
d. In main.cpp, instantiate the tutor and some more students. Add the students to the tutor’s list.
Webots I
- Where would you find official Webots documentation?
- Create a new blank Webots world with a rectangle arena.
a. Where is this world saved?
b. Add a robot anywhere in the world.
c. Add a block anywhere in the world.
- Create a new Webots controller called “HelloWebots” which prints “Hello Webots!” to the console.
a. Where is this controller saved?
b. What files are created?
c. What is the controller analogous to in the real world?
d. Modify the main function to run a control loop with a refresh rate equal to that of the world simulation.
- Create a new Webots controller called “VelocityControl” which only drives the robot forward once the controller is initiated.
- Create a new Webots controller called “PositionControl” which only drives the robot forward 1 cell once the controller is initiated.
- The following error is common when using the motors in position control. Recreate how this error is caused and what would be the fix?
Error: wb_motor_set_position() called with an invalid 'position' argument (NaN).
- The following error is common when using the motors in position control. Recreate how this error is caused and what would be the fix?
Error: wb_motor_set_velocity() called with negative 'velocity' argument is in position control mode
- Examine this controller. Why does nothing get printed to the console?
#include <iostream>
#include <webots/Robot.hpp>
int main() {
webots::Robot robot;
double timeStep{robot.getBasicTimeStep()};
while (robot.step(timeStep) != -1) {
std::cout << "Hello";
}
}
- Create a new Webots controller called “DistanceSensor”. Verify the distance sensor works by moving the obstacle within the world.