Course Materials

OOP - Week 4: 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 three files. In Question 2, write the whole program in one file.

Filter:
Sort:

Question 1

Write the class Student in two files, and use it in a third file. Put the default value in the header only.

Student.h (with a header guard) and Student.cpp:

  1. Private data members: a const int id and a double gpa.
  2. Two private static data members: nextId, which starts at 20260001, and count, the number of students that exist right now, which starts at 0.
  3. A constructor with a default parameter g = 0.0. It takes the next id (and moves nextId forward), sets the gpa with setGpa, and increases count.
  4. A copy constructor. A copy is a new student: it takes the next id, gets the gpa of the original, and increases count.
  5. A destructor that decreases count.
  6. setGpa, which accepts only a value from 0.0 to 4.0, the getters getId and getGpa, a function print(), and hasHigherGpa(other), which returns true if this gpa is higher than the gpa of other.
  7. Two static functions, getCount() and getNextId().

Mark every function that only reads the object as const.

main.cpp:

  • Print getCount() and getNextId() before any student exists.
  • Create s1 and s2 with a gpa each, and s3 with no argument. Print the three students, then print getCount().
  • In a block, create s4 and print getCount(). After the block, print getCount() again.
  • Create s5 as a copy of s1. Print it, then print getCount().
  • Create a const student c and print it. Print s1.hasHigherGpa(c).
  • Print getCount() and getNextId() again.
Solution
Student in three filescode/oop/week-04/worksheet/solution-01
#ifndef STUDENT_H
#define STUDENT_H

class Student {
private:
    const int id;
    double gpa;
    static int nextId;
    static int count;

public:
    Student(double g = 0.0);                    // the default value is written here only
    Student(const Student& other);
    ~Student();

    void setGpa(double g);

    int getId() const;
    double getGpa() const;
    void print() const;
    bool hasHigherGpa(const Student& other) const;

    static int getCount();
    static int getNextId();
};

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

int Student::nextId = 20260001;                 // the static members are defined here, once
int Student::count = 0;

Student::Student(double g) : id(nextId++) {
    gpa = 0.0;
    setGpa(g);
    count++;
}

Student::Student(const Student& other) : id(nextId++) {
    gpa = other.gpa;
    count++;
}

Student::~Student() {
    count--;
}

void Student::setGpa(double g) {
    if (g < 0.0 || g > 4.0)
        return;
    gpa = g;
}

int Student::getId() const {
    return id;
}

double Student::getGpa() const {
    return gpa;
}

void Student::print() const {
    cout << "id " << id << ": gpa " << gpa << endl;
}

bool Student::hasHigherGpa(const Student& other) const {
    return gpa > other.gpa;
}

int Student::getCount() {
    return count;
}

int Student::getNextId() {
    return nextId;
}
#include <iostream>
#include "Student.h"
using namespace std;

int main() {
    cout << "count " << Student::getCount() << ", next id " << Student::getNextId() << endl;

    Student s1(3.2);
    Student s2(3.9);
    Student s3;
    s1.print();
    s2.print();
    s3.print();
    cout << "count " << Student::getCount() << endl;

    {
        Student s4(2.5);
        cout << "count " << Student::getCount() << endl;
    }
    cout << "count " << Student::getCount() << endl;

    Student s5 = s1;
    s5.print();
    cout << "count " << Student::getCount() << endl;

    const Student c(3.5);
    c.print();
    cout << s1.hasHigherGpa(c) << endl;

    cout << "count " << Student::getCount() << ", next id " << Student::getNextId() << endl;
    return 0;
}

Question 2

The classes Date and Address are given:

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 Address {
private:
    int building, street;

public:
    Address(int b, int s) {
        building = b;
        street = s;
    }

    int getBuilding() const {
        return building;
    }
    int getStreet() const {
        return street;
    }
};

Write the class Student, and a main that uses it. Everything goes in one file, after the two classes above.

Student has:

  • Private data members: an int id, a Date birthDate and an Address address.
  • A constructor that takes the id, the day, the month, the year, the building and the street. It sets the two member objects in the member initializer list.
  • print(), which prints the id, the birth date and the address. It can only use the public functions of Date and Address.
  • getStreet(), which returns the street of the address.
  • isOlderThan(other), which returns true if this student was born before other. Compare the year first, then the month, then the day.

Mark every function that only reads the object as const.

In the file, write two functions. Each one receives the array and its size:

  • findOldest returns the index of the oldest student.
  • countOnStreet(arr, size, street) returns how many students live on that street.

In main:

  • Create students, an array of 3 students, with an initializer list. Two of them live on the same street.
  • Print the three students.
  • Print the oldest student, and the number of students who live on the street of your choice.
Solution
#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 Address {
private:
    int building, street;

public:
    Address(int b, int s) {
        building = b;
        street = s;
    }

    int getBuilding() const {
        return building;
    }
    int getStreet() const {
        return street;
    }
};

class Student {
private:
    int id;
    Date birthDate;
    Address address;

public:
    Student(int i, int d, int m, int y, int b, int s)
        : id(i), birthDate(d, m, y), address(b, s) {
    }

    void print() const {
        cout << "id " << id
             << ", born " << birthDate.getDay() << "/" << birthDate.getMonth() << "/" << birthDate.getYear()
             << ", building " << address.getBuilding() << ", street " << address.getStreet() << endl;
    }

    int getStreet() const {
        return address.getStreet();
    }

    bool isOlderThan(const Student& other) const {
        if (birthDate.getYear() != other.birthDate.getYear())
            return birthDate.getYear() < other.birthDate.getYear();
        if (birthDate.getMonth() != other.birthDate.getMonth())
            return birthDate.getMonth() < other.birthDate.getMonth();
        return birthDate.getDay() < other.birthDate.getDay();
    }
};

int findOldest(const Student arr[], int size) {
    int oldest = 0;
    for (int i = 1; i < size; i++) {
        if (arr[i].isOlderThan(arr[oldest]))
            oldest = i;
    }
    return oldest;
}

int countOnStreet(const Student arr[], int size, int street) {
    int count = 0;
    for (int i = 0; i < size; i++) {
        if (arr[i].getStreet() == street)
            count++;
    }
    return count;
}

int main() {
    Student students[3] = {
        Student(101, 15, 3, 2005, 12, 5),
        Student(102, 2, 11, 2004, 7, 9),
        Student(103, 28, 6, 2005, 30, 5)
    };

    for (int i = 0; i < 3; i++)
        students[i].print();

    cout << "oldest: ";
    students[findOldest(students, 3)].print();
    cout << "students on street 5: " << countOnStreet(students, 3, 5) << endl;
    return 0;
}

                    

Question 3

Trace the following program. A Car has an Engine, a Radio and a static data member count, the number of cars that exist. Every constructor and destructor prints a message.

#include <iostream>
using namespace std;

class Engine {
private:
    int power;

public:
    Engine(int p = 0) : power(p) {
        cout << "Engine " << power << endl;
    }
    Engine(const Engine& other) : power(other.power) {
        cout << "copy Engine " << power << endl;
    }
    ~Engine() {
        cout << "~Engine " << power << endl;
    }
};

class Radio {
private:
    int volume;

public:
    Radio(int v = 0) : volume(v) {
        cout << "Radio " << volume << endl;
    }
    ~Radio() {
        cout << "~Radio " << volume << endl;
    }
};

class Car {
private:
    Engine engine;
    Radio radio;
    static int count;

public:
    Car(int power, int volume) : engine(power), radio(volume) {
        count++;
        cout << "Car, count " << count << endl;
    }
    Car(const Car& other) : engine(other.engine) {
        count++;
        cout << "copy Car, count " << count << endl;
    }
    ~Car() {
        count--;
        cout << "~Car, count " << count << endl;
    }

    void drive() const {
        cout << "driving" << endl;
    }
    static int getCount() {
        return count;
    }
};

int Car::count = 0;

void service(Car c) {
    c.drive();
}

int main() {
    Car car1(100, 5);
    {
        Car car2(150, 8);
        service(car1);
    }
    cout << "count " << Car::getCount() << endl;
    Car car3 = car1;
    cout << "end" << endl;
    return 0;
}

Write the exact output of the program. Do not run it before you have written your answer.

Solution
Engine 100
Radio 5
Car, count 1
Engine 150
Radio 8
Car, count 2
copy Engine 100
Radio 0
copy Car, count 3
driving
~Car, count 2
~Radio 0
~Engine 100
~Car, count 1
~Radio 8
~Engine 150
count 1
copy Engine 100
Radio 0
copy Car, count 2
end
~Car, count 1
~Radio 0
~Engine 100
~Car, count 0
~Radio 5
~Engine 100

                    

Question 4

The class Student keeps a static counter. count is declared in Student.h and defined in Student.cpp:

Student with a static counter in three filescode/oop/week-04/worksheet/question-04
has a note
#ifndef STUDENT_H
#define STUDENT_H

class Student {
private:
    static int count;
public:
    Student();
    ~Student();
    static int getCount();
};

#endif
#include "Student.h"

int Student::count = 0;

Student::Student() { count++; }
Student::~Student() { count--; }
int Student::getCount() { return count; }
#include <iostream>
#include "Student.h"
using namespace std;

int main() {
    Student a, b;
    cout << Student::getCount() << endl;
    return 0;
}

Answer the question below, then press Check answers.

Where does the definition belong? 0 / 1 answered

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

1.int Student::count = 0;line 3 of the program

Why is this line written in Student.cpp and not in Student.h?

Answer for int Student::count = 0;