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 [...]
Function polymorphism is a neat C++ addition to C’s capabilities
Whereas default arguments
Let you call the same function by using varying numbers of arguments
function polymorphism, also called function overloading
Lets you use multiple functions sharing the same name
The word polymorphism
Means having many forms, so function polymorphism lets a function have many forms
Similarly, the expression function overloading
Means you [...]
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 [...]
Passing structures by value makes the most sense when the structure is relatively compact
So let’s look at a couple examples along those lines
The first example deals with travel time (not to be confused with time travel)
Some maps will tell you that
It is 2 hours, 56 minutes, from Zero One City to Binary Falls
And 1 hour, [...]
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 [...]