OOP - Week 2: 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 Questions 1 and 2, write the whole program in one file and compare your output with the solution.
Question 1
Write a complete class Rectangle with the following:
- Two private data members,
widthandheight, bothint. - A setter and a getter for each member. A setter ignores a value that is zero or negative.
- A default constructor that sets both members to 1 and prints
default constructor. - A parameterized constructor (
widthandheight) that uses the setters, so an invalid value is ignored and that member stays 1. It printsconstructor WxHafter the setters have run, so the numbers are the stored ones. - A copy constructor that prints
copy constructor WxH. - A destructor that prints
destructor WxH. - Member functions
area(),perimeter()andprint(). The functionprint()printsWxH area A perimeter P.
In main:
- Create
r1with the default constructor,r2with a width and a height of your choice,r3as a copy ofr2, andr4with an invalid width and a valid height. - Set a new width and height for
r1, and change the height ofr3. - Print the four rectangles with
print(). - Then print
r2 is W wide and H highusing the getters ofr2.
Run your program and look at the output: why is r2 unchanged after you changed the height of r3? In which order are the destructors called, and why?
Question 2
Every student has exactly 5 subjects. Declare const int SUBJECTS = 5; before the class and use it everywhere instead of the number 5.
Write a complete class Student with the following:
- Two private data members:
id(int) and an arraygradesofSUBJECTSintegers. - A setter
setId, which accepts only a positive value, and a settersetGrade(subject, grade), which accepts only a subject from 0 to 4 and a grade from 0 to 100. A setter ignores an invalid value. - A second setter
setGrades(marks, count)that receives an array of grades and its size. It sets the subjects one by one by callingsetGrade, so every grade is checked, and it never goes past the 5 subjects. - The getters
getId()andgetGrade(subject). MakegetGradereturn -1 for an invalid subject. - One constructor with default parameters,
id = 1andgrade = 0. It gives the student that id, sets all five grades to the given grade using the setters, and printsconstructor ID. It must work with no arguments, with one argument, and with two arguments. - A copy constructor that copies the id and every element of the array, and prints
copy constructor ID. A destructor that printsdestructor ID. - A function
average()that returns the average of the five grades as adouble(use a cast todouble, as you did in Week 1, so the division is not an integer division), a functionlowest()that returns the lowest grade, and a functionhighest()that returns the highest grade. - A function
print()that printsid I, grades G G G G G, average A, lowest L, highest H. Print the average with a plaincout, withoutsetprecision.
In main:
- Create
s1with no arguments,s2ands3with an id and a grade of your choice,s4as a copy ofs3, ands5with only an id (one argument). - Set a grade in two subjects of
s1. - Try
s2.setGradewith an invalid subject. - Create an array of five grades where one grade is above 100, and give it to
s2withsetGrades. - Set a grade in the last subject of
s3. Change the id ofs4, and change its grade in subject 0. - Print the first four students with
print(). - Print the last grade of
s3usinggetGrade. Print the id ofs5and its grade in a subject that does not exist, usinggetIdandgetGrade.
Run your program and look at the output: why does s2 keep its old grade in the position of the invalid grade after setGrades? And you changed the last grade of s3 after s4 was copied from it, so why does s4 still have the old grade?
Question 3
Trace the following program. It uses two classes: Box has a default constructor, a parameterized constructor and a copy constructor, and Tag has one constructor with default parameters, written with a member initializer list, and a copy constructor. Every constructor and destructor prints a message.
#include <iostream>
using namespace std;
class Box {
private:
int size;
public:
Box() {
size = 1;
cout << "Box default" << endl;
}
Box(int s) {
size = s;
cout << "Box param " << size << endl;
}
Box(const Box& other) {
size = other.size;
cout << "Box copy " << size << endl;
}
~Box() {
cout << "Box destroy " << size << endl;
}
int getSize() {
return size;
}
};
class Tag {
private:
int id, level;
public:
Tag(int i = 0, int l = 5) : id(i), level(l) {
cout << "Tag " << id << "," << level << endl;
}
Tag(const Tag& other) : id(other.id), level(other.level) {
cout << "Tag copy " << id << endl;
}
~Tag() {
cout << "Tag destroy " << id << endl;
}
};
void take(Box b) {
cout << "take " << b.getSize() << endl;
}
void look(Box& b) {
cout << "look " << b.getSize() << endl;
}
int main() {
Box a;
Box b(7);
Box c = b;
Tag t1;
Tag t2(3);
Tag t3(4, 9);
Tag t4 = t3;
take(b);
look(c);
cout << "end" << endl;
return 0;
}- Write the exact output of the program. Do not run it before you have written your answer.
- For every statement in
mainthat creates an object or calls a function with an object, choose the constructor that runs.
This question needs JavaScript to check your answers. You can still read the program and work out the answers on paper.
Box a;line 54 of the programBox b(7);line 55 of the programBox c = b;line 56 of the programTag t1;line 57 of the programTag t2(3);line 58 of the programTag t3(4, 9);line 59 of the programTag t4 = t3;line 60 of the programtake(b);line 61 of the programlook(c);line 62 of the program