Wyo C++ - Ch. 4 Notes
Objective #1: Understand the different variable types used in C++ and
how they differ from constants.
- Variables are used to store values in virtually every computer program.
- Since values can either be whole numbers, decimal numbers, letters (i.e.
characters), or whole words (i.e. string values), you need to first choose
an appropriate data type when you use a variable. For example, if you
want to store coffee in a container, you would use a styrofoam cup and not
a Dixie cup. Otherwise, you would burn your hands. Likewise, if you want to
store chicken soup in a container, you would use a bowl and not a plate. Otherwise,
the soup would spill off of the edge of the plate.
- Most computer languages have a select number of different data
types.
- You must select the proper data
type for each variable that you use in a program in
order to program efficiently.
- this decreases memory (RAM) usage
- this increases the speed of your program
- The following data types
are common in C++ and will be used in this course:
- To store whole numbers in a variable, we use a variable of the int
data type.
- An int variable uses 4 bytes
of memory.
- An int variable can store a number as low as -2,147,483,648.
- An int variable can store a number as high as 2,147,483,647.
(Ignore our textbook which may say that the int data type uses only
2 bytes of memory and can only store values up to 32,767. The textbook
is slightly out-of-date in this regard.)
- To store decimal numbers in a variable, we use a variable of the double
data type.
- A double variable uses 8 bytes of memory.
- A double variable can store a number as low as -1.7 x 10308.
- A double variable can store a number as high as 1.7 x 10308.
- A double variable can store a number with up to 15 digits of precision
(significant digits.)
- To store a letter or a single character (such as #, $, *, etc.), we
use a variable of the char data type.
- A char variable only uses 1 byte of memory.
- A char variable can only hold one letter, digit, or character.
- To store a word or phrase (string value), we use a variable that is
a string. Actually, we will call this an apstring later in the
course. Technically string is not a data type but rather an object. We
will study apstring objects in more detail in Ch. 6.
- There are other data types that are used in C++ but we will not use them
too often in this course. These include unsigned char, short, unsigned int,
long, and unsigned long for whole numbers and float and long double for decimal
values. The data type bool is useful to store true and false values
but we will simply use an int variable with either a 1 value (to represent
true) or a 0 value (to represent false) if necessary in this course.
Objective #2: Be able to write declaration statements that declare
variables. Also be able to write initialization statements that initialize variables.
- Variables must be declared before they are used in C++. Get into
the habit of doing this at the top of your program.
Examples of declaration statements with explanatory inline comments:
char grade;
// a students semester grade
int numStudents;
// number of students in our class
double price;
// price of item
apstring userName; //
user's name
- Variable names are technically known as identifiers
. You can choose your own variable names but you must be careful to use valid
ones. Otherwise, the compiler will be confused and errors will result. Identifiers
are really just tags for memory addresses similar to how the Web page address
www.microsoft.com is a tag for an IP address. When choosing your variable
names:
- do not use keywords that are defined in the programming language
- do not include spaces or other disallowed characters
- do not use more than 31 characters
- begin the identifier with a letter
- Use a conventional method of making your variables easy to read at a quick
glance. For this class, we will begin variable identifiers with lowercase
letters (eg. grade). However, if you wish to use more than one word within
the identifier, you must capitalize the following words or parts of words
(eg. semesterGrade, billGatesWealth). Please do not
separate successive words with underscore characters ( _ ) as you may have
seen in the textbook or other programming courses (eg. semester_grade, bill_gates_wealth).
- You should usually initialize your variables at the same time that you declare
them. This is done with a declaration statement that is also an initialization
statement. C++ does not automatically initialize all variables to the value
0 like some other programming languages. If you do not initialize a variable
to a certain value, the variable will have an indeterminate
value (also known as "garbage value"). Garbage values
can corrupt the logic of your program and cause errors when you least expect.
Examples:
int numberOfPizzas = 3;
double monthlyCarPayment = 685;
char letterGrade = 'A';
apstring firstName = "Bill";
Note that char values must be surrounded with single quotes and string
values must be surrounded with double quotes.
- true initialization occurs within variable declaration statements
only. An assignment statement that assigns the value of zero to a variable
towards the top of a program technically is not considered initialization.
In the following example, initialization technically does not occur:
int numPizzas;
numPizzas = 10;
- For this course, please do not initialize multiple variables in a single
statement with commas even though your textbook and other C++ programmers
may do so. Rather, you should use multiple statements for clarity and the
preferred style for this course.
Instead of
int num = 0, salary = 0;
or
int num = 0,
salary = 0;
please use
int num = 0;
int salary = 0;
- As an exercise, write a program that prints out the indeterminant value
(garbage value) of an uninitialized variable.
Objective #3: Use constants.
- Sometimes you need to use the same value many times throughout a program.
In this case, it is proper to use a constant rather than a variable.
- For example, if you need to use the number 12 many times because of the
fact that there are 12 items in a dozen then it would be wise and efficient
to use a constant named DOZEN that is initialized to the value 12 rather than
to type the number 12 itself throughout your program. Using the constant named
DOZEN makes your program easier for others to understand. It also makes the
program easy to upgrade down the road. For example, if the government were
to change the standard that there are 13 in a dozen rather than 12, then it
would be easier to update your program if you had used the constant DOZEN.
I admit this is a far-fetched example but it can definitely be more efficient
to use constants in such situations.
- Many programmers use the convention of having all uppercase letters in constant
identifiers. Use the underscore character ( _ ) between consecutive
words. This allows other programmers to be able to "pick out" your
constants at a quick glance.
- Examples:
- const double PI = 3.14159;
- const double PA_SALES_TAX = 0.06;
- const int SPEED_OF_LIGHT = 299792458; //
commas can't be used here
Objective #4: List and explain the 5 steps of the Programming Process. (This
objective is from Ch. 2 of our textbook.)
- The 5 steps of the Programming Process are:
- Define the problem and understand the given specifications. Create
a test plan during this step.
- Develop an algorithm by writing pseudocode.
- Code the program by writing it out on paper first.
- Test and debug the program.
- Document and maintain the program.
- We will discuss and use the 5 steps of the Programming Process in our first
programming assignment. You must memorize these steps however at this time.
Objective #5: Explain and appreciate the importance of using good C++ coding
style.
- comments can be inline (using // ) or block ( using /*....*/ ). However,
you should only use the old-fashioned C-style comments (/*...*/) when commenting
out blocks of code as you debug a program. Use // comments in your programming
assignments before submitting them for a grade.
- For this course, you will be required to follow our Coding
Standards. Otherwise, you will lose points on assignments even if a program
happens to execute successfully.
- Since it is extremely rare for a programmer to work alone, his or her code
must be read by coworkers from time to time. It is important to observe common
coding standards so that others can read & understand your code with minimum
difficulty. Also, others will need to integrate your code into their program,
so following consistent coding standards will help avoid syntax and lexical
errors.