Course Materials

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.

Filter:
Sort:

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?

Solution
constructor 1
constructor 2
constructor 0
constructor 0
destructor 2
constructor 5
constructor 6
in enroll
destructor 5
after enroll
destructor 7
destructor 0
end
destructor 1

                    
Solution: the student that is never destroyed
Student 6 is never destroyed. It was created with new inside enroll, and the pointer p is destroyed at the end of the function, so the address is lost and delete can never be called.

Fix: add "delete p;" at the end of enroll.

                    

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;
};

#endif

Write Student.cpp and main.cpp.

Student.cpp:

  1. The constructor creates the array with new[] and sets every grade to 0.
  2. The copy constructor makes a deep copy.
  3. The destructor releases the array.
  4. setGrade accepts an index only if it is valid, and a grade only from 0 to 100.
  5. 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.
  6. 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.
  7. print() prints the id and every grade on one line.

main.cpp:

  • Write a function show that receives a Student by value and prints it.
  • Create a student a with at least 4 subjects and give each subject a grade, with the same grade twice. Create b as a copy of a, remove one subject from b by its index, and then remove every subject that has the repeated grade. Print a and b, and call show(a).
  • Create a student on the heap, remove one subject from it, print it, and delete it.
Solution
Student with a dynamic array in three filescode/oop/week-05/worksheet/solution-02
#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;
};

#endif
#include <iostream>
#include "Student.h"
using namespace std;

Student::Student(int i, int n) : id(i), numSubjects(n), grades(new double[n]) {
    for (int k = 0; k < numSubjects; k++)
        grades[k] = 0.0;
}

Student::Student(const Student& other)
    : id(other.id), numSubjects(other.numSubjects), grades(new double[other.numSubjects]) {
    for (int k = 0; k < numSubjects; k++)
        grades[k] = other.grades[k];
}

Student::~Student() {
    delete[] grades;
}

void Student::setGrade(int index, double value) {
    if (index >= 0 && index < numSubjects && value >= 0.0 && value <= 100.0)
        grades[index] = value;
}

void Student::removeSubject(int index) {
    if (index < 0 || index >= numSubjects)
        return;
    double* smaller = new double[numSubjects - 1];
    int next = 0;
    for (int k = 0; k < numSubjects; k++)
        if (k != index)
            smaller[next++] = grades[k];
    delete[] grades;
    grades = smaller;
    numSubjects--;
}

void Student::removeSubjectsWithGrade(double grade) {
    int count = 0;
    for (int k = 0; k < numSubjects; k++)
        if (grades[k] == grade)
            count++;
    if (count == 0)
        return;
    double* smaller = new double[numSubjects - count];
    int next = 0;
    for (int k = 0; k < numSubjects; k++)
        if (grades[k] != grade)
            smaller[next++] = grades[k];
    delete[] grades;
    grades = smaller;
    numSubjects -= count;
}

void Student::print() const {
    cout << "student " << id << ":";
    for (int k = 0; k < numSubjects; k++)
        cout << " " << grades[k];
    cout << endl;
}
#include <iostream>
#include "Student.h"
using namespace std;

void show(Student s) {
    s.print();
}

int main() {
    Student a(1, 4);
    a.setGrade(0, 90);
    a.setGrade(1, 80);
    a.setGrade(2, 70);
    a.setGrade(3, 80);

    Student b = a;
    b.removeSubject(0);
    b.removeSubjectsWithGrade(80);
    a.print();
    b.print();
    show(a);

    Student* p = new Student(2, 3);
    p->setGrade(0, 55);
    p->setGrade(1, 65);
    p->setGrade(2, 75);
    p->removeSubject(2);
    p->print();
    delete p;
    p = nullptr;

    return 0;
}

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.

What does the copy constructor do? 0 / 3 answered

This question needs JavaScript to check your answers. You can still read the program and work out the answers on paper.

1.grades = other.grades;line 4 of the program

After Student b = a; runs the copy constructor, what is true about the grades of a and b?

Answer for grades = other.grades;
2.delete[] grades;line 8 of the program

a and b are destroyed at the closing brace of main. What happens when the two destructors run this line?

Answer for delete[] grades;
3.b.setGrade(0, 50);line 14 of the program

The setGrade function changes the first grade of the array that the object points to. What happens to a?

Answer for b.setGrade(0, 50);