Course Materials

Mockup Final Solutions 1

Question 1

Output:

Media 1
Song
Media 2
Video
Media 3
info 1
play song 1
info 2
play video 2
info 3
play media 3
~Video
~Media 2
~Song
~Media 1
~Media 3
Media 4
Song
info 4
play song 4
end
~Song
~Media 4

Question 2

StatementCompiles?Reason
(1)Yessize is protected, so a derived class can use it.
(2)Noid is private in Shape, so Circle cannot use it.
(3)YesA reference of the base class can refer to a derived object.
(4)NoA Circle* cannot hold the address of a Shape, because not every shape is a circle.
(5)Nop is a Shape*, and Shape has no radiusUp. The compiler looks at the type of the pointer.
(6)Yesarea() is a function of Shape. It is virtual, so the version of Circle runs.
(7)Nonew Shape[3] needs a default constructor, and Shape does not have one.
(8)Nosize is protected, so main cannot use it.
(9)No3 * a needs an operator* with an int on the left. A member operator has the object on the left.
(10)Yesa * 2 calls a.operator*(2).
(11)NoOnly the prefix operator++ is written. a++ needs the postfix version, with the extra int parameter.
(12)NoPoint has no operator>>.

Question 3

#include <iostream>
using namespace std;

class Team {
private:
    const int id;
    int* points;
    int size;

public:
    Team(int i = 0, int n = 1) : id(i) {
        size = (n < 1) ? 1 : n;
        points = new int[size];
        for (int k = 0; k < size; k++)
            points[k] = 0;
    }
    Team(const Team& other) : id(other.id) {
        size = other.size;
        points = new int[size];
        for (int k = 0; k < size; k++)
            points[k] = other.points[k];
    }
    ~Team() {
        delete[] points;
    }

    Team& setPoints(int k, int p) {
        if (k >= 0 && k < size && p >= 0)
            points[k] = p;
        return *this;
    }
    int total() const {
        int sum = 0;
        for (int k = 0; k < size; k++)
            sum += points[k];
        return sum;
    }
    bool operator<(const Team& other) const {
        return total() < other.total();
    }
    friend ostream& operator<<(ostream& out, const Team& t);
};

ostream& operator<<(ostream& out, const Team& t) {
    out << "Team " << t.id << ": " << t.total();
    return out;
}

int main() {
    Team a(1, 3);
    Team b(2, 3);
    a.setPoints(0, 10).setPoints(1, 5).setPoints(2, 7);
    b.setPoints(0, 20).setPoints(1, -4).setPoints(5, 9);

    cout << a << endl;
    cout << b << endl;
    if (b < a)
        cout << "Team 2 has fewer points" << endl;

    Team c = a;
    c.setPoints(0, 0);
    cout << a << endl;
    cout << c << endl;
    return 0;
}

With the values of this solution, the output is:

Team 1: 22
Team 2: 20
Team 2 has fewer points
Team 1: 22
Team 1: 12

Question 4

#include <iostream>
using namespace std;

class Shape {
protected:
    int id;

public:
    Shape(int i) : id(i) { }
    virtual ~Shape() { }
    virtual double area() const {
        return 0;
    }
    void print() const {
        cout << "shape " << id << ": " << area() << endl;
    }
};

class Rectangle : public Shape {
private:
    int width, height;

public:
    Rectangle(int i, int w, int h) : Shape(i), width(w), height(h) { }
    double area() const override {
        return width * height;
    }
};

class Square : public Rectangle {
public:
    Square(int i, int side) : Rectangle(i, side, side) { }
};

class Triangle : public Shape {
private:
    int base, height;

public:
    Triangle(int i, int b, int h) : Shape(i), base(b), height(h) { }
    double area() const override {
        return 0.5 * base * height;
    }
};

double totalArea(Shape* shapes[], int n) {
    double sum = 0;
    for (int k = 0; k < n; k++)
        sum += shapes[k]->area();
    return sum;
}

int main() {
    Shape* shapes[3];
    shapes[0] = new Rectangle(1, 4, 5);
    shapes[1] = new Square(2, 3);
    shapes[2] = new Triangle(3, 6, 4);

    for (int k = 0; k < 3; k++)
        shapes[k]->print();
    cout << "total area: " << totalArea(shapes, 3) << endl;

    for (int k = 0; k < 3; k++)
        delete shapes[k];
    return 0;
}

print() is not virtual, but it calls area(), which is virtual. So it prints the area of the real object. With the values of this solution, the output is:

shape 1: 20
shape 2: 9
shape 3: 12
total area: 41

Question 5

This question is checked on the exam page. Press Check answers under each part.