Datasheet

Const References
You will often find code that uses const reference parameters. At first, that seems like a contradiction.
Reference parameters allow you to change the value of a variable from within another context.
const
seems to prevent such changes.
The main value in
const reference parameters is efficiency. When you pass a variable into a function,
an entire copy is made. When you pass a reference, you are really just passing a pointer to the original
so the computer doesn’t need to make the copy. By passing a
const reference, you get the best of both
worlds no copy is made but the original variable cannot be changed.
const references become more important when you are dealing with objects because they can be large
and making copies of them can have unwanted side effects. Subtle issues like this are covered in
Chapter 12.
C++ as an Object-Oriented Language
If you are a C programmer, you may have viewed the features covered so far in this chapter as convenient
additions to the C language. As the name C++ implies, in many ways the language is just a “better C.”
There is one major point that this view overlooks. Unlike C, C++ is an object-oriented language.
Object-oriented programming (OOP) is a very different, arguably more natural, way to write code.
If you are used to procedural languages such as C or Pascal, don’t worry. Chapter 3 covers all the back-
ground information you need to know to shift your mindset to the object-oriented paradigm. If you
already know the theory of OOP, the rest of this section will get you up to speed (or refresh your mem-
ory) on basic C++ object syntax.
Declaring a Class
A class defines the characteristics of an object. It is somewhat analogous to a struct except a class defines
behaviors in addition to properties. In C++, classes are usually declared in a header file and fully
defined in a corresponding source file.
A basic class definition for an airline ticket class is shown below. The class can calculate the price of the
ticket based on the number of miles in the flight and whether or not the customer is a member of the
“Elite Super Rewards Program.” The definition begins by declaring the class name. Inside a set of curly
braces, the data members (properties) of the class and its methods (behaviors) are declared. Each data
member and method is associated with a particular access level:
public, protected, or private. These
labels can occur in any order and can be repeated.
// AirlineTicket.h
#include <string>
class AirlineTicket
{
public:
AirlineTicket();
26
Chapter 1
04_574841 ch01.qxd 12/15/04 3:39 PM Page 26