Class Anatomy:
A class declaration defines member functions and member state on which the member functions act. A member function definition defines a single copy of code, placed in static memory, that serves all instances of the class. Non-static member data declarations define data held in each instance of the class. The values of that data are unique to each class instance.
Str.h
- Functions with names that are the same as the class name are constructors. The declaration of a class instance always causes a constructor to be called.
- A function with the name of the class prepended with a tilde (~) is a destructor. Destructors are called when the thread of execution leaves the scope in which the class instance was declared.
- Constructors and destructors are defined to manage allocation of resources to each instance of the class. Using code should not have to participate in the management for those resources.
- Operators are functions that are called whenever the operator symbol(s) appear immediately following the name of an existing instance of the class.
- Function arguments that are assigned a value define the default value for that argument, which is omitted in a function definition if we want to use the default value. Default arguments must be defined right-to-left.
- Functions whose declaration ends with the keyword const guarantee that the state of the instance won't be changed by calling the function.
- Note that class declarations are always part of a header file, e.g., Str.h. The member function definitions are usually placed in an implementation file, Str.cpp. However, it is acceptable to place the function definition in-line withing the class declaration, or just after the class declaration if we qualify them as inline.