Assignment 1
Due 2026
,Question 1
Vehicle hierarchy and Qt parent–child list
1.1 Interpretation of the requirements
The task is to design a console-based vehicle management application using
sound object oriented principles and Qt features. The specification requires
the following:
* Each vehicle must have:
* a model (text)
* a year (integer with a reasonable range)
* There are two specialised vehicle types:
* Passenger vehicles with a passenger count
* Transport vehicles with a carrying capacity in kilograms
* Vehicles created without explicit values must use sensible defaults
* All appropriate getters and setters must be provided
* A function must output vehicle details
* A list of vehicles must be implemented using Qt’s parent–child facility
* The program must create several vehicles (including one using the default
constructor), add them to the list, and print them to the console
To support reflection later in Question 2, it is appropriate to design all
classes as subclasses of QObject and expose their data through Q_PROPERTY.
,1.2 Class design overview
A clean design is a small inheritance hierarchy:
* Vehicle (base class)
* PassengerVehicle (derived class)
* TransportVehicle (derived class)
This avoids redundant code and follows OOP principles such as reuse and
polymorphism.
Vehicle (Base Class)
Superclass: QObject
Attributes:
* QString model
* int year
Operations:
* Constructors with default values
* Getter and setter for model
* Getter and setter for year (with validation)
* Virtual function describe() to output details
,PassengerVehicle (Derived from Vehicle)
Additional attribute:
* int passengerCount
Operations:
* Getter and setter for passenger count
* Override describe()
TransportVehicle (Derived from Vehicle)
Additional attribute:
* double capacityKg
Operations:
* Getter and setter for capacity
* Override describe()
All classes inherit QObject, include Q_OBJECT, and define Q_PROPERTY entries
so their properties can be inspected reflectively.
, 1.3 Base Vehicle class design
The Vehicle class stores common data and ensures values remain valid.
```cpp
class Vehicle : public QObject {
Q_OBJECT
Q_PROPERTY(QString model READ model WRITE setModel)
Q_PROPERTY(int year READ year WRITE setYear)
public:
explicit Vehicle(const QString &model = "Unknown",
int year = QDate::currentDate().year(),
QObject *parent = nullptr)
: QObject(parent), m_model(model), m_year(year)
{
setYear(m_year); // ensure validation
}
QString model() const { return m_model; }
void setModel(const QString &model) { m_model = model; }
int year() const { return m_year; }
void setYear(int year) {
int current = QDate::currentDate().year();
if (year < 1886 || year > current + 1)
m_year = current;
else
m_year = year;
}
virtual QString describe() const {
return QString("Model: %1, Year: %2").arg(m_model).arg(m_year);