OOP - Week 3: Programming Questions
Note: Try to solve these questions on your own before checking the solutions. If you are stuck, reread Lecture 1, Lecture 2 and Lecture 3. In Question 1, write the whole program in one file. In Question 2, write three files.
Question 1
Write a complete class Date with the following:
- Three private data members,
day,monthandyear, allint. - A setter and a getter for each member. A setter ignores an invalid value: a day must be from 1 to 31, a month from 1 to 12, and a year must be at least 1.
- One constructor with default parameters,
day = 1,month = 1andyear = 2000. It uses the setters, so an invalid value is ignored and that member keeps its safe starting value. - A function
print()that prints the date. - A function
isEqual(other)that takes anotherDateand returnstrueif both dates have the same day, month and year. - A function
later(other)that takes anotherDateand returns the later of the two dates as a newDate. If the dates are equal, return either one. - A function
addMonths(months)with the default value 1. It returns a newDatethat is that many months later. The day stays the same, and the year goes up when the month passes 12. The date it was called on is not changed. - A function
monthsBetween(other)that returns the number of months between the two dates as a positive number. Ignore the days.
Pass the other date by const reference, and read its data members directly.
In main:
- Create
d1,d2,d3andd4.d1andd2have the same values,d3has different values, andd4has no arguments. Print all four. - Print
d1.isEqual(d2)andd1.isEqual(d3). - Call
print()ond1.later(d3). - Create
d5withd1.addMonths()andd6withd1.addMonths(n), wherenis a number of your choice that putsd6in the next year. Printd1,d5andd6. - Print
d1.monthsBetween(d3)andd3.monthsBetween(d1).
Question 2
Write the class Employee in two files, and use it in a third file. Put the default values in the header only.
Employee.h (with a header guard) and Employee.cpp:
- Private data members:
id(int) andsalary(double). - A constructor with default parameters (
id = 0,salary = 0.0) that uses the setters. - Setters and getters, named
setId,setSalary,getIdandgetSalary.setIdaccepts only a positive value, andsetSalaryaccepts only zero or more. raise(percent)increases the salary by that percent.percentis adouble, and its default is 10.earnsMore(other)returnstrueif this salary is higher than the salary ofother.print()prints the id and the salary.
main.cpp has these functions. Each one receives the array and its size:
totalSalaryreturns the sum of the salaries as adouble.findById(arr, size, id)returns the index of that id, or -1 if it is not found.raiseAll(arr, size, percent)raises every employee. The default percent is 10.printAllprints every employee.
In main:
- Create
staff, an array of 5 employees. Give the first three an id and a salary with constructor calls, and leave the last two to the constructorβs default values. - Use the setters on
staff[3](a valid id and a valid salary) and onstaff[4](a valid id and a negative salary). - Print all the employees.
- Print
staff[0].earnsMore(staff[1]), the total salary, andfindByIdfor an id that exists and for one that does not. - Call
staff[1].raise()with no argument and printstaff[1]. Then callraiseAllwith a percent of your choice, and print the total salary.
Question 3
Trace the following program. The class Coin has a constructor with a default parameter, written with a member initializer list, and a copy constructor. Every constructor and the destructor print a message. Assume that the compiler never makes an extra copy of a temporary object: not when it creates the array elements, not when twice returns, and not when the result initializes c. The only copy is the one made by passing an object by value.
#include <iostream>
using namespace std;
class Coin {
private:
int value;
public:
Coin(int v = 1) : value(v) {
cout << "make " << value << endl;
}
Coin(const Coin& other) : value(other.value) {
cout << "copy " << value << endl;
}
~Coin() {
cout << "free " << value << endl;
}
int getValue() {
return value;
}
bool isBigger(const Coin& other) {
return value > other.value;
}
Coin twice() {
return Coin(value * 2);
}
};
void byValue(Coin c) {
cout << "byValue " << c.getValue() << endl;
}
void byRef(Coin& c) {
cout << "byRef " << c.getValue() << endl;
}
int main() {
Coin a(5);
Coin b;
Coin wallet[3] = { Coin(10), Coin(20) };
byValue(a);
byRef(b);
if (b.isBigger(a))
cout << "b is bigger" << endl;
else
cout << "b is not bigger" << endl;
Coin c = a.twice();
cout << "end" << endl;
return 0;
}Write the exact output of the program. Do not run it before you have written your answer.