Intro to C++
A Deitel Book
A lot of the stuff in c++ hasn't changed. There is plenty of great books and material to study c++.
I will be following this book C++ How to program by Deitel Fourth Edition and providing my own examples.
A Programmers Perspective
The word programmer gets thrown around a lot but we fail to mention the other side of coding that is part of the discipline, and that is problem solving. Our brains are constantly thinking about our next solution in a fraction of a second. I like to think we are 50% programmers and 50% problem solvers. Much of the code we write is similar to how an author writes a book. There are moments where we spit fire and others where we have to think about our next move so we don't create plot holes. We take big problems and break it down to smaller ones and then solve one at a time. This is known as rubber duck debugging. Be open to the idea of learning something new everyday and not knowing everything.
Intro to C++
What are headers?. I use headers by including the directive #include. C++ depends on headers when using several built in functions such as the math header
iostream is used to support the input/output file system.
The next line tells the compiler to use the std namespace.
Next I have main(). This is the main program that will be executed first regardless of its location within the file. The main() function expects a return of zero to show there are no errors. If a true or 1 were to return an error has occurred or there is a bug in code.
When using variables I need to determine the type. There are several types including int, string, char, double, void, and many more. This will return a type of value. In my code I have main() as the type of int which means it will return an integer value. When it reaches the return it will end the program with a value of 0.
Global variables can be accessed by many functions and are declared outside the main() function. Local variables are declared inside a function and can only be accessed by that function. If you create a local variable with the same name as a global variable, the local variable will store its own value in memory.
cin (console input) is used for console input and cout(console output) is used for console output. Simply cout will be used to print a string or a result. cin will be used for user input which is typed on the keyboard into the console. I can store this data into cin or cout by pointing at it with two arrows << or >>
Similar to the order of operations PEMDAS (parenthesis, exponents, multiplication, addition, subtraction) the order of precedence in C++ is multiplication, modulus, division, addition, subtraction, result. The difference is in the modulus which comes after multiplication and before division. Parenthesis are not required because multiplication has higher precedence than addition. The calculation is evaluated from left to right.
Actions to execute and the order of actions. One could argue that pouring milk in a bowl and then pouring cereal is the same as pouring cereal fist and then pouring the milk because it returns the same result. The order of operations is important when solving a problem. This is what makes an algorithm.
Conditional Operator ? :
A conditional operator has two results one for each condition true or false which is closely related to the if/else structure.
A nested if/else structure can perform faster than a selection of if structures because of early exit when a condition is satisfied. Test for conditions that are likely to be true at the beginning of the structure.
Common Programming Errors
Placing a semicolon after the condition in an if statement. Forgetting one or both braces in a block of code. Not providing an action that causes the while loop to become false may lead to an infinite loop.
To build an executable file using mingw. Replace home.exe with the name of your choice and home.cpp with the name of your c++ file.
g++ -o home.exe home.cpp
- ofstream: Stream class to write on files
- ifstream: Stream class to read from files
- fstream: Stream class to both read and write from/to files.
Exit with an Error