C++20 sections · 1024 units
Open in Course

Defining a Class

Basic structure

You define a class with the class keyword followed by the class name and a body enclosed in curly braces. class Car { }; creates an empty class. End the definition with a semicolon - forgetting it causes cryptic compiler errors.

Inside the braces, you declare member variables and member functions. class Car { int speed; void drive(); }; defines a Car with a speed variable and a drive function. These members describe what every Car object will contain.

Place your class definition in a header file (.h) so multiple source files can use it. This keeps your code organized and enables reuse across your entire project.