SOURCE:WIKIPEDIA
Important Terms:
A variable is a named memory location which temporarily stores data that can change while the program is running. A constant is a named memory location which temporarily stores data that remains the same throughout the execution of the program. The type of a variable indicates what kind of value it will store. The name of a variable is known as its identifier. A variable can be given a value through an assignment statement. C++ recognizes eleven built-in data types which are designated by reserved words:
** The basic C++ types that are included in the AP Computer Science subset are int, double, bool and char.
|
Variables Most Often Used
Data Type | C++ Keyword | Stores | Bytes of Memory | Range of Values |
Character | char | 1 character | 1 | 1 character |
Short integer | short | Integers | 2 | -32,768 to 32,767 |
Integer | int | Integers | 4 | -2,147,483,648 to 2,147,483,647 |
Long Integer | long | Integers | 4 | -2,147,483,648 to 2,147,483,647 |
Float | float | Decimal values to 7 decimal digit precision | 4 | 3.4e-38 to 3.4e38 |
Double | double | Decimal values to 15 decimal digit precision | 8 | 1.7e-308 to 1.73e308 |
Boolean | bool | Boolean (Logical) values |
| True or False |
Rules for assigning variables:
Assign short, int or long data types when you are sure a variable is an integer number (NO decimal points). While these three integer choices are available, based upon the size of the numbers you are using, we will be using only int.
Assign float or double when decimals are needed. We will primarily be using double, as suggested by the AP Computer Science subset.
Assign char if the variable will always contain ONE character of data. This means only one letter (or character).
The names of variables in the C++ language are referred to as identifiers. The best naming convention is to choose a variable name that will tell the reader of the program what the variable represents. Variables that describe the data stored at that particular memory location are called mnemonic variables.
Rules for naming variables:
Variable names in Visual C++ can range from 1 to 255 characters. To make variable names portable to other environments stay within a 1 to 31 character range.All variable names must begin with a letter of the alphabet or an
underscore( _ ). For beginning programmers, it may be easier to begin all variable names with a letter of the alphabet.After the first initial letter, variable names can also contain letters and numbers. No spaces or special characters, however, are allowed.Uppercase characters are distinct from lowercase characters. Using all uppercase letters is used primarily to identify constant variables. You cannot use a C++ keyword (reserved word) as a variable name.
Tip: Some programmers use an underscore in variable names to separate parts of the name, such as shipping_weight. Others prefer a “capital style” notation, such as shippingWeight to separate parts of the name. | |
NEVER use uppercase for every letter in a variable name, because uppercase names are reserved for constant variables. |
Samples of acceptable variable names: | Samples of unacceptable variable names: |
Grade | Grade(Test) |
GradeOnTest | GradeTest#1 |
Grade_On_Test | 3rd_Test_Grade |
Declaring Variables
|
The equal sign (=) is used for assigning values to variables.
The format of an assignment is:
variable = expression;
The variable is a variable name that you defined in the program.
The expression is any variable, numerical, literal, or expression that produces a data type that is the same as the variable’s data type.
Data may be placed in a variable when it is declared: or data may be placed in a variable AFTER it has been declared |
Never put commas in numerical values that you assign to variables. |
Compound Operators: C++ has its own shortcut syntax involving the placement of values into numerical variables. You should be able to recognize all possible codings. x += y; is the same as x = x + y; While a powerful tool used to update variables, compound operators can be troublesome. In the order of operations, the compound operators have lower precedence than regular math operators. Check out these two examples:
Be careful when using compound operators. Remember the operator precedence. |
+ - * / % | Addition Subtraction Multiplication Division Modulus (also called remainder) |
|
Did you notice that there is no symbol for “exponent”? |
The MODULUS Operator % The modulus operator produces the remainder of dividing the first value by the second value. For example: 22 % 6 = 4 because 22 / 6 = 3 with a remainder of 4
|
| Confusing DIVISIONS Be careful when performing integer division. When dividing an integer by an integer, the answer will be an integer (not rounded). Compare these divisions: (5 is an integer while 5.0 is a double)
When an operation involves two types (as the mixed division shown above), the smaller type is converted to the larger type. In the case above, the integer 5 was converted to a double type before the division occurred. |
| |||||||
The cout statement may be used to print the information stored in any variable location to the screen. | |||||||||
int num1 = 4; |
or for easier reading:
cout |
Numbers and/or computations may also be printed without variable names:
cout // prints 976 on screen
cout // prints 40 on screen
Formatting Output
Displaying Amounts of Money:
|
Using cin >>
Sample program:// sample program for cin int main(void) cin can accept more than one variable as follows: cout > integer >> decimal; Character Literals and Variables
|
The Advanced Placement Computer Science (APCS) Committee is the committee responsible for creating the APCS Examination. They have created a variety of C++ tools, based on the C++ standard, for the beginning programmer. One such tool, features the creation of an apstring data type. The letters “ap” associated with these tools let you know that they are not part of your current C++ compiler, but were developed by the APCS Committee.
The AP* tool needed to use this new string variable is apstring.cpp, and this new variable is declared as being of type apstring. Using the type apstring is similar to using int, double, and char.
You must use #include “apstring.cpp”to have access to this special apstring data type. You must install the AP Classes in the “include” directory of your software before using them. A link to the AP Classes can be found on the C++ main (opening) page.
//sample program
#include
#include “apstring.cpp”
int main(void)
{
apstring CrewName; //declare variable
CrewName = “Neelix”; //initialize variable
return 0;
}
The use of the apstring class will eliminate all of the problems of dealing with character string arrays that we have seen. With the apstring class we can initialize string variables whenever and wherever we wish and the computer will automatically keep track of the length of the string. |
AP* is a registered trademark of the College Entrance Examination Board,
which was not involved in the production of this web site.
More Than You Wanted to Know
About Floating Point Variables
“Why do I tend to get warnings when I declare float variables?” Visual C++ defaults all floating point numbers to doubles. Since doubles are “larger” (or “smaller”) than floats and require 8 bytes (floats require 4 bytes), the program is most likely thinking that you are casting - forcing a variable to take on the properties of another type. It is trying to warn you that your “double” may lose some of its accuracy if changed to a smaller ranged type, such as a float. Just remember: To Visual C++, any number with a decimal point is a double. Since the Advanced Placement Examination deals primarily with doubles, we also will concentrate on the use of doubles. Floating point variables involved with integer division are often confusing. Consider the following situation:
1. If you declare slices as an int, and use you have integer division taking place on the right hand side, since 375 and 100 are ints. This integer calculation is completed and the result is placed into a float. You get a warning of possible loss of data due to the integer division (in this case). You get the WRONG ANSWER displayed. 2. If you declare slices as an int, and cast the right hand side you are forcing the machine to accept the value of the parenthesis as a float type. But the parenthesis is computed first and the integer division occurs. Now, there is no warning, but the WRONG ANSWER is displayed. 3. If you declare slices as a float, you have prevented “integer division” from occurring and the CORRECT ANSWER is displayed. 4. Suggestion: ALWAYS BE AWARE OF INTEGER DIVISION - AND AVOID IT! The simple addition of the .0 makes 375.0 a double and solves all of the problems of integer division occurring. Clear as muddy water, isn’t it!!! :- ) |
C++ is a superset of C and offers all the advantages of a procedural language. You can write a C++ program filled with for loops and if statements just as you would in C. But C++ offers much, much more. C++ allows you to create objects. An object is sort of like a struct on steroids. Understanding the value of objects is the key to understanding the popularity of C++.
Understanding Object Programming
As its name implies, an object programming language allows you to create, interact with, and delete objects. Just as a variable is based on a type definition, an object is based on a class definition. First, you’ll declare a class, then you’ll define an object (or many objects) said to be members of that class. While a struct definition is limited to data elements only, a class definition can include both data elements (called data members) as well as function pointers (called member functions). To make this clearer, let’s start with a simple problem and see how we’d solve it using both structs and objects.
No comments:
Post a Comment