C++ Classes
Group data and behavior.
On this page
The concept
A class defines a custom type. Public members are accessible to users of that type.
Code example
C++
#include <iostream>
class Counter { public: int value = 0; void increment() { value++; } };
int main() { Counter c; c.increment(); std::cout << c.value; }Output
1
Follow the code
Open the walkthrough to inspect the example one step at a time.
Show guided walkthrough
Guided walkthroughReady
1
#include <iostream>2
class Counter { public: int value = 0; void increment() { value++; } };3
int main() { Counter c; c.increment(); std::cout << c.value; }Follow the example
Start to inspect the source, values, and output. These prepared steps explain the published sample; grouped loop steps describe multiple iterations.
View the complete dry-run table
| Step / line | What happens | State after step | Output so far |
|---|---|---|---|
| Define Counter with an integer value and increment method. | — | — | |
| Create c with value 0, call increment, then print value. | c.value = 1 | 1 |
Behind the scenes
How C++ processes this code
The C++ compiler produces a native executable. Execution enters main, where objects and function calls perform the work.