FH Joanneum (Embedded Computing): WS2022/2023 (Questions About Code)

Question 1: C++ Inheritance

Given the following class definitions:

class Base
{
public:
    const char* method() const
    {
        return "I am an instance of class Base";
    }
};

class Derived : public Base
{
public:
    const char* method() const
    {
        return "I am an instance of class Derived";
    }
};

What would the following code output?

Code

Output

Base b;
std::cout << b.method() << std::endl;
Derived d;
std::cout << d.method() << std::endl;
Derived d;
Base* b = &d;
std::cout << b->method() << std::endl;

Question 2: C++ Inheritance

Given the following class definitions:

class Base
{
public:
    virtual const char* method() const
    {
        return "I am an instance of class Base";
    }
};

class Derived : public Base
{
public:
    virtual const char* method() const
    {
        return "I am an instance of class Derived";
    }
};

What would the following code output?

Code

Output

Base b;
std::cout << b.method() << std::endl;
Derived d;
std::cout << d.method() << std::endl;
Derived d;
Base* b = &d;
std::cout << b->method() << std::endl;

Question 3: Memory Leak

The following code contains a memory leak.

  • What constitutes that leak?

  • What would be a fix to the problem?

#include <vector>

class Object
{
public:
    Object(int id) : _id(id) {}
    int id() const { return _id; }
private:
    int _id;
};

class ObjectContainer
{
public:
    void add_object(Object* o) { _container.push_back(o); }

private:
    using _Container = std::vector<Object*>;
    _Container _container;
};

int main()
{
    ObjectContainer container;
    Object* o = new Object(666);
    container.add_object(o);

    return 0;
}

Question 4: std::vector

The following code fragment contains a bug. What is the bug? Describe it.

#include <vector>
#include <iostream>

int main()
{
    std::vector<int> my_vector = { 1,2,3 };
    std::cout << my_vector[3] << std::endl;

    return 0;
}