Course Materials

OOP - Week 6: 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 of Vehicle is given, so write the rest in three files.

Filter:
Sort:

Question 1

Trace the following program. ElectricCar and SportsCar are both derived from Car. Every constructor and destructor prints a message.

#include <iostream>
using namespace std;

class Car {
private:
    int speed;

public:
    Car(int s) : speed(s) {
        cout << "Car " << speed << endl;
    }
    Car(const Car& other) : speed(other.speed) {
        cout << "copy Car " << speed << endl;
    }
    ~Car() {
        cout << "~Car " << speed << endl;
    }
};

class ElectricCar : public Car {
private:
    int battery;

public:
    ElectricCar(int s, int b) : Car(s), battery(b) {
        cout << "ElectricCar " << battery << endl;
    }
    ElectricCar(const ElectricCar& other) : Car(other), battery(other.battery) {
        cout << "copy ElectricCar " << battery << endl;
    }
    ~ElectricCar() {
        cout << "~ElectricCar " << battery << endl;
    }
};

class SportsCar : public Car {
private:
    int power;

public:
    SportsCar(int s, int p) : Car(s), power(p) {
        cout << "SportsCar " << power << endl;
    }
    ~SportsCar() {
        cout << "~SportsCar " << power << endl;
    }
};

int main() {
    Car c(80);
    {
        ElectricCar e(60, 300);
        ElectricCar f = e;
    }
    SportsCar s(200, 450);
    cout << "end" << endl;
    return 0;
}

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

Solution
Car 80
Car 60
ElectricCar 300
copy Car 60
copy ElectricCar 300
~ElectricCar 300
~Car 60
~ElectricCar 300
~Car 60
Car 200
SportsCar 450
end
~SportsCar 450
~Car 200
~Car 80

                    

Question 2

The class Vehicle has a plate and a speed. Its header is given:

#ifndef VEHICLE_H
#define VEHICLE_H

#include <string>
using namespace std;

class Vehicle {
private:
    string plate;
    int speed;

public:
    Vehicle(const string& p, int s);
    void accelerate(int amount);
    int getSpeed() const;
    string getPlate() const;
    void print() const;
};

#endif

Write the class Bus, which is a Vehicle with seats and passengers, in Bus.h and Bus.cpp, and use it in main.cpp. Assume that Vehicle.cpp is written.

Bus.h and Bus.cpp:

  1. Private data members: seats and passengers, both int.
  2. A constructor that takes the plate, the speed and the number of seats. It builds the Vehicle part with the plate and the speed, and starts with no passengers.
  3. A copy constructor that makes a copy of the whole bus.
  4. A destructor that prints bus PLATE is gone, using the plate of the bus.
  5. board(count) adds count passengers only if all of them fit in the free seats. It returns true if they were added, and false otherwise.
  6. isFull() returns true if every seat is taken.
  7. print() prints the vehicle line, using the print of Vehicle, and then a second line with the seats and the passengers.

Mark every function that only reads the object as const.

main.cpp:

  • Write a function countFull that receives an array of buses and its size, and returns how many buses are full.
  • Create an array of 3 buses.
  • Board passengers so that one bus becomes full. Try to board more passengers than a bus has seats, and print a message when board returns false.
  • Print the three buses, and print the number of full buses.
Solution
Bus derived from Vehiclecode/oop/week-06/worksheet/solution-02
#ifndef VEHICLE_H
#define VEHICLE_H

#include <string>
using namespace std;

class Vehicle {
private:
    string plate;
    int speed;

public:
    Vehicle(const string& p, int s);
    void accelerate(int amount);
    int getSpeed() const;
    string getPlate() const;
    void print() const;
};

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

Vehicle::Vehicle(const string& p, int s) : plate(p), speed(0) {
    if (s > 0)
        speed = s;
}

void Vehicle::accelerate(int amount) {
    if (amount > 0)
        speed += amount;
}

int Vehicle::getSpeed() const {
    return speed;
}

string Vehicle::getPlate() const {
    return plate;
}

void Vehicle::print() const {
    cout << "vehicle " << plate << ", speed " << speed << endl;
}
#ifndef BUS_H
#define BUS_H

#include "Vehicle.h"

class Bus : public Vehicle {
private:
    int seats;
    int passengers;

public:
    Bus(const string& p, int s, int n);
    Bus(const Bus& other);
    ~Bus();
    bool board(int count);
    bool isFull() const;
    void print() const;
};

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

Bus::Bus(const string& p, int s, int n) : Vehicle(p, s), seats(n), passengers(0) {
}

Bus::Bus(const Bus& other) : Vehicle(other), seats(other.seats), passengers(other.passengers) {
}

Bus::~Bus() {
    cout << "bus " << getPlate() << " is gone" << endl;
}

bool Bus::board(int count) {
    if (count > 0 && passengers + count <= seats) {
        passengers += count;
        return true;
    }
    return false;
}

bool Bus::isFull() const {
    return passengers == seats;
}

void Bus::print() const {
    Vehicle::print();
    cout << "seats " << seats << ", passengers " << passengers << endl;
}
#include <iostream>
#include "Bus.h"
using namespace std;

int countFull(const Bus buses[], int size) {
    int count = 0;
    for (int k = 0; k < size; k++)
        if (buses[k].isFull())
            count++;
    return count;
}

int main() {
    Bus buses[3] = {Bus("AB1", 60, 2), Bus("CD2", 50, 3), Bus("EF3", 40, 4)};   // no default constructor: each bus gets its arguments

    buses[0].board(2);
    buses[1].board(2);
    if (!buses[2].board(5))
        cout << "not enough seats" << endl;

    for (int k = 0; k < 3; k++)
        buses[k].print();
    cout << "full buses: " << countFull(buses, 3) << endl;

    return 0;
}

Question 3

Car inherits Vehicle with protected inheritance. Vehicle has a private, a protected and a public member:

class Vehicle {
private:
    int plate;
protected:
    int wheels;
public:
    int speed;
};

class Car : protected Vehicle {
public:
    void test() {
        speed = 50;
        wheels = 4;
        plate = 7;
    }
};

int main() {
    Car c;
    c.speed = 80;
    return 0;
}

For each line below, say whether it is allowed. Press Check answers when you are done.

Is this line allowed? 0 / 4 answered

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

1.speed = 50;line 13 of the program

This line is inside a function of Car, which inherits Vehicle with protected inheritance. Is it allowed?

Answer for speed = 50;
2.wheels = 4;line 14 of the program

The same function of Car. Is this line allowed?

Answer for wheels = 4;
3.plate = 7;line 15 of the program

The same function of Car. Is this line allowed?

Answer for plate = 7;
4.c.speed = 80;line 21 of the program

This line is in main, which is outside code. Is it allowed?

Answer for c.speed = 80;