ASSIGNMENT 1 2026
DUE: 15 MAY 2026
, Question 1
For this question you need to ensure that you design the appropriate classes
needed to address the specification below.
Create a console application to handle vehicle details. Considering the required functionality
that is required and OOP design principles (avoiding anti-patterns and having minimal
redundant code), create and implement the appropriate classes necessary to achieve the
following.
We define a base Vehicle class and two subclasses
( PassengerVehicle and TransportVehicle ) using Qt’s object model. Each class
inherits QObject (to leverage Qt’s meta-object system) and uses the Q_OBJECT macro. Key
member variables are exposed as Qt properties with Q_PROPERTY to enable reflection
(needed later). For example, Vehicle has model (QString) and year (int),
while PassengerVehicle adds passengerCount and TransportVehicle adds cargoCapaci
ty . Constructors initialize defaults; getters/setters manipulate these properties. By passing
a QObject *parent to each constructor and calling QObject(parent) , we build a parent–
child hierarchy. Qt automatically adds a created object to its parent’s children() list and
takes ownership (deleting children in the parent’s destructor) . This means we can
keep a “root” QObject whose children are all vehicle instances, avoiding manual memory
management. Finally, each class provides a display() method to print its details.
C++
#include <QObject>
#include <QString>
#include <QDebug>
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(QObject *parent = nullptr)
: QObject(parent), m_model("Unknown"), m_year(0) {}
// Accessors
QString model() const { return m_model; }
int year() const { return m_year; }
void setModel(const QString &m) { m_model = m; }
void setYear(int y) { m_year = y; }
// Display details
void display() const {
qDebug() << "Vehicle:" << m_model << ", Year:" << m_year;
}
private:
QString m_model;
int m_year;
};