C++ Reference ยท Keywords and Statements
The class and struct keywords
Reference for C++ class and struct: members, default access and inheritance, constructors, aggregates, forward declarations and when to choose which.
class and struct both define a user-defined type that bundles data members and member functions. They are nearly identical. The only differences are defaults: members of a struct are public unless stated otherwise, members of a class are private; likewise struct inherits publicly by default, class privately.
By convention, struct is used for simple data holders without invariants, and class for types that protect their state behind an interface. The choice is about communicating intent, not about capability: a struct can have constructors, virtual functions, templates and everything else.
class Account { public: explicit Account(double balance) : balance_(balance) {} double balance() const { return balance_; } private: double balance_; };
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.
Syntax
class-key attribute-list name final(optional) : base-clause(optional) { member-specification }; // class-key is class, struct (or union) struct Name; // forward declaration
| Part | Description |
|---|---|
class-key |
class (private by default) or struct (public by default). |
final |
Optional; prevents further derivation. |
base-clause |
: public Base, private Other. Without an access specifier the default of the class-key applies. |
| member-specification | Data members, member functions, nested types, static members, access labels (public:, protected:, private:), friends and using-declarations. |
Usage notes
- Declaring a class twice with different class-keys is legal but triggers warnings on some compilers (MSVC mangles names differently). Use the same key everywhere.
- A class is an aggregate when it has no user-declared constructors, no private or protected non-static data members, no virtual functions and no virtual, private or protected base classes. Aggregates support
{...}and designated initializers. - The compiler generates default constructor, copy/move operations and destructor when you do not declare them. See the rule of zero, three and five.
- Member functions defined inside the class are implicitly
inline. Members may be used in member function bodies before they are declared, but not in declarations. - Class types are complete only after the closing brace; inside a member function body the class is treated as complete.
- A class with at least one virtual function has a hidden pointer to its vtable, which increases
sizeof. - Data members are initialized in declaration order, not in the order of the constructor's initializer list.
- Use default member initializers (
int x = 0;) so that every constructor gets sane values. - A type that holds an invariant should hide its data. A plain struct with public fields is fine for a pure data carrier.
enum classandunionare different kinds of types; they are not interchangeable withstruct.