Namespaces
Variants
Views
Actions

Classes

From cppreference.com
< cpp‎ | language
 
 
C++ language
General topics
Flow control
Conditional execution statements
if
Iteration statements (loops)
for
range-for (C++11)
Jump statements
Functions
Function declaration
Lambda function declaration
inline specifier
Dynamic exception specifications (until C++20)
noexcept specifier (C++11)
Exceptions
Namespaces
Types
Specifiers
decltype (C++11)
auto (C++11)
alignas (C++11)
Storage duration specifiers
Initialization
Expressions
Alternative representations
Literals
Boolean - Integer - Floating-point
Character - String - nullptr (C++11)
User-defined (C++11)
Utilities
Attributes (C++11)
Types
typedef declaration
Type alias declaration (C++11)
Casts
Implicit conversions - Explicit conversions
static_cast - dynamic_cast
const_cast - reinterpret_cast
Memory allocation
Classes
Class-specific function properties
explicit (C++11)
static
Special member functions
Templates
Miscellaneous
 
 

A class is a user-defined type.

A class type is defined by class-specifier, which appears in decl-specifier-seq of the declaration syntax. The class specifier has the following syntax:

class-key attr class-head-name { member-specification } (1)
class-key attr class-head-name : base-specifier-list { member-specification } (2)
class-key - one of class, struct, union
attr - (since C++11) optional sequence of any number of attributes
class-head-name - the name of the class that's being defined. Optionally prepended by nested-name-specifier (sequence of names and scope-resolution operators, ending with scope-resolution operator), optionally followed by keyword final. The name may be omitted, in which case the class is unnamed (note that unnamed class cannot be final)
base-specifier-list - list of one or more parent classes and the model of inheritance used for each (see derived class)
member-specification - list of access specifiers, member object and member function declarations and definitions (see class definition)

The class keys struct and class are indistinguishable in C++, except that the default access mode and default inheritance mode are public if class declaration uses the struct class-key and private if the class declaration uses the class class-key. Both class and struct can be used in a class definition.

The use of the class key union results in a union definition, which defines a class that holds only one of its data members at a time.

A class can have the following kinds of members:

1) data members:
2) member functions:
3) nested types:
a) nested classes and enumerations defined within the class definition
b) aliases of existing types, defined with typedef or type alias (since C++11)declarations
c) the name of the class within its own definition acts as a public member type alias of itself for the purpose of lookup (except when used to name a constructor): this is known as injected-class-name
4) enumerators from all unscoped enumerations defined within the class, or introduced by using-declarations or using-enum-declarations (since C++20)
5) member templates (variable templates, (since C++14)class templates or function templates) may appear in the body of any non-local class/struct/union.

All members are defined at once in the class definition, they cannot be added to an already-defined class (unlike the members of namespaces)

A member of a class T cannot use T as its name if the member is a static data member, a member function, a member type, a member template , an enumerator of an unscoped enumeration, a member of a member anonymous union. However, a non-static data member may use the name T as long as there are no user-declared constructors.

A class with at least one declared or inherited virtual member function is polymorphic. Objects of this type are polymorphic objects and have runtime type information stored as part of the object representation, which may be queried with dynamic_cast and typeid. Virtual member functions participate in dynamic binding.

A class with at least one declared or inherited pure virtual member function is an abstract class. Objects of this type cannot be created.

A class with a constexpr constructor is a LiteralType: objects of this type can be manipulated by constexpr functions at compile time.

(since C++11)

Some member functions are special: under certain circumstances they are defined by the compiler even if not defined by the user. They are:

(since C++11)
(since C++11)