[ fromfile: classes.xml id: definingclasses ]
class ClassName { public: publicMembers private: privateMembers };
The first line of the class definition is called the classHead.
The features of a class include data members, member functions, and access specifiers (public, private, protected
).
Member functions are used to initialize, manipulate, or otherwise manage the data members.
After you have defined a class, you can use the class name as a type for variables, parameters, and returns from functions.
Variables of a class type are called objects, or instances, of the class.
Member functions for class ClassName specify the behavior of all objects of type ClassName.
Each member function can access all other members of the class.
Nonmember functions can only manipulate objects indirectly by calling member functions.
The set of values of the data members of an object is called the state of the object.
Example 2.3. src/classes/fraction/fraction.h
Example 2.4 is an implementation file that contains definitions of the functions declared in Example 2.3.
Example 2.4. src/classes/fraction/fraction.cpp
Every identifier has a scope (Section 20.2), a region of code in which a name is "known" (or visible) and accessible.
The toString()
member function typically returns a string containing a "snapshot" of the current state of an object.
To increase flexibility, you can give the toString()
function one or more parameters that permit the string to have a variety of formats.
Generated: 2012-03-02 | © 2012 Alan Ezust and Paul Ezust. |