CS 102 Lecture Notes
Week Four, Wednesday/Thursday: Classes, Part II
const Objects and const Member Functions
Class member data that should never change during program execution should be declared "const."
A function is specified as const both in its prototype and
in its definition by inserting the the keyword "const" after the function's
parameter list. Any function so specified will then be unable to modify
class member data. This is a protection mechanism to help prevent
faults in subsequent program development. If you choose to use const
functions, you should use them consistently.
Composition: Objects as Members of Classes
"Composition" is also sometimes called "aggregation." Aggregation is the
primary means of creating higher level abstrations of classes.
For example, an Employee class might have a Date member for the
employee's hire date or birth date.
friend Functions and friend Classes
A "friend" function or class is given access to the private functions and
data of the class in which the friend is declared. This feature of C++
violates encapsulation and should be used cautiously.
Using the this Pointer
Every object (an instance of a class) has access to its own memory address
usijng a pointer called "this." For example, you can set up two-way connections
between objects by passing one of them the address of the first one using "this."
This page established September 18, 1999; last updated January 29, 2000, by Rick Wagner.