On the surface, a good portion of programming may seem like mathematics, which to an extent it is. There appears to be operators, operands, expressions, and variables throughout code. We know what they mean, but to a computer it doesn't initially understand these terms and symbols. Only when a programmer provides these definitions does it act accordingly. Because of this nature, we have the ability to redefine some of these terms and symbols to be more flexible. In this lesson, we will focus on this flexibility as we discuss comments, primitive data types, objects, data types, these terms and symbols to get an idea of how programming works comments, the structure of declaring a data type and statements. On top of that there will be new header files to include in the #include directive, along with the introduction of objects. The first thing to do is to create documentation for the programmer.
/*
Block text
*/
// Line comment
Comments are one or more lines of text that are ignored by the compiler. They are used for documentation purposes to help users understand the program. A line comment, denoted by the two forward slashes (//), will cause the compiler to ignore any text after it that's on the same line. Caution, anything before the symbol and any line after will be read by the compiler. To have the compiler ignore multiple lines of text, use a block text comment. A block text comment is enclose with the (/*) symbol in the beginning, and the (*/) symbol at the end. Sometimes, programmers use comments to omit code to tell the compiler to ignore a specific section. This method can help determine the issue, but there are more useful options.
In programming, a variable is a unique identifier that references a location in memory that can store a value of that specific type. To declare a variable, the statement needs at least a data type and variable name.
data_type variable_name;
int feet;
A variable's name should strictly start with a letter or underscore (_), followed by a combination of letters, numbers, or additional underscores. Both spaces and symbols (with the exception of the underscore) are not allowed when naming a variable. It is illegal to use a keyword that is already reserved by the C++ programming language. A variable's name should have meaning, so programmers aren't confused. For example, a variable named inches has more meaning than a variable named x.
//Not Allowed
It is best to define a variable right away by assigning it a value. Not doing so can potentially assign a random value. This happens when designating a memory location to a variable, as that location may already be filled with data from a previous program. Defining a variable will provide an empty location of memory, thus avoiding the issue.
int 1st_var; //has digit as first character
int turning-point; //has (-) which is a symbol
int feet to inches; //has spaces between
int cout; //has a reserved keyword
//Allowed
int var_1st;
int turning_point;
int feet_to_inches;
data_type variable_name = expression;
int inches = 0;
It is possible to refer to a variable throughout the program, and even reassign a variable's value.
variable_name = expression;
inches = 2;
Reassigns a new value to an already declared/defined variable, thus overwriting the existing value. The data type doesn't need to be expressed, since it was stored into memory when the variable was defined. Note, the equal sign (=) is called the assignment operator and does not mean “equal to”. This is later explained in the arithmetic and mathematical functions section. For now, know that the assignment operator assigns the right value to the left variable. Depending on the data type appended at the beginning, a variable can hold integers, decimals, strings, and more.
From the various data types used in C++, the most fundamental are number types.
short number_of_students_drop = -7;
unsigned long long humans_on_earth = 7800000000;
Number types include both integer and floating-point data types. Integer types specify a variable as including whole numbers and negative whole numbers, but exclude decimals. To reference a variable as an integer type use the keyword int. To describe int in more detail, it is possible to use modifiers. Modifiers change the range and sign of an integer type. Range modifiers change the minimum and maximum value an integer type can have. From the smallest range to the biggest range, the following modifiers can be appended before the keyword int: short, long and long long. In addition, there are sign modifiers that include or exclude negative values, and are called signed and unsigned respectively. These modifiers can be attached in front of a range modifier, or attached to the int data type itself. If the program requires more precise number values, there are specific data types for the situation.
Floating-point types specify a variable as holding both decimal and integer values. From the smallest decimal places to the biggest decimal places, floating-point types come in a range of float, double, and long double. Caution, floating-point types don't use sign modifiers. The reason we don't always use floating-point variables is because integers can be more efficient with storage. The following table shows typical memory size of each data type along with a rough estimate of their range (table 2.1).
Data Types | Typical Range | Typical Memory Size | |
---|---|---|---|
Integer Types | signed long long int | -9,223,372,036,754,775,808 - 9,223,372,036,854,775,807 | 64 bits |
unsigned long long int | 0 - 18,446,744,073,709,551,615 | ||
signed long int | -2,147,483,648 - 2,147,483,647 | 32 bits | |
unsigned long int | 0 - 4,294,967,295 | ||
signed int | -2,147,483,648 - 2,147,483,647 | ||
unsigned int | 0 - 4,294,967,295 | ||
signed short int | -32,768 - 32,767 | 16 bits | |
unsigned short int | 0 - 65,535 | ||
Floating-point Types | float | range of about ±10^38 and about 7 significant decimal digits | 4 bytes |
double | range of about ± 10^308 and about 15 significant decimal digits | 8 bytes | |
long double | Just a lot bigger than double | 12 bytes |
Using the table above, we will examine the range of number types. For instance, notice how it says bits for each row under Typical Range. A bit can hold only two values: 1 (electricity going through) or 0 (no electricity going through). That being said, by adding more bits their can be a combination of 1s and 0s, with each combination representing unique value. For a number type's range, each combination of bits - which have a radix of 2 - can represent a numerical value of radix 10. For example, 000 is the equivalent of 0 and 001 is the equivalent of 1 and so forth. For more information on binary arithmetic, please visit the digital design lectures. When it comes to unsigned integer types, the range is from 0 to 2n - 1. Where (n) is the number of bits and 2 represents the base for radix 2. For signed integers, the range is from -(2n/2) to ((2n/2) - 1). These equation can explain our range for integer types. Looking at both signed long int and signed int, notice that they have the same range. That's because these values aren't exact, and only represent an estimation since every system has different ranges. Though, it is safe to assumed that signed long int is at least the same size of signed int, if not bigger which it usually is. Typing integer types for each variable can be time consuming, which is why there are shorthand versions. To save time, it is automatically assumed that a variable is signed, unless specified. Further, it is possible to remove int from any of the range modifiers. Applying the two recent rules to integer types, the following table is how programmers represent those data types within their code.
Data Types | Typical Range | Typical Memory Size | |
---|---|---|---|
Integer Types | long long | -9,223,372,036,754,775,808 - 9,223,372,036,854,775,807 | 64 bits |
unsigned long long | 0 - 18,446,744,073,709,551,615 | ||
long | -2,147,483,648 - 2,147,483,647 | 32 bits | |
unsigned long | 0 - 4,294,967,295 | ||
int | -2,147,483,648 - 2,147,483,647 | ||
unsigned int | 0 - 4,294,967,295 | ||
short | -32,768 - 32,767 | 16 bits | |
unsigned short | 0 - 65,535 | ||
Floating-point Types | float | range of about ±10^38 and about 7 significant decimal digits | 4 bytes |
double | range of about ± 10^308 and about 15 significant decimal digits | 8 bytes | |
long double | Just a lot bigger than double | 12 bytes |
Regarding both floating-points and integers, an integer expressions can be assigned to a floating point variable with no issues, but a floating point expression can't be assigned to an integer variable. If you try to assign a floating point value to an integer variable, the integer variable will only keep anything before the decimal point, thus loosing information. In the situation where you want to change the data type, you can use static cast.
static_cast<data_type>(expression);
float more_precise_inches = static_cast<float>(inches);
Besides holding integers and decimals, a variable can also hold one or more characters.
string username = "xXUltimateGamer42Xx";
char multi_choice_answer = 'A';
To store one or more characters, use the string type and the character type. Before declaring these data types, it is always a good idea to implement the string header file in the #include directive. To declare a string type variable use the keyword string. Like the previous lecture, a string value is a collection of characters encapsulated in double quotation marks ("") and can hold zero or more characters. A character type is denoted as char and is attached to the beginning of a variable which can hold a single character in single quotations (''). The characters are based off the American Standard Code for Information Interexchange (ASCII), and consist of numbers, symbols, and letters. According to the C++ language, there is a difference from the character numbers and integer numbers. For example, the character “1” acts more like a symbol, while the integer 1 acts more like a digit you can use during calculations. Both string and character types are already part of the C++ standard library, and can specifically be found in the string header file. Sometimes, characters can get misinterpreted by the compiler because of its significance in the C++ language. To avoid this issue with strings, use the following escape sequences to let the compiler know it's a character.
Escape Sequences | Definition |
---|---|
\' | Compiler considers single quote as part of the string |
\" | Compiler considers double quote as part of the string |
\? | Compiler considers question mark as part of the string |
\\ | Compiler considers backslash as part of the string |
enum data_type_name {value1, value2, ..., valuen};
enum guitar_string {E, A, D, G, B, e};
Enumeration is a user defined data type that establishes a finite set of values. In our example, the data type is guitar_string, which has six possible values as represented within the curly-braces ({}). To use the user defined data type with variables, append the user defined data type before the variable as if it were a string or integer.
guitar_string plucked = D;
const data_type variable_name = expression;
const float GRAVITY = -9.81;
A constant, denoted by the keyword const, is a type qualifier that prevents any variable's value to change during run time. The type qualifier can be attached to the beginning of any data type listed in the previous table. It is considered good practice to capitalize constant variables to differentiate them from regular variables. Avoid using magic numbers when defining constants. Magic numbers are values that don't have any significant meaning. In the example, -9.81 has significant meaning since it is the amount of force (in newtons) gravity has on objects in Earth.
Both groups of data types have unique rules when it comes to assigning an expression (in this case the value on the right of the assignment operator) to their variable.
Just like regular math, arithmetic expressions in C++ can include addition (+), subtraction (-), multiplication (*), and division (/) operators. C++ implements an additional operator called the modulo operator (%) , which finds the remainder when dividing. Note, the modulo operator is only used with integers, not with floating-points. Furthermore, arithmetic expressions can use variables as their operands, along with integers and decimals. The programming language also includes an exponential notation operator (E), to make it easier for programmers to do scientific notation. For exponential notation, know that the base is 10. Like math, operators have an order of precedence. The following is a list of common operators' precedence from high to low:
Level of Precedence | Operators |
---|---|
High | Parenthesis () |
Exponents xn | |
Multiplication (*), Division (/), and Modulo (%) | |
Low | Addition (+), Subtraction (-) |
Some operators are grouped together since they have equal precedence. Use the left-to-right rule as you would in math.
On top of these operators, C++ has included increment (++) and decrement (--) operators because of how common we use them in programming. Both operators are the equivalent of a variable adding/subtracting 1 to itself, and assigning the new value to itself:
x++; //same as x = x + 1;
x--; //same as x = x - 1;
In math terms, x cannot possibly equal x + 1. Though, in programming terms, the equal sign (=) is the assignment operator. Remember, the assignment operator just assigns the right expression to the left variable, either initializing the variable's value or overwriting the existing variable's value. Since the variable on the left side of the assignment operator is often used in the right expression, C++ has expanded on its assignment operator. This includes, but are not limited to, the addition assignment (+=), the subtraction assignment (-=), the division assignment (/=), and the multiplication assignment (*=) operators:
a += 2 //same as a = a + 2;
b -= a + 1 //same as b = b - a + 1;
c /= 4 //same as c = c / 4;
d *= 3 * c//same as d = d * 3 * c;
To use more complicated math expressions, implement the cmath header file in the #include directives. This file includes a set of C++ function calls that act like their math counterparts. A function call orders a function defined in the header file or program to execute and possibly return a value by calling its name and optionally providing additional values within the parentheses.
function_name(parameter1, parameter2, ..., parameterk);
To avoid confusion, both math functions and C++ functions are two different meanings. A function in math is denoted by (f) and is a rule, stating every element (x) in set A has one element (f(x)) in set B. A function in C++ carries out a process depending on the content of the block statement and the function's parameters. Unless it is specified that we are using math functions, assume we are referring to C++ functions. The following is a list of common functions in the cmath library:
Functions Calls | Description |
---|---|
pow(x, n) | x to the n power |
sqrt(x) | the square root of x |
cbrt(x) | the cube root of x |
exp(x) | e to the power of x |
log(x) | natural log of x |
log10(x) | common log of x (base 10) |
cos(x) | cosine of x (in radians) |
sin(x) | sine of x (in radians) |
tan(x) | tangent of x (in radians) |
acos(x) | arccosine of x (in radians) |
asin(x) | arcsine of x (in radians) |
atan(x) | arctangent of x (in radians) |
For a complete list of the functions please google cmath C++.
When it comes to readability it's best to follow common practices. Put space between binary operators (+, -, *, /, and %), but don't put space between unary operators, for example, the negative sign (-). Do not put space between a function name and its parenthesis, but put space after every C++ keyword.
Like arithmetic, string variables are allowed in string expressions, and can even be concatenated (put together) with the plus operator (+). Note, it is possible to put strings and string variables together during concatenation, but at least one operand must be a string variable on either side of the plus operator. It is not possible to mix string data types and char data types during concatenation.
string h = “Hello”;
string w = “World”;
string hw = h + “ “ + w;
It is a good time to point out that the string header file acts like a class. It will take some time to clarify, which is why we will go into more detail in the Classes and Overriding lectures. For now, consider a class as a collection of functions that an object (in this case our variable) can use. The declaration of an object is an instance of a class that can provide additional information for the class' functions to utilize. The process of using a class' function is called a member function call and looks like the following:
variable_name.function_name(parameter1,parameter2,…,parametern);
hw.length();
The length function returns the length of the string variable (hw) as a positive integer. This is one of the many functions used in the string header file. The following are a couple of essential string functions, and to find out more google “string header file c++”.
Function Name | Definition |
---|---|
hw.length() | Returns length of string variable as a positive integer |
hw.substr(i) | Returns substring of hw from index i to end of string |
hw.substr(i, n) | Returns substring of hw from index i to the length of n |
getline(f, s) | Read string s from the input of stream f |
With the substr(i) function, notice how it mentions index. Each character of the string is indexed with a number from 0 to k. Meaning the first character of the string is 0, and the last character of the string is kth index. Back then, computers started counting at 0 instead of 1. Though we have the capability to start at one, tradition has been kept for compatibility reasons. As for the process of reading string s from the input of stream f, it will be explained in the output and input statements section.
Just like the previous lecture, an output statement is composed of the keyword cout from the C++ standard library, and one or more expressions. Each expression has the double less-than operator (<<) appended to the beginning of it:
cout < expression1 < expression2 < … < expressionn;
string period = “.”;
cout < “The answer to life is “ < 4 * 10 + 2 < period < “\n”;
Output statements print the value of each expression to the screen. In our case it is the terminal. It is possible to put integer, floating-point, char, string, and enum variables in the output statement. Note, it is possible to bypass the variable definition and place the expression in the output statement.
Sometimes output doesn't come out as expected. To format output, include the iomanip header file in the #include directive. Once the header file is included, it is possible to use the following functions in the expressions of cout statements.
Command Name | Description |
---|---|
setw(x) | sets the width of the next output field depending on variable x. |
setprecision(x) | Sets the subsequent floating-point numbers depending on x. |
fixed | Prints trailing zeros. |
As for input statements, they are composed of the keyword cin from the C++ standard library, and one or more variables. Each variable has the double greater-than operatory (>>) appended to the beginning of it:
cin >> variable1 >> variable2 >> … >> variablen;
string month_of_birth = “”;
string day_of_birth = “”;
int year_of_birth = 0;
cin >> month_of_birth >> day_of_birth >> year_of_birth;
Input statements obtain input from the user and store it in one or more predefined variables for the program to use. Caution, even if not explicitly stated, the new line escape sequence (\n) is added at the end of each input statement. In the instance where a user types more than one word (a sentence) for a string variable, the variable will only take one word before it reaches a space and either dumps the rest of the sentence or stores it in another string variable. To store a sentence in a string variable, use the getline function.
getline(cin, variable_name);
string full_name = “”;
getline(cin, full_name);
Caution, if a getline function is called after cin queries input, then the function will read the leftovers from the previous cin. For example, remember how every cin leaves a (\n) escape sequence, if getline is called then the function will read and execute the (\n) before it can even query the user for information. To avoid this, use the following command before getline:
cin.ignore(256, '\n');