Team 2550
Technical Documentation
|
|
C++ has several types of data that you can store while the code is running. These include integer, floating point, character, and string. All of these have different mathematical and logical operations.
The data types are...
int
: a whole number that can be any value between -4,000,000,000 (billion) and 4 billion long
: a longer version of int that can store numbers much larger and smaller, as a consequence, it also takes up more RAM.long long
: self explanatorychar
: a single ASCII character. This type is stored as a value between 0 and 127. An ASCII character table showing the characters that correspond to the numbers is shown below. You can preform mathematical operations on them like normal integers.bool
: a logical (true or false) value, it can either be 1 or 0. float
: a number that can have a decimal value. It has 7 significant digits and can have a power of ±38double
: a larger version of float with 15 significant digits. It also can have a power of up to ±308long double
: a longer version of doublestring
: a user defined variable type, it can be any size, but you cannot preform mathematical operations in the same way that you can with any of the above variables.float
or double
values.You can put an “unsigned” in front of any of these variable types in order to make the numbers positive only. This allows them to be twice as large, but be careful, if you do an operation that turns it into a negative value, it will turn into an extremely large number.
Example:
unsigned int num = 5; // creates a variable of type integer called num and sets it to 5
'A'
. To see an ASCII table, go to http://en.cppreference.com/w/cpp/language/ascii.You can create a variable in C++ by specifying the type and name.
int num; // this is valid code, you can declare variables in C++ without giving them a value
The reason allocation without initialization is possible is because assigning a value to a variable takes a significant amount of time, so it is pointless to assign every variable you create to 0 if you are going to make it nonzero before it is used. For instance, if you are using a variable for user input, then you can create (allocate) it and give it a value later. Long story short, don't assign an initial value to a variable unless you are going to be doing calculations on it directly.
x
, call it something like userInput
. Its name should be self-explanatory.Declaring a constant is almost the same as declaring a variable. Just put a “const” in front of the type.
You can perform all of the basic math operations on numeric variables. The following operations are built into C++, they are sorted by the PEMDAS order of operations.
C++ Order of Precedence**
pow
(power) that fills this role.C++ Operators**
Name | Symbol |
---|---|
Assignment | = |
Addition | + |
Subtraction | - |
Multiplication | * |
Division | / |
Modulus | % |
The mod[ulus] operator calculates the remainder. If the result of a division yields a decimal result when using integer math (14/4 is a good example), you will end up with a whole number answer that is not estimated, it simply ignores any decimal digits.
We know:
14/4 = 3.5
However, using integer math, this expression evaluates to 3, but there is still a remainder of 2.
In order to get the remainder, use the expression 14 % 4, the resulting value will be 2.
The mod operator can be useful in many different situations. You can use it to:
When programming, you often want to add a certain value to a variable and store the result back into that same variable. C++ has a few operators to do that. These shorthand expressions can be seen to the left.
One thing worth noting is that x++
and ++x
evaluate to the same thing, but there is a difference in some cases, as shown in the code below.
++x
operator instead of x++
. Internally, x++
may have an extra copy operation. Use ++x
if you can.Shorthand Operations**
Shorthand | Action |
---|---|
x++; | x = x + 1; |
x–; | x = x - 1; |
++x; | x = x + 1; |
–x; | x = x - 1; |
x += 5; | x = x + 5; |
x -= 5; | x = x - 5; |
x *= 5; | x = x * 5; |
x /= 5; | x = x / 5; |
Comparison operators can compare two different values.
Comparison operators**
Operator | Action |
---|---|
> | greater than |
< | less than |
>= | greater than or equal |
<= | less than or equal |
== | equal, evaluates to true if values are the same |
!= | not equal |
a == b; //evaluates to true, checks to see if a is equal to b a = b; //copies the value of b into a
To temporarily change the type of a variable, use the following code.
static_cast<type_to_convert_to>(variable_name);
The following code is valid...
(type_to_convert_to)(variable_name)
There are certain cases where the C++ compiler will automatically determine the type of math to use.
Condition (If...) | The result... |
---|---|
all operands (numbers) are of type int. | is an integer. |
all operands are of type float. | is a float. |
the expression has multiple types of operands. | The compiler will detect that there is a floating point number in the expression, and automatically use floating point math.
|
the value of a float is assigned to an int. | the number is truncated (cut off) at the decimal.
|
an integer expression is stored into a float variable. | Integer math will be preformed, even if the result has a decimal value. int a, b = 2, 3;
float x = b / a;
// x has a value of 0
|
This applies even if you are storing the value in a float variable. This could potentially cause some issues because you cannot always be sure that the numbers you are entering into the float have a decimal place. If they may not, then it is best to surround your expression in a static_cast statement.3 / 2 evaluates to 1.