From da65a4ff31061da10993396a70c3529e490f22f7 Mon Sep 17 00:00:00 2001 From: Michael McLeod Date: Fri, 9 Jan 2026 16:51:49 +0000 Subject: [PATCH] Explain initialiser list --- 02cpp1/sec03ObjectOrientedProgramming.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/02cpp1/sec03ObjectOrientedProgramming.md b/02cpp1/sec03ObjectOrientedProgramming.md index 895fc8498..036f1e5ad 100644 --- a/02cpp1/sec03ObjectOrientedProgramming.md +++ b/02cpp1/sec03ObjectOrientedProgramming.md @@ -210,6 +210,8 @@ class Ball }; ``` +- The syntax `: position(p), radius(r), mass(m)` after the constructor signature is called an initialiser list. This can be used to initialise member variable with simple expressions. It's very useful when just assigning constructor arguments to member variables. + Now we can even make our code **more flexible without sacrificing safety**. Let's say the ball can change _mass_ or _radius_. We can't just make these variables public and change them independently, because then the _density_ will no longer be consistent with the new mass / radius. We need to add **setter** functions which **maintain the integrity of the object**: ```cpp class Ball