Uploaded by bob smith

C++ Variables

advertisement
C++ Variables
-Variables are used to temporarily store data within a program
-When you run the program, variables are stored in the computer’s memory
-When a value is written in a memory location the value replaces the previous value in
that location
-When you exit your program, the variables are deleted
-We have to first declare a variable before we can use it in C++. Every variable must have a data
type and a variable name
-In C++ you can declare variables at different places in the program. They have different
meanings and scope depending on where they are declared.
-For now, we will declare all our variables at the beginning of main()
-Examples of variable declaration
-int wordCount; (creates an integer variable)
-float unitPrice, totalPrice; (creates two float variables)
-double result; (creates a double variable)
-char grade; (creates a character variable)
-const double PI = 3.14156265; (creates a constant that’s value cannot be changed
inside the program
-Comments start with /* and end with */ or for only one line start with //
Naming Rules for Variables
-Variable names can range from 1 to 255 characters
-Begin all variable names with a letter of the alphabet. After that, variable names can also
contain numbers, but no spaces or special characters (except “_”) is allowed
-You cannot use C++ reserved words as variable names
-Use meaningful names that are easy to read
-C++ is case sensitive. All uppercase names usually indicate constants.
-camelCase is when you use a lowercase letter at the beginning of the name and uppercase
letters for the beginning of other words in the name
Integer Types
-Numerical integer types are used to represent integers
-The basic type is int (the default is signed), which is guaranteed to have a width of at least 16
bits, however on 32/64-bit systems it is almost exclusively guaranteed to have width of at least
32 bits.
8-bit = Up to 256
16-bit = Up to 216
32-bit = Up to 232
64-bit = Up to 264
-Use the command sizeof(datatype) to check the size of data types
-Modifiers modify the integer type and can be mixed in any order. Only one of each group can
be presented
-Signedness
-signed- tar (idk she flipped the slide)
Examples:
Signed Integers = short, int, long, long long
Unsigned Integers= unsigned short, unsigned, unsigned long, unsigned long long
Floating-point types
-Used to represent real numbers such as 3.14 or 0.01, with different levels of precision
depending on which type is used
-float: single precision type (32-bit)
-double: double precision type (64-bit)
-long double: precision and range not less than double
-In this class, when using fractions just use double all of the time
Boolean and character types
-Boolean type, known in C++ as bool, can only represent one of two states, true or false
-Character types are used to represent a single character such as “a”
-The basic type is char, which is a one-byte (8-bit) character
Assignment Operator
-Assignment Operator assigns what on the right side to a variable on the left
-Basically, just assigning values to data types for calculations and stuff
-Assignment operator looks for things on the right side of the equation first, so all variables on
the right side must have values
Strings
-The string class is a compound type and it is used to store sequences of characters such as
words or sentences
-Put #include <string> before the int main
-Define it with a name like any other variable
Download