A counter that always starts at zero is useful, but some objects need values at creation. A constructor runs when an object is initialized. It has the same name as the class and no return type—not even void. Its parameters describe the information needed to create the object.
The colon after the parameter list starts a member initializer list. In Score(int start) : points(start) {}, the member points is initialized from the parameter start before the constructor body runs. This is initialization, not a later assignment. If you initialize several members, separate their entries with commas. Actual initialization follows the order in which the members are declared in the class, so write the list in that same order.
Score board(7); creates an object called board and supplies 7 to its constructor. This is different from calling an ordinary member function on an object that already exists.
#include <iostream>
class Score {
private:
int points;
public:
Score(int start) : points(start) {}
int read() { return points; }
};
int main() {
Score board(7);
std::cout << board.read() << '\n';
}
The example prints 7 even though the constructor body is empty: points(start) has already stored the argument. Your Ticket constructor works the same way. Its quantity(count) entry is complete, but cents(0) ignores the supplied price. Change only that temporary initializer. The two tickets pass different prices, so both totals should come from their own constructor arguments.
cents from the constructor parameter price, rather than zero. The supplied tickets should total 700 and 375 cents.Edit main.cpp, then press Run to inspect the Console. Press Submit to check your work. Keep the supplied probes in place.