C++ functions that process arrays need to be informed about
The kind of data in the array
The location of the beginning of the array
And the number of elements in the array
The traditional C/C++ approach to functions that process arrays
Is to pass a pointer to the start of the array as one argument
And to pass the size [...]
When you choose to use an array to represent data, you are making a design decision
But design decisions should go beyond how data is stored; they should also involve how the data is used
Often, you’ll find it profitable to write specific functions to handle specific data operations
(The profits here include increased program reliability, ease of [...]
Let’s look at the implications of (A-1)
The function call sum_arr(cookies, ArSize)
Passes the address of the first element of the cookies array and the number of elements of the array to the sum_arr() function
The sum_arr() function
Assigns the cookies address to the pointer variable arr and assigns ArSize to the int variable n
This means (A-1) doesn’t really [...]
C and C++ use the & symbol to indicate the address of a variable
C++ assigns an additional meaning to the & symbol
Presses it into service for declaring references
For example, to make rodents an alternative name for the variable rats, you could do the following (A-1):
Example of (A-1)
int rats;
int & rodents = rats; // makes rodents [...]
Sometimes it is useful to have a collection of values of different types and to treat the collection as a single item
For example, consider a bank certificate of deposit, which is often called a CD
A CD is a bank account that does not allow withdrawals for a specified number of months
A CD naturally has three [...]
You can declare class members
Whether they are data items or member functions
Either in the public or the private section of a class
But because one of the main precepts of OOP is to hide the data
Data items normally go into the private section
The member functions that constitute the class interface
Go into the public section
Otherwise, you can’t [...]
Inline functions
Are a C++ enhancement designed to speed up programs
The primary distinction between normal functions and inline functions
Is not in how you code them
But in how the C++ compiler incorporates them into a program
To understand the distinction between inline functions and normal functions
You need to peer more deeply into a program’s innards than we have [...]
Suppose you create a screen menu that asks the user to select one of five choices—for example
Cheap
Moderate
Expensive
Extravagant
Excessive
You can extend an if else if else sequence to handle five alternatives
But the C++ switch statement more easily handles selecting a choice from an extended list
Here’s the general form for a switch statement (A-1):
Example of (A-1)
switch (integer-expression)
{
case [...]
A string
Is a series of characters stored in consecutive bytes of memory
C++ has two ways of dealing with strings
The first, taken from C and often called a C-style string, is the first one this page examines
Later, we will discuss an alternative method based on a string class library
The idea of a series of characters stored [...]
A for loop provides
A step-by-step recipe for performing repeated actions
Let’s take a detailed look at how it’s set up
The usual parts of a for loop handle these steps:
Setting a value initially
Performing a test to see whether the loop should continue
Executing the loop actions
Updating value(s) used for the test
The C++ loop design positions these elements
So that [...]