Static members and operators
Data that belongs to a whole class rather than one object, const correctness, and operator overloading, so that your own types work with +, ==, << and [].
About 6 hours of study
Learning objectives
- Use static data members and static member functions
- Write const member functions
- Overload arithmetic and comparison operators
- Overload << and []
Lessons
- 1
Static and const members
A static member belongs to the class, not to each object; const member functions promise not to change the object.
- 2
Operator overloading
Teach your classes +, ==, [] and <<, as member functions or as friend functions, so they read like built-in types.
Module practice
- C++: Counting objects with static membersEasy
- C++: Student IDs from a static counterMedium
- C++: One interest rate for every accountHard
- C++: Comparing version numbersEasy
- C++: Adding fractions with operator+Medium
- C++: A 2D vector with operatorsMedium
- C++: A 2 × 2 matrix with operatorsHard