Course Materials

Mockup Midterm Solutions 1

Question 1

Output:

open 5
open 7
open 8
open 1
dup 15
dup 18
check 18
close 18
close 15
open 1
end
close 1
close 1
close 8
close 7
close 5
  • Ticket a(5) prints open 5. The array has 3 elements: the first two are created with 7 and 8, and the third one uses the default value 1, so it prints open 1 (the order of the elements is the order of the index).
  • Ticket b = a; is a copy, so the copy constructor runs, and the number is 5 + 10 = 15.
  • check(row[1]) copies row[1] into the parameter t, so the number is 8 + 10 = 18. The parameter is destroyed when check ends, so close 18 comes before the end of the block.
  • The block ends and b is destroyed: close 15.
  • c uses the default value 1. At the end of main the objects are destroyed in the reverse order of their creation: c, then the array elements from the last to the first, then a.

Question 2

StatementCompiles?Reason
(1)Noempty() is a const function, so it cannot change cash.
(2)Nobonus() is static, so it has no object. It cannot use the non-static member cash by its name only.
(3)YesgetCash() is a const function, so it can be called through a const reference.
(4)Now is a const reference, and add is not a const function.
(5)Yesw2 is not const.
(6)Now1 is a const object, and add is not a const function.
(7)YesgetId() is a const function.
(8)YesA static function is called with the class name.
(9)YesA static function can also be called through an object, even a const one.
(10)NogetId() is not static, so it needs an object.
(11)NoWallet has no default constructor, so w3 cannot be created without arguments. The const is not the reason: Wallet w; fails too.
(12)Noid is a private member (and it is const), so main cannot change it.

Question 3

#include <iostream>
using namespace std;

class Date {
private:
    int day, month, year;

public:
    Date(int d, int m, int y) : day(d), month(m), year(y) {
    }

    int getDay() const {
        return day;
    }
    int getMonth() const {
        return month;
    }
    int getYear() const {
        return year;
    }
};

class Book {
private:
    const int id;
    Date published;
    double price;
    static int count;

public:
    Book(int i, int d, int m, int y, double p) : id(i), published(d, m, y) {
        price = 0.0;
        setPrice(p);
        count++;
    }
    ~Book() {
        count--;
    }

    void setPrice(double p) {
        if (p > 0.0)
            price = p;
    }

    bool isNewerThan(const Book& other) const {
        if (published.getYear() != other.published.getYear())
            return published.getYear() > other.published.getYear();
        if (published.getMonth() != other.published.getMonth())
            return published.getMonth() > other.published.getMonth();
        return published.getDay() > other.published.getDay();
    }

    void print() const {
        cout << "book " << id << ": " << published.getDay() << "/" << published.getMonth() << "/"
             << published.getYear() << ", price " << price << endl;
    }

    static int getCount() {
        return count;
    }
};

int Book::count = 0;

int main() {
    Book b1(1, 10, 5, 2020, 25.5);
    Book b2(2, 3, 8, 2019, 40.0);
    b1.print();
    b2.print();
    cout << b1.isNewerThan(b2) << endl;
    cout << b2.isNewerThan(b1) << endl;

    cout << "count " << Book::getCount() << endl;
    {
        Book b3(3, 1, 1, 2021, -5.0);
        b3.print();
        cout << "count " << Book::getCount() << endl;
    }
    cout << "count " << Book::getCount() << endl;
    return 0;
}

With the values of this solution, the output is:

book 1: 10/5/2020, price 25.5
book 2: 3/8/2019, price 40
1
0
count 2
book 3: 1/1/2021, price 0
count 3
count 2

Question 4

#include <iostream>
using namespace std;

class Rectangle {
private:
    int width, height;

public:
    Rectangle(int w = 1, int h = 1) {
        width = w;
        height = h;
    }

    int area() const {
        return width * height;
    }

    void print() const {
        cout << width << " x " << height << endl;
    }
};

int largestIndex(const Rectangle arr[], int size) {
    int best = 0;
    for (int i = 1; i < size; i++) {
        if (arr[i].area() > arr[best].area())
            best = i;
    }
    return best;
}

int totalArea(const Rectangle arr[], int size) {
    int total = 0;
    for (int i = 0; i < size; i++)
        total += arr[i].area();
    return total;
}

int main() {
    Rectangle rects[4] = { Rectangle(3, 4), Rectangle(5, 2), Rectangle(6, 6) };

    for (int i = 0; i < 4; i++)
        rects[i].print();

    int big = largestIndex(rects, 4);
    cout << "largest: " << big << endl;
    cout << "total area: " << totalArea(rects, 4) << endl;
    return 0;
}

With the values of this solution, the output is:

3 x 4
5 x 2
6 x 6
1 x 1
largest: 2
total area: 59

Question 5

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