You define a class in C++ with the class (or struct) keyword, a name, and a body of data members and member functions, and you control who can use each member with the access specifiers public, private and protected. In a class, members are private by default, so anything outside the class can only use what you mark public.
This guide covers the syntax, the three access levels, how to split declaration and definition, and how to create objects.
A class bundles data (members) and the functions that work on that data (member functions). Here is a complete example of a BankAccount class.
The class ends with a semicolon, which beginners often forget and which produces confusing compiler errors. acc is an object, one instance of the class. A class itself is a type; it takes no memory until you create objects.
Access specifiers decide which code may name a member.
| Specifier | Accessible from |
|---|---|
public | Anywhere |
protected | The class itself, friends and derived classes |
private | The class itself and its friends |
A specifier applies to every member after it until the next specifier. You can repeat specifiers, but grouping members by access keeps the class easy to read.
The compiler checks access at compile time. It is a design tool and not a security feature, because anyone who can edit or recompile your code can bypass it.
In C++ the only difference between class and struct is the default access. Members and base classes of a class are private by default, while those of a struct are public. By convention, use struct for simple data holders with no invariants and class when you protect state behind member functions.
Real projects usually put the class declaration in a header and the longer member function bodies in a .cpp file. Use the scope resolution operator :: to say which class a function belongs to.
Functions defined inside the class body are implicitly inline, which is fine for short ones. Mark member functions that do not change the object as const, as balance() does, so they can be called on const objects.
You can create objects on the stack, which is the default choice, or on the heap through a smart pointer.
Use . on an object or reference and -> on a pointer. Brace initialisation {} is preferred because it rejects narrowing conversions such as int x{3.5}.
explicit unless implicit conversion is intended.const to every member function that does not modify state.Private in a class, public in a struct. Everything else about the two keywords is identical.
Yes. A class definition is a declaration, and }; ends it. Omitting it gives errors that often point to the next line.
Yes, when both objects are of the same class. Access is per class, not per object, so a member function can read the private data of another instance passed as an argument.
friend keyword do?It grants a named function or class access to private and protected members. Use it rarely, since it weakens encapsulation.
Define a class with class Name { ... };, put the interface under public, keep data under private, and use protected only for members meant for derived classes. A class defaults to private and a struct to public. Declare in headers, define in .cpp files, mark read-only functions const, and create objects on the stack first.