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 5Ticket a(5)printsopen 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 printsopen 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])copiesrow[1]into the parametert, so the number is 8 + 10 = 18. The parameter is destroyed whencheckends, soclose 18comes before the end of the block.- The block ends and
bis destroyed:close 15. cuses the default value 1. At the end ofmainthe objects are destroyed in the reverse order of their creation:c, then the array elements from the last to the first, thena.
Question 2
| Statement | Compiles? | Reason |
|---|---|---|
| (1) | No | empty() is a const function, so it cannot change cash. |
| (2) | No | bonus() is static, so it has no object. It cannot use the non-static member cash by its name only. |
| (3) | Yes | getCash() is a const function, so it can be called through a const reference. |
| (4) | No | w is a const reference, and add is not a const function. |
| (5) | Yes | w2 is not const. |
| (6) | No | w1 is a const object, and add is not a const function. |
| (7) | Yes | getId() is a const function. |
| (8) | Yes | A static function is called with the class name. |
| (9) | Yes | A static function can also be called through an object, even a const one. |
| (10) | No | getId() is not static, so it needs an object. |
| (11) | No | Wallet has no default constructor, so w3 cannot be created without arguments. The const is not the reason: Wallet w; fails too. |
| (12) | No | id 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 2Question 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: 59Question 5
This question is checked on the exam page. Press Check answers under each part.