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
Vehicleis given, so write the rest in three files.
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.
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;
};
#endifWrite 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:
- Private data members:
seatsandpassengers, bothint. - A constructor that takes the plate, the speed and the number of seats. It builds the
Vehiclepart with the plate and the speed, and starts with no passengers. - A copy constructor that makes a copy of the whole bus.
- A destructor that prints
bus PLATE is gone, using the plate of the bus. board(count)addscountpassengers only if all of them fit in the free seats. It returnstrueif they were added, andfalseotherwise.isFull()returnstrueif every seat is taken.print()prints the vehicle line, using theprintofVehicle, 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
countFullthat 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
boardreturnsfalse. - Print the three buses, and print the number of full buses.
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.
This question needs JavaScript to check your answers. You can still read the program and work out the answers on paper.
speed = 50;line 13 of the programThis line is inside a function of Car, which inherits Vehicle with protected inheritance. Is it allowed?
wheels = 4;line 14 of the programThe same function of Car. Is this line allowed?
plate = 7;line 15 of the programThe same function of Car. Is this line allowed?
c.speed = 80;line 21 of the programThis line is in main, which is outside code. Is it allowed?