Classes
Objective #1: Use classes
- An overloaded function is a function that can be used by specifying two or more different sets of
parameters. The find member function in the string class is overloaded. The find function can be passed a string parameter or a character
literal. The compiler automatically detects which version of the function
must be called depending on the parameters.
- The keyword this is used within several member functions of the string class
(as well as the methods of other classes) to refer to the object to which
the function applies. For example, the statement return
*this; is at the end of all of the overloaded
operator functions in the string class and it returns the current object
that the this object points to. Note that returning a pointer to an object
(*this) saves memory since only a pointer variable is actually being returned.
(Using return this; would
pass the whole object.)
- under development
Objective #2: Use destructors
- A destructor is used to release the memory used by an object when it falls
out of scope or is no longer needed. Usually, C++ will provide a destructor
automatically even if one is not added to the class definition. (Similar to
how a default constructor is added if the programmer doesn't do so explicitly.)
A destructor is denoted by the use of the tilde (~) in front of the class
name. For example, the prototype for the destructor to the Transaction class
is:
~Transaction( );
and the definition of a simple destructor is
Transaction::~Transaction()
{
}
Objective #3: Use static properties.
- A property is static by using the keyword static in its declaration as in
static int numTransactions;
or
const static int LIMIT = 100;
in the client:
cout <<
Transaction.LIMIT;
Objective #3: Use static methods.
- A method can be static by using the keyword static in its method header
in Transaction.h
int numTransactions();
in Transaction.cpp
int Transaction::get NumTransactions()
{
return numTransactions;
}
in a client:
cout << Transaction.getNumTransactions() << endl;
Objective #4: Use free functions where necessary and appropriate.
- The function getline is a free function that is used with the string
class. A free function is not included as part of a class definition. Therefore
a free function is not a member function. But, the prototype for a free function
may be placed in the .h file where a class definition is found so that the
function can be used hand-in-hand with the class. The free function's definition
is placed in the implementation .cpp file along with the function definitions
for the member functions. However the scope specifier is not used in the function
header for a free function since it is technically not part of the class.
Objective #5: Use const appropriately in a class.
- The const keyword is used
at the end of a member function definition header to specify that the
member function's method should not change any member variables. Such
member functions are called const member functions. A careful programmer
would never have to use const in this way since he/she would not write
code within a member function that changes member variables unless he/she
was sure that he/she means to do so. But, it is common and wise for programmers
to use const to make "constant member functions" to avoid logic errors.
A syntax error will result if you do accidentally try to modify member
variables within a constant member function. It is common to use const
in this way in accessor functions whose only purpose is to "retrieve"
data anyway.
For example, it is used in this way with string's length member function:
int length( ) const;
This prevents the programmer who wrote the length function from accidentally
changing the value of any string member variables (such as myLength
or myCapacity).
- const can also be used within the parameter list of a function. This is done in
string's find member function:
int find( const string & str ) const;
where the incoming parameter str is being passed to the find member function
by reference. Passing an string by reference is advisable because it
is memory efficient and faster than passing a potentially large string
object by value. But, if the programmer really doesn't intend to modify
the actual value of str, then he should use const in this way. Using const
here will cause a syntax error to occur if he accidentally writes code
within the find function that accidentally modifies str, thus providing
a check against his own mistakes or sloppy programming.
- const is also used to keep the returned object from being modified within the
call statement of the client program. In the overloaded assignment operator
of the string class, const is used in this way,
const string & operator = ( char ch );
Consider a client program with the following code:
string myInitial;
(myInitial = 'P') = 'G';
The assignment statement above is ambiguous and therefore disallowed
by the use of const. The value returned as myInitial after the execution
of the parentheses is 'P' but the use of const keeps it from being modified
further when the second assignment operator is applied in the very
same statement. This use of const would however allow two separate
statements to assign different values to myInitial as in:
myInitial = 'P';
myInitial = 'G';
It is common and wise to use const in this way when overloading assignment
and compound operators.