A derived class contains a base-class part and can add its own data and behavior. Public inheritance describes an “is a” relationship: a specialized object should be usable where the base kind is expected. It is not merely a shortcut for borrowing fields.
#include <iostream>
class Pass {
int days;
public:
Pass(int length) : days(length) {}
int duration() const { return days; }
};
class WeekendPass : public Pass {
public:
WeekendPass() : Pass(2) {}
};
int main() {
WeekendPass ticket;
std::cout << ticket.duration() << '\n';
}
The colon after WeekendPass introduces its base list. public Pass keeps the base's public operations public on the derived object. The constructor's : Pass(2) initializes the base part before the derived constructor body runs. This is a constructor initializer, not assignment to a separate temporary pass.
The base's private data remains private. The derived class does not gain permission to read days directly; it can use the public duration operation. This keeps the base responsible for rules about its own data, often called invariants. A derived object should use the base's public operations rather than bypassing those rules. protected is another access level that permits derived-class access, but exposing data that way can make invariants harder to preserve.
The task's Volunteer is a kind of Member. Its name belongs to the base part; its assigned shift belongs to the derived part. Finish the two constructor initializers independently. The supplied getters make both effects visible without adding printing inside the classes. Later lessons will distinguish ordinary inherited operations from virtual operations that choose behavior at runtime.
Edit main.cpp; the supplied Console probes already call your code.
memberName to the Member base constructor. Expect Member: Mina.shift from assignedShift. Expect Shift: 2.Use Run to inspect the Console, then Submit to check your work.