Topics covered:
Downloading / installing netbeans / Cygwin on windows.
Category: Tutorials
C++ Tutorial #1 — Hello World
Topics covered: During this video I introduce #include (used to include a header file into a program) using namespace std (this will be discussed at length later on) and cout.
Hello World
“Hello World” is the quintessential “starter program” in most programming languages, we simply include enough files to write this message to the screen, and then try to explain what each part of the program does. In this case the program will look something like this (I don’t have the source code transcribed for this video right now):
#include
using namespace std;
int main(){
cout << "Hello World" << endl;
return 0;
} //end main
Now let’s break the above code down part by part and explain what each part of it does.
#include
This line tells our compiler to “include” the library “iostream”, which is also known as input/output stream. This library contains all the code required to use the functions cout / cin (covered later) in our programs. For quite some time cin / cout will be all that we’re really using iostream for, as many of the more advanced uses of it just aren’t sensible at this point.
Using namespace std;
Namespaces can be fairly confusing to some people, because until you see what the above program looks like without this statement it’s hard to understand what exactly it does, so I’ll show you that, and then try to explain it a bit better:
#include
int main(){
std::cout<<"Hello World" << endl;
return 0;
}
In the above code you’ll notice that I left out the using namespace std;
but had to change cout<<
to std::cout<<
. This is what the using namespace std;
line does. It allows us to use items from the ‘standard namespace’ without mentioning the namespace that they’re in. We’ll talk about namespaces MUCH later on in the series when we get out of using just STD, and maybe get into Boost and other libraries.
int main(){
This line is fairly simple, it shows where the start of the “main” part of our program is. I want you guys to take special notice to the {
though. That’s known as a Scope Bracket, these can make your life easier or harder based on the habits you develop with them now. You guys should use this as something of a rule when it comes to programming: “Every line I write should end with either a ;
or an { / }
. This will make your life much easier, especially in the early stages of programming.
cout <<“Hello World”<
This line calls the function “cout” from the iostream library, and passes in the text string “Hello World”. The part after that (endl) is just to make a new line after that. We’ll get more into how this works in subsequent lessons
return 0;
This line tells the compiler that it can “return” (to the command prompt) if everything went well. The return command will make more sense later. The reason we’re using return 0 and not some other number will be covered way later as well.
} //end main
This is really just a closing bracket }
, the //end main
after it is what’s known as a comment. Comments are used by programmers to keep track of what’s what in a program. In this case when you have a longer program with many brackets, you might want to know that this bracket is closing your int main() opening bracket.
C++ Tutorial #2 — Simple variables and cin
Homework: C++ Homework 1: Outputting variables
Topics covered: Simple variable declarations with primitive datatypes, cin (an iostream member)
Source code: Source Code Available Here
Declaring variables
In this lesson I teach you guys how to declare variables and work with ‘built-in types’ within c++. The proper declaration for a variable in C++ is as follows: Type variableName
. The types that were introduced in this lesson were int, and double. Int is an integer number, while double is a number that can contain a floating decimal value.
C++ Tutorial #3 — Order of operations, Simple interest, compounding interest
Topics covered: Order of operations in C++, simple interest formula / calculation, compounding interest formula calculation
Source code:
#include
using namespace std;
int main(){
//PEMDAS
//In math -- Parenthesis, Exponents, Mult / division, add / sub
//In C++ Paren. -- Evaluated first
//Mult / Div
//Add / Sub -- Last in priority
//Evaluates left to right
//1*5-4*3
//5-12
double principal=0, rate=0, time=0;
cout << "What is the initial investment? ";
cin >> principal;
cout << "What is annual percentage yield (APY -- APR)? ";
cin >> rate;
cout << "How many years will this be invested for? ";
cin >> time;
cout << "The amount of money at the end of the time period will be: ";
return (0);
}
Order of operations
In C++, there is an order of operations, much like the one that we see in traditional mathematics. In C++, the order of operations is as follows:
- Parenthesis
- Multiplication and Division
- Addition and subtraction.
- “Left to right”
Cmath library
The Cmath library contains a lot of useful things, for instance square root (sqrt) floating point modulo (fmod / fmodf) and more. In this case we are using ‘pow’ which is used like this: pow(double, int);
C++ Tutorial #4 — Simple output manipulation– money
Homework: C++ Homework 2: Using external libraries to calculate numbers.
Topics covered: Manipulating the output of cout statements using the iomanip library and some of its member functions.
Source Code:
#include
#include
using namespace std;
int main(){
double x=1.7356, y = 2.337, z=2.0, a=2.0;
cout << setiosflags(ios::showpoint) << setprecision(3);
cout << (x*y);
cout << endl <
Iomanip
In this program we include a C++ library call iomanip. iomanip is a library that is used to manipulate the output of our program. We’ll talk a little bit more about the members of this below:
Setiosflags
setiosflags
is used to change the formatting of our output in some way. The way we access it is a little different from the way that we access other functions, and we’ll cover why in a later lesson. The specific members of setiosflags that we call in this program are ios::fixed
and ios::showpoint
. ios::fixed
means that we won’t (ever) be using scientific notation on our numbers (therefore we’ll never see a number displayed as 1.06e+12 or something similar). ios::showpoint
means the we will always be showing the trailing post-decimal point numbers (if there are any). The proper way to invoke these two statements are as follows:
cout << setiosflags(ios::fixed) << setiosflags(ios::showpoint);
Setprecison
Setprecision is a statement that specifies how many decimal points to print out after the “0”. Example if we have the number and used
setprecision(2)
it would print out: 54.27
to the console.