OOP - Week 5: 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 2, the header is given, so write two files.
Question 1
Trace the following program. The constructor and the destructor of Student print a message. Some objects are created on the heap, and list is an array of students that is also created on the heap.
#include <iostream>
using namespace std;
class Student {
private:
int id;
public:
Student(int i = 0) : id(i) {
cout << "constructor " << id << endl;
}
~Student() {
cout << "destructor " << id << endl;
}
void setId(int i) {
id = i;
}
};
void enroll() {
Student s(5);
Student* p = new Student(6);
cout << "in enroll" << endl;
}
int main() {
Student a(1);
Student* p = new Student(2);
Student* list = new Student[2];
list[1].setId(7);
delete p;
p = nullptr;
delete p;
enroll();
cout << "after enroll" << endl;
delete[] list;
cout << "end" << endl;
return 0;
}Write the exact output of the program. Do not run it before you have written your answer. Then answer a second question: which student is never destroyed, and which line would you add to fix it?
Question 2
A Student keeps one grade for each subject in an array on the heap. The header is given:
#ifndef STUDENT_H
#define STUDENT_H
class Student {
private:
int id;
int numSubjects;
double* grades;
public:
Student(int i, int n);
Student(const Student& other);
~Student();
void setGrade(int index, double value);
void removeSubject(int index);
void removeSubjectsWithGrade(double grade);
void print() const;
};
#endifWrite Student.cpp and main.cpp.
Student.cpp:
- The constructor creates the array with
new[]and sets every grade to 0. - The copy constructor makes a deep copy.
- The destructor releases the array.
setGradeaccepts an index only if it is valid, and a grade only from 0 to 100.removeSubject(index)is for a student who withdraws from one subject. If the index is not valid, nothing changes. Otherwise create a new, smaller array, copy every grade except the removed one, release the old array, and make the pointer point to the new array.removeSubjectsWithGrade(grade)removes every subject that has this grade. If there is none, nothing changes. Count the matches first, so the new array has the right size.print()prints the id and every grade on one line.
main.cpp:
- Write a function
showthat receives aStudentby value and prints it. - Create a student
awith at least 4 subjects and give each subject a grade, with the same grade twice. Createbas a copy ofa, remove one subject frombby its index, and then remove every subject that has the repeated grade. Printaandb, and callshow(a). - Create a student on the heap, remove one subject from it, print it, and delete it.
Question 3
A Student keeps its grades in an array on the heap, and grades is the pointer to it. Here are its copy constructor and its destructor. a and b are created in main:
Student::Student(const Student& other) {
id = other.id;
numSubjects = other.numSubjects;
grades = other.grades;
}
Student::~Student() {
delete[] grades;
}
int main() {
Student a(1, 3);
Student b = a;
b.setGrade(0, 50);
return 0;
}Answer the three questions below, then press Check answers.
This question needs JavaScript to check your answers. You can still read the program and work out the answers on paper.
grades = other.grades;line 4 of the programAfter Student b = a; runs the copy constructor, what is true about the grades of a and b?
delete[] grades;line 8 of the programa and b are destroyed at the closing brace of main. What happens when the two destructors run this line?
b.setGrade(0, 50);line 14 of the programThe setGrade function changes the first grade of the array that the object points to. What happens to a?