Namespaces
Variants
Views
Actions

Class declaration

From cppreference.com
< cpp‎ | language
Revision as of 21:44, 2 November 2012 by P12bot (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
 

Contents

Syntax

class Template:sparam { Template:sparam } Template:sparam ; (1)
class Template:sparam : Template:sparam { Template:sparam } Template:sparam ; (2)
class Template:sparam ; (3)
class Template:sparam final Template:sparam (4) (since C++11)

Class Body

A list of member and friend declarations and access specifiers:

public: (1)
protected: (2)
private: (3)
friend Template:sparam (4)
Template:sparam (5)
static Template:sparam (6)
Template:sparam (7)

Ancestor List

A list of classes that have already bee fully defined optionally prefixed with an access specifier

Object List

An optional list of instances of the previously defined class

Explanation

  1. Defines a class and its member
  2. Defines a class inheriting other classes
  3. Forwards declares a class
  4. Defines a class that cannot be derived from ( see final )

If friend or member functions have their body defined inside the class body, they are implicitly inlined

Notes

(since C++11) A default value can be assigned to data members inside the class body (ie: not necessarily in a constructor)

See also


Example

class C;
 
class D : public B // B needs to be defined
{
  private:
    C *ptr_c; // a pointer/reference to C can be used as C has been forward declared
    double x = 12.3; // C++11 inline data member initialization
    static const int sci = 1; // this is valid in C++98 as well
  public:
    typedef B parent_type;
 
    // inline function
    virtual parent_type foo() const
    {
        return B();
    }
 
    // non-inline function declaration. needs to be defined externally
    void bar();
} D_obj; // An object of type D is defined
 
// definition of a class method outside the class
void D::bar()
{
   //...
}