Introduction
The concept of data abstraction deals with how data is held in a program, and how you, the programmer, can use it. The overall process itself is pretty simple, but there are some complicating factors. In all computers, data is held in a series of 0’s and 1’s, that is, binary. For example, a computer might hold the data value 19 as 00000000 00010011. Because humans are not too great at manipulating large quantities of numbers, most high level programming languages, such as C++, will manage low-level data for you. Therefore, all the programmer must deal with are variables. This "covering up" process is called abstraction—using a simpler concept to represent and interface with another. Your programming language abstracts the concept of low-level data, freeing you from worry about the finer details and giving you much simpler, human-understandable data.
Identifiers
All high-level programming languages support the use of variables, which are essentially little containers that contain the data of your program. Variables are accessed through identifiers, which are any uninterrupted (i.e., no spaces) combination of characters and digits. Identifiers are used to represent each part of a C++ program, including variables, functions, and much more. It is conventional for identifiers to follow the camelCase format, which means that the first word is not capitalized, and each following one is.
payRate userAge findMaxValue calcDiscriminant
Identifiers must start with either a normal character or an underscore, and can be as long as is practical. All strings of characters and digits are valid identifiers, as long as it is not specifically reserved by the C++ language. In general, you want to use noun identifiers for “things,” variables or other objects that contain information, and verb identifiers for functions, which perform actions rather than hold values.
Literals
While a variable is an identifier that holds a value, a literal is the value itself. You won’t be using too many literals in your programming, but keep in mind that they are specific pieces of information, such as a number, a letter, or a sequence of words. For example...
"Hello!” 5 345.7 'A'
...are all literals.
Data Types
In C++, there are four fundamental data types. Fundamental types represent the types of data which are stored in technically different formats within memory. All other types are simply built out of the four fundamental types; hence are called derived data types. The four fundamental types are...
Name | C++ Type | Information |
Integer | int | Any positive or negative integer. Usually, an “int” holds 32 bits of data; a number between -2^31 and 2^31 - 1. |
Character | char | Technically, a character is just an eight-bit integer that can only hold values between -128 and 127. The value corresponds to a character (a single letter or something like a bracket) using a standard convention. For example, the letter ‘G’ is represented by the value 71. |
Floating Point Value | float, double | Any positive or negative quantity (can be fractional). These values are held in memory in the form of scientific notation. double is 64 bits, and can contain a more precise value than float. |
Boolean | bool | Only the values ‘true’ or ‘false,’ but how bools are actually stored in memory varies. |
Granted, there are more fundamental types, but these are the four major classes. Other types are simply smaller/larger/unsigned versions.
Variables
Now, you must be wondering how to actually use variables. In C++, all variables are explicitly declared and strongly typed, which is to say to create a variable, you must declare its name and what data type it can hold. The syntax is simple: type the name of the data type you are using, then your variable identifier, then end the line with a semicolon. Get used to that—almost every line in C++ is ended with a semicolon. Finally, it is often conventional, but not required, to declare all variables at the start of a function.
int myAge;
Declares a variable named myAge with the type integer.
double payRate;
Declares a variable named payRate with the type double.
The assignment operator (‘=’) is used to assign a value to a variable. The assignment operator always takes the value on the right and assigns it to the identifier on the left. When assigning a literal integer or double value, you can simply type your number. However, when assigning a character value, wrap your character in single quotes, and when assigning a string, wrap your phrase in double quotes. Assignment can also be combined with the declaration.
myAge = 24;
The variable myAge now holds the value 24.
double payRate = 23.50;
This creates the variable payRate, and makes it hold the value 23.5.
char letter = ‘U’;
The variable letter now holds the letter ‘U’.
Now, later in your program, whenever you use your variable, the computer will retrieve and use the value held within.
Scope
Finally, there is the topic of scope. The scope of a variable is the "arena" in which it can be used, or to put it simply, the area between the opening and closing curly braces in which your variable was first declared. Keep this in mind when declaring variables, as they can only be used within their scope, and any scopes there within. This is because when a scope ends, all variables declared within it will be destroyed.
{ // – Begins the scope int myVar; // Here, myVar can be used. } // – Ends the scope // Here, it can’t.
Now, you may wonder what happens if a variable is not declared in any scope. This is called a global variable, and can be accessed from anywhere in your program. In general, don’t use these (except in specific situations), as global variables can lead to very hard-to-find bugs in your program.
Reserved Words in C++
You can’t use these as identifiers.
alignas | decltype | new | struct | alignof | default | noexcept | switch |
and | delete | not | template | and_eq | do | not_eq | this |
asm | double | nullptr | thread_local | auto | dynamic_cast | operator | throw |
bitand | else | or | true | bitor | enum | or_eq | try |
bool | explicit | private | typedef | break | export | protected | typeid |
case | extern | public | typename | catch | false | register | union |
char | float | reinterpret_cast | unsigned | char16_t | for | requires | using |
char32_t | friend | return | virtual | class | goto | short | void |
compl | if | signed | volatile | concept | inline | sizeof | wchar_t |
const | int | static | while | const_cast | long | static_assert | xor |
continue | mutable | static_cast | xor_eq |