C Primer Plus (5th Edition)

   

Before we discuss each class member in detail, let's get an overview of the members we have available.

Syntax Box 12.1 expands the syntax shown in Figure 12.1 by including the total set of members we can include in the class definition. The first few lines displays the familiar class definition syntax consisting of the class keyword followed by the name of the class (identifier) and the set of curly braces that forms the class body where the class members reside. Further down, the class members have been divided into three broad categories data members, function members, and nested types. The following provides a brief introduction to the members of each of these categories, which also have been listed in the syntax box. Data members consist of member variables, constants, and events.

Syntax Box 12.1 Overview of the Class Members

Class_definition::= class <Class_identifier> { <Class_members> }

where

<Class_members> ::= <Data_members> ::= <Function_members> ::= <Nested_types> <Data_members> ::= <Member_variables> ::= <Constants> ::= <Events> <Function_members> ::= <Methods> ::= <Constructors> ::= <Destructor> ::= <Properties> ::= <Indexers> ::= <Operators> <Nested_types> ::= <Nested_classes> ::= <Nested_structs> ::= <Nested_enums>

Notes:

  • The class definition provided here concentrates on the insides of the class and omits the syntax elements related to access modification, inheritance, and interfaces of the class itself.

  • The class members can be written inside the class in any order you want without changing the semantics.

  • A <Function_member> can either be an <Instance_function> or a <Static_function> (also called <Class_function>). An instance function is executed in relation to a particular object and, as opposed to the static function, needs an object to be called through.

Function members consist of methods, constructors, destructors, properties, indexers, and operators:

Nested types are classes, structs, enums, and interfaces defined within the class body. They allow the programmer to hide types that are only needed within a class. Just like helper methods are kept private to lessen the complexity faced by the user of the class, nested types help us to reduce the number of classes to which an assembly user needs to look.


   

Категории