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.
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:
- Private data members: a const
int idand adouble gpa. - Two private static data members:
nextId, which starts at 20260001, andcount, the number of students that exist right now, which starts at 0. - A constructor with a default parameter
g = 0.0. It takes the next id (and movesnextIdforward), sets the gpa withsetGpa, and increasescount. - A copy constructor. A copy is a new student: it takes the next id, gets the gpa of the original, and increases
count. - A destructor that decreases
count. setGpa, which accepts only a value from 0.0 to 4.0, the gettersgetIdandgetGpa, a functionprint(), andhasHigherGpa(other), which returnstrueif this gpa is higher than the gpa ofother.- Two static functions,
getCount()andgetNextId().
Mark every function that only reads the object as const.
main.cpp:
- Print
getCount()andgetNextId()before any student exists. - Create
s1ands2with a gpa each, ands3with no argument. Print the three students, then printgetCount(). - In a block, create
s4and printgetCount(). After the block, printgetCount()again. - Create
s5as a copy ofs1. Print it, then printgetCount(). - Create a
conststudentcand print it. Prints1.hasHigherGpa(c). - Print
getCount()andgetNextId()again.
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, aDate birthDateand anAddress 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 ofDateandAddress.getStreet(), which returns the street of the address.isOlderThan(other), which returnstrueif this student was born beforeother. 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:
findOldestreturns 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.
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.
Question 4
The class Student keeps a static counter. count is declared in Student.h and defined in Student.cpp:
#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.
This question needs JavaScript to check your answers. You can still read the program and work out the answers on paper.
int Student::count = 0;line 3 of the programWhy is this line written in Student.cpp and not in Student.h?