June 2025
Intermediate to advanced
1093 pages
33h 24m
English
In Chapter 12, Listing 12.24, Easter was a free function. Wouldn't it make sense if Easter were a method of Year? Then we could directly ask an instance Year year{2018} with year.easter() which day Easter falls on in that year.
// https://godbolt.org/z/zqKvMedz7 (full example)class Date; // forward declarationclass Year : public Value {public: explicit Year(int v) : Value{v, 4} {} Date easter() const; // declare new method};// Declare Month, Day, and Date here. Then:Date Year::easter() const { // define new method const int y = value_; int a = value_/100*1483 - value_/400*2225 + 2613; int b = (value_%19*3510 + a/25*319)/330%29; b = 148 - b - (value_*5/4 + a - b)%7; return Date{Year{value_}, Month{b/31}, Day{b%31 ...
Read now
Unlock full access