Namespaces
Variants
Views
Actions

Classes

From cppreference.com
< cpp‎ | language
Revision as of 19:22, 2 April 2013 by Cubbi (Talk | contribs)

 
 
C++ language
General topics
Flow control
Conditional execution statements
Iteration statements
Jump statements
Functions
function declaration
lambda function declaration
function template
inline specifier
exception specifications (deprecated)
noexcept specifier (C++11)
Exceptions
Namespaces
Types
decltype specifier (C++11)
Specifiers
cv specifiers
storage duration specifiers
constexpr specifier (C++11)
auto specifier (C++11)
alignas specifier (C++11)
Initialization
Literals
Expressions
alternative representations
Utilities
Types
typedef declaration
type alias declaration (C++11)
attributes (C++11)
Casts
implicit conversions
const_cast conversion
static_cast conversion
dynamic_cast conversion
reinterpret_cast conversion
C-style and functional cast
Memory allocation
Classes
Class-specific function properties
Special member functions
Templates
class template
function template
template specialization
parameter packs (C++11)
Miscellaneous
Inline assembly
 

A class is a user-defined type.

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

Template:sparam Template:sparam Template:sparam Template:sparam { Template:sparam } (1)
Template:sparam - one of class, struct, union
Template:sparam(C++11) - optional sequence of any number of attributes
Template:sparam - the name of the class that's being defined. Optionally prepended by Template:sparam (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)
Template:sparam - optional list of one or more parent classes and the model of inheritance used for each (see derived class)
Template:sparam - 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 four kinds of members

a) non-static data members, including bit fields.
b) static data members
a) non-static member functions
b) static member functions
a) nested classes and enumerations defined within the class definition
b) aliases of existing types, defined with typedef or type alias declarations
4) enumerators from all unscoped enumerations defined within the class

All members are defined at once in the class definition, they cannot be added later.

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

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

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

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