class
or keyword struct
, with the following syntax:class class_name { access_specifier_1: member1; access_specifier_2: member2; ... } object_names; |
class_name
is a valid identifier for the class, object_names
is an optional list of names for objects of this class. The body of the declaration can contain members, which can either be data or function declarations, and optionally access specifiers.private
, public
or protected
. These specifiers modify the access rights for the members that follow them:private
members of a class are accessible only from within other members of the same class (or from their "friends").protected
members are accessible from other members of the same class (or from their "friends"), but also from members of their derived classes.public
members are accessible from anywhere where the object is visible.class
keyword have private access for all its members. Therefore, any member that is declared before any other access specifier has private access automatically. For example:
|
|
Rectangle
and an object (i.e., a variable) of this class, called rect
. This class contains four members: two data members of type int
(member width
and member height
) with private access (because private is the default access level) and two member functions with public access: the functions set_values
and area
, of which for now we have only included their declaration, but not their definition.Rectangle
was the class name (i.e., the type), whereas rect
was an object of type Rectangle
. It is the same relationship int
and a
have in the following declaration:
|
|
int
is the type name (the class) and a
is the variable name (the object). Rectangle
and rect
, any of the public members of object rect
can be accessed as if they were normal functions or normal variables, by simply inserting a dot (.
) between object name and member name. This follows the same syntax as accessing the members of plain data structures. For example:
|
|
rect
that cannot be accessed from outside the class are width
and height
, since they have private access and they can only be referred to from within other members of that same class.
|
|
area: 12 |
::
, two colons), seen in earlier chapters in relation to namespaces. Here it is used in the definition of function set_values
to define a member of a class outside the class itself.area
has been included directly within the definition of class Rectangle
given its extreme simplicity. Conversely, set_values
it is merely declared with its prototype within the class, but its definition is outside it. In this outside definition, the operator of scope (::
) is used to specify that the function being defined is a member of the class Rectangle
and not a regular non-member function.::
) specifies the class to which the member being declared belongs, granting exactly the same scope properties as if this function definition was directly included within the class definition. For example, the function set_values
in the previous example has access to the variables width
and height
, which are private members of class Rectangle
, and thus only accessible from other members of the class, such as this.width
and height
have private access (remember that if nothing else is specified, all members of a class defined with keyword class
have private access). By declaring them private, access from outside the class is not allowed. This makes sense, since we have already defined a member function to set values for those members within the object: the member function set_values
. Therefore, the rest of the program does not need to have direct access to them. Perhaps in a so simple example as this, it is difficult to see how restricting access to these variables may be useful, but in greater projects it may be very important that values cannot be modified in an unexpected way (unexpected from the point of view of the object).Rectangle
, we could have declared the object rectb
in addition to object rect
:
|
|
rect area: 12 rectb area: 30 |
Rectangle
, of which there are two instances (i.e., objects): rect
and rectb
. Each one of them has its own member variables and member functions.rect.area()
does not give the same result as the call to rectb.area()
. This is because each object of class Rectangle
has its own variables width
and height
, as they -in some way- have also their own function members set_value
and area
that operate on the object's own member variables.rect.area
or rectb.area
. Those member functions directly used the data members of their respective objects rect
and rectb
.area
before having called set_values
? An undetermined result, since the members width
and height
had never been assigned a value.void
.Rectangle
class above can easily be improved by implementing a constructor:
|
|
rect area: 12 rectb area: 30 |
Rectangle
has no member function set_values
, and has instead a constructor that performs a similar action: it initializes the values of width
and height
with the arguments passed to it.
|
|
void
: Constructors never return values, they simply initialize the object.
|
|
rect area: 12 rectb area: 25 |
Rectangle
are constructed: rect
and rectb
. rect
is constructed with two arguments, like in the example before.rectb
. Note how rectb
is not even constructed with an empty set of parentheses - in fact, empty parentheses cannot be used to call the default constructor:
|
|
rectc
a function declaration instead of an object declaration: It would be a function that takes no arguments and returns a value of type Rectangle
.class_name object_name = initialization_value;
{}
) instead of parentheses (()
):class_name object_name { value, value, value, ... }
|
|
foo's circumference: 62.8319 |
|
|
initializer_list
as its type.:
) and a list of initializations for class members. For example, consider a class with the following declaration:
|
|
|
|
|
|
|
|
|
|
foo's volume: 6283.19 |
Cylinder
has a member object whose type is another class (base
's type is Circle
). Because objects of class Circle
can only be constructed with a parameter, Cylinder
's constructor needs to call base
's constructor, and the only way to do this is in the member initializer list.{}
instead of parentheses ()
:
|
|
|
|
Rectangle
.->
). Here is an example with some possible combinations:
|
|
*
, &
, .
, ->
, []
). They can be interpreted as:expression | can be read as |
---|---|
*x | pointed to by x |
&x | address of x |
x.y | member y of object x |
x->y | member y of object pointed to by x |
(*x).y | member y of object pointed to by x (equivalent to the previous one) |
x[0] | first object pointed to by x |
x[1] | second object pointed to by x |
x[n] | (n+1 )th object pointed to by x |
[]
) and the chapter about plain data structures introduced the arrow operator (->
).class
, but also with keywords struct
and union
.struct
, generally used to declare plain data structures, can also be used to declare classes that have member functions, with the same syntax as with keyword class
. The only difference between both is that members of classes declared with the keyword struct
have public
access by default, while members of classes declared with the keyword class
have private
access by default. For all other purposes both keywords are equivalent in this context.struct
and class
, since unions only store one data member at a time, but nevertheless they are also classes and can thus also hold member functions. The default access in union classes is public
.Previous: Other data types | Index | Next: Classes (II) |