You have used + to add numbers. For a small class such as a distance or a page count, you can write a function named operator+ to define addition for that type. A reader should still be able to understand a + b as a sum, without knowing the function body.
#include <iostream>
struct Distance {
int metres;
};
Distance operator+(const Distance& a, const Distance& b) {
return Distance{a.metres + b.metres};
}
int main() {
Distance trip = Distance{30} + Distance{45};
std::cout << trip.metres << '\n';
}
operator+ is the function's name. It takes two read-only references because addition should not change either operand, and it returns a new value. The expression a + b selects this function when a and b are Distance objects. Returning Distance{...} uses the aggregate initialization you used with structs.
An overload does not change an operator's precedence or number of operands. You cannot redefine addition for two built-in integers. At least one operand must have a class or enumeration type for a user-defined operator overload.
The task uses PageCount, another small value type. Its meaning is deliberately narrow: adding page counts produces a combined count. All supplied amounts are small and nonnegative, so arithmetic stays safely within the integer range. Avoid modifying a or b; their const references also let the compiler protect them from accidental assignment.
The Console prints the sum and the original operands. These observations help distinguish “produce a new total” from “change an existing count.” Keep operator behavior aligned with the ordinary expectation for addition.
Edit main.cpp; the supplied Console probes already call your code.
operator+ to return a new PageCount containing the sum. Expect Combined: 200, with Originals: 120 80 unchanged.Use Run to inspect the Console, then Submit to check your work.