Basics
The basics of C++ are essential to understanding it and getting a foundation of the language, this includes variables, operators, and basic input and output.
|
Variables
Variables are an essential understanding for data storage and manipulation in C++. Understanding the types of variables their uses and the way in which variables are declared and defined is very important for a basic C++ understanding.
Types of VariablesThere are multiple different types of variables including booleans, characters, integers, floating points, and other miscellaneous variable types. There are many others that I wont mention yet but these are the most basic and essential for basic knowledge.
BooleansBooleans are variables that hold a true or false data type. In C++ a value of zero is false and any other integer value is true. But using true or false is the easiest way to define booleans. The declaration starts with "bool" then followed by the name of the variable. Then to define you write the variable followed by an equal sign and the given value. You can combine the declaration and definition into one line as shown in the code line 14.
Characters & StringsCharacter and string variables are variables that hold character data. A Char variable or character variable is a variable that holds a single character byte. While a string holds an array of character bytes, like a string of characters. With a string variable we can hold words sentences or even longer pieces of character data. Similarly to other variables to initialize a char variable you write "char" followed by the name of the variable an equal sign then the given character inside ''. For a string variable write std::string, standing for standard namespace string variable, followed by the name of the variable an equal sign and the string data within "".
IntegersInteger data types hold whole integers up to certain sizes of bytes. An important concept for integers is that of unsigned integers. Unsigned integers are integers that will not hold any negative value. Think of it as an absolute value data type. Because it does not hold any negative numbers it can hold two times the size of a normal integer. The reason it actually holds two times the number plus one is because of the nature of bytes but is not important to understand.
In the photo it lists all the types of integers, from the smallest 8 bit integers to 64 bit integers. The normal integer type is 16 bits and to use the other sizes of integers you need to include the header file "cstdint". FloatsFloating point variables are variables that hold numbers with decimal places to varying degrees of accuracy.
There are three distinct types of floating point variables, floats, doubles and long doubles. In that order they increase in accuracy or the number of places past the decimal. The actual accuracy is a bit hazy but a floating point variable has around 6-7, a double has 15 and a long double can have anywhere from 15-33. If you need a specific number of decimal places that are within the 10-30 range I would suggest a different method as the accuracy can be a bit iffy. But for general accuracy of float > double > long double there is a use for each. Specifically in the accuracy of mathematic operations int eh code. OthersThere are some other variables and data storers however they are not essential for basic knowledge. There are variables like voids, pointers and references. As well as other very specific use oriented data types.
I wont show them because it will be quite confusing but they are important for future use so they are covered in the intermediate section. DeclarationTo declare a variable you simply use the specific name of the type of variable you want to declare eg, int, float, double, char etc. You then follow this with the name of the variable or what you want to write each time in order to call on the data held in teh variable. this can be anything without spaces and special characters or numbers. But underscores are completely acceptable and useful for breaking apart words eg, number_of_houses. But of course short variable names makes it easier to write a lot. Its completely up to you!
DefinitionTo define a variable means to assign it with an initial value or piece of data. To assign data to a variable you simply write the variables name followed by an equal sign and then the value or data you want to assign.
You can also follow a declaration with a definition to define a variable in one line. This creates the variable and assigns data to it. To do this simply write the declaration then an equal sign and then the data. |
Operators
Operators are used in C++ to manipulate and assign data in variables. Not only to complete mathematical operations but also manipulate data to use in code.
Assignment OperatorThe assignment operator allows us to assign values to variables. The symbol itself is the equal sign. By following a created variable by the assignment operator and then a certain value we can assign that value to the variable.
Arithmetic OperatorsArithmetic operators are operators used for operations of addition, subtraction, multiplication, division, and modulus.
Theses are the supported mathematical operations in C++. Addition is used by the plus sign and can be used to add two values and then assign them to a variable. Subtraction is denoted by the minus sign and used in the same use case. Multiplication is the asterisk symbol and division is the forward slash. The only unique operator is the modulus operator. It is written as the percent symbol and it gives the remainder of the division operation. As an example this means if you divide 5 by 5 you would get a remainder of 0. This is what the modulus operator does. Increment/ Decrement OperatorsThe increment and decrement operators allow the simple incrementation of variables. It allows the manipulation of the variables values based on the value they hold.
The simplest use of increment and decrement operators is the ++ and -- operators. These operators are written before or after a variable like in the photo and they simply add or subtract a value of one from the variable. The next method or operator is the += and -= operators. These operators add or subtract the given value from the variable. rather than setting the value of the variable like in line 13 and 14 it mean that you automatically add that value or subtract that value. |
Iostream
Iostream is used in C++ and is a standard library implemented in C++. It allows for basic input and output of strings and numbers to the console. This is the most basic starting pace for user input in C++ programs.
Header FileTo include iostream into your C++ program you must first add the header file to the program for the compiling process. This just means including #include <iostream> at the top of the .cpp file.
CoutThe cout function from iostream allows us to output strings or numbers to the console. Meaning we can tell the user something or display data.
To do this we use the standard namespace, std::cout followed by << then the string or number (or variable) followed by << and std::endl. This is one line for outputting anything to the console. The std::endl means to end the line of the console and start a new one for a new output. CinThe cin function allows the opposite of the cout function and allows the user to input through the console.
To do this std::cin followed by >> then the variable followed by << and std::endl. This is one line for outputting anything to the console. The std::endl means to end the line of the console and start a new one for a new output. |