COS3711
Assignment 1
Due 2026
,Question 1
Vehicle hierarchy and Qt parent–child list
Interpretation of the requirements
The key requirements are:
* Every vehicle has a model and a year.
* The year must be reasonable.
* Two specialised types exist:
* Passenger vehicles with a passenger count
* Transport vehicles with carrying capacity in kilograms
* Objects created without details must use sensible defaults.
* All getters and setters must be provided.
* A function must output vehicle details.
* The list must use Qt parent–child ownership rather than a container.
* The program must create and print multiple vehicles, including one created
with the default constructor.
To support Question 2 later, all classes should inherit from QObject and
expose properties using Q_PROPERTY.
Class design
A clean inheritance hierarchy avoids redundancy and promotes reuse.
,Base class: Vehicle
Responsibilities:
* Store shared attributes
* Provide validation logic
* Enable polymorphic output
Attributes:
* QString model
* int year
Operations:
* Constructors with defaults
* Getters and setters
* Validation of year
* Virtual describe() function
```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)
{
setYear(year);
, }
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);
}
private:
QString m_model;
int m_year;
};
```
Key design justification:
* Centralised validation prevents duplicated logic.
* Defaults ensure valid objects even when using empty constructors.
* Q_PROPERTY enables reflection for Question 2.