Wyo C++ - Ch. 10 Notes
Objective #1: Use enum.
- The keyword, enum, allows
you to create data types of your own. You must also specify the legal values
for the new data type when you create it. The following statement could be
used to create the data type, Sundaes,
enum Sundaes {chocolate, banana, vanilla, strawberry};
- You should capitalize an enumerated data type that you create so that it
is easy to differentiate it from built-in C++ data types such as int, double,
etc.
- After an enumerated data type is created, you can then declare variables
of that new data type. For example, the following statement declares
two variables, mySundae and yourSundae, as variables of the data type, sundaes,
Sundaes mySundae, yourSundae;
- The C++ compiler then identifies the values listed within the braces of
the original enum statement with the values 0, 1, 2, etc. In the example above,
chocolate would have the actual numeric value of 0, banana would have the
value of 1, etc. The statement,
mySundae = chocolate;
would assign to the variable, mySundae, the value, chocolate. However, the
C++ compiler actually assigns mySundae the integer value of 0.
- If you wish to assign your own values to the predefined values, you could
use a statement like,
enum Sundaes {chocolate = 250, banana = 180, vanilla = 190, strawberry
= 200};
for example, where the numeric quantities may refer to the number of calories
in each type of ice cream sundae. However, you could not use the same value
for two or more different types of ice cream sundae.
- The keyword typedef
can be used to give a new name to an existing data type such as int. If you
wish to use the name wholenumber to declare a variable of type int, then you
can give the data type an alias of wholeNumber with typedef.
typedef int wholeNumber;
Objective #2: Understand what structures are
and how to use them.
- Structures
are used in C++ to group variables together in order to make one's programming
task more efficient. Any combination of variables and arrays can be combined
into one structure. This is a useful and efficient way to store data. For
example, the structure
struct Student
{
apstring socSecNum;
apstring lastName;
apstring firstName;
int pointsEarned;
double gpa;
};
allows the programmer to keep related data referring to individual students
together. Notice that this example groups strings, integer, and double variables
together into one structure. Each of the different variables are called
members of the structure.
You still need to create an actual variable of the type,
Student, before you make use of this structure. This would be done with
a statement like,
Student freshmen;
which creates a variable called freshmen of the type Student (which happens
to be a structure as opposed to an int or a float.) Then, to assign a GPA
of 3.4 to the GPA field of the variable, freshmen, you would use the statement,
freshmen.gpa = 3.4;
The period symbol (.) that is used between the structure identifier, freshmen,
and the member, GPA, is called the dot operator. The dot
operator simply allows the
programmer to reference individual members of a structure.
- It is possible to nest a structure inside of another structure. They are
called nested structures in this case. That is, one struct variable
can be a member of another struct variable.
Example -
struct Name
{
string firstName;
string lastName;
};
struct Student
{
Name wholeName;
string socSecNum;
int pointsEarned;
double gpa;
};
- The struct definition for Name must appear before the struct definition
for Student since the compiler must know what a Name is since it is used within
the definition of a Student.
- To refer to one of the members of the nested structure it is necessary to
use the dot operator twice as in the following example:
Student classClown;
classClown.wholeName.firstName = "Robert"
Please note that some of the following topics are
not tested on the AP Computer Science A Exam. AP students should check College
Board's Web site for specific details on what topics ARE part of AP C++
A Subset and what topics ARE NOT part of the A Subset.
Objective #3: Understand the basics of pointers.
- A pointer
is a variable or a constant that holds a memory address. That is, a pointer
named, gradesPtr, holds a memory address such as 334712, in which the value
of the variable, grades, is stored. Just like "1600 Pennsylvania Avenue"
is the street address of the President and not the actually the President,
himself, a pointer "points" to where some variable is stored in
the RAM (memory) of the computer. C++ programmers rarely need to know the
actual memory address (which is probably a hexidecimal number) of a variable,
but using pointers does help programmers handle data more efficiently sometimes.
- An integer data typed variable is stored in 4 contiguous bytes of memory
in Visual C++, version 6 for Windows 95/NT. If you created a pointer that
pointed to that integer variable, the pointer would store the memory address
of the first byte (of four) of the integer variable. Since a pointer variable
itself needs 4 bytes of memory of storage, it may not seem efficient to use
a pointer to refer to a variables of certain data types when it uses up the
same amount of storage. There are however advantages to doing so that you
may learn later in your programming career.
You have already used pointers earlier in this course when you declared
character arrays, as in:
char stateAbbreviation[3];
stateAbbreviation is actually a pointer. It "points" to the first
character of the character array. Since there is a null terminator at the
end of every character array, C++ can determine how long a character array
is and having a pointer point to the first character in the array is all
that is necessary to keep track of the character array.
Objective #4: Declare pointers.
- A pointer is declared similarly as other types of variables except that
the dereferencing operator (*)
is used to indicated that it is a pointer.
int numberOfStudents;
int *pNumberOfStudents;
In the example above, numberOfStudents is simply a variable of type, integer,
while pNumberOfStudents is a pointer variable which points to an integer
value. Many programmers name pointer variables by placing "p"
at the beginning of the identifier (i.e. variable name). This makes it easier
to read your code because the pointer variables stick out in the program.
- Note that there is NO space between the dereferencing operator (*) and the first letter
of the pointer's variable name.
Objective #5: Use the address-of operator and dereferencing operators.
- The address-of operator is the & symbol.
- The address-of operator (&)
returns the "address of" the variable rather than the
variable's contents (i.e. its actual value.)
- You should interpret (i.e. say to yourself) "the address
of" every time that you see an & symbol.
- For example, the following line of code:
pSum = &total;
should be read as "pSum stores the address of the variable total"
- The dereferencing operator
is the * symbol.
- You should interpret (i.e. say to yourself) "that which
_____ points to" every time that you see a * symbol.
- For example, the following line of code:
int *pMoney;
should be read as "that which pMoney
points to is an integer data type"
- The dereferencing operator can be used in at least 3 ways:
- It is used in the declaration of a pointer variable as you read
above.
- It can also be used within a program to reference that actual value
of another variable, possibly having it printed on the screen or used
in practically any other way.
- Example:
int *pResult;
int numOne, numTwo;
int answer;
answer = numOne * numTwo;
cout << "The answer is " << answer
<< endl;
The code above would work exactly the same if answer in the final
cout statement was changed to *pResult
and the line,
pResult = &answer;
was added after the statement: int
answer;
- It can also be used to actually change the value of a variable without
using referring to the variable, itself!
- Example:
int *pSneaky;
// "that which pSneaky points to is an integer
int innocentVariable = 100;
pSneaky = &innocentVariable;
*pSneaky = 0;
cout << innocentVariable << endl;
The code above causes 0 to be printed out even though innocentVariable
was initialized to 100 and never directly changed via an assignment
statement later in the program!
Objective #6: Use pointers with character arrays.
- As we have stated before, a character array in C++ is actually a pointer
constant that points to the first character in the character array. You'll
note that the dereferencing operator (*) is not used with the name of the
character array because it is a pointer constant and not
a pointer variable. That is, after the declaration,
char stateAbbrev[3] = "PA";
has been made the pointer constant, stateAbbrev, cannot be reassigned to point
to anything besides the 'P' even with something like the statement, stateAbbrev
= "MD".
Objective #7: Use subscript notation with built-in
C++ character arrays.
- While a string variable or a built-in C++ character array is stored in continguous
memory locations, C++ numbers the individual characters in the array beginning
with index position
0. So after the declaration statement,
char stateName[11] = "New Jersey";
or
string stateName = "New Jersey";
is made, the 'N' is considered to be in the index position 0, the first
'e' in the index position 1, the 'w' in the index position 2, and so on. Note
that the index position 3 contains a blank space, which is, of course, a valid
character. In the case of built-in, C++ character arrays, the null terminator
('\0') which marks the end of the character array, is located in the index
position 10.
- You may use subscript
notation to change the value of one specific character
within a character array or a string variable. The statement,
stateName[1] = 'E';
would change the character array or string variable to "NEw Jersey".
- The dereferencing operator (*) can be used to change the contents of a character
in a character array. (This will not work with string variables though.) The
statement,
*(stateName + 6) = 'R';
changes the character array to "NEw JeRsey", since the 'r' was in
the index position 6 and is now replaced with 'R'.