C++20 sections · 1024 units
Open in Course

Member Functions

Object behavior

Member functions (also called methods) define what an object can do. They have direct access to all member variables without needing parameters. Inside void Car::drive(), you can read and modify speed directly.

You can declare functions inside the class or just declare them and define them outside: void drive(); inside the class, then void Car::drive() { speed += 10; } in the .cpp file. This keeps class definitions clean.

Const member functions promise not to modify the object: void display() const; This lets you call display() on const objects and communicates that the function is read-only.