Tag Archive for Tutorials

C++ Tutorial #0 — Setting up Cygwin


Part 1– Setting up the netbeans IDE



Part 2 (which may no longer be necessary, since 7.1 came out)

Topics covered:
Downloading / installing netbeans / Cygwin on windows.

Install instructions from netbeans (verbose)

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.

Source code: Unavailable for the first 3 lessons (if someone would like to transcribe it for me I’ll post it)


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 <iostream>

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 <iostream>
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 <iostream>

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”<<endl;
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


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: Source Code Available Here


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:

  1. Parenthesis
  2. Multiplication and Division
  3. Addition and subtraction.
  4. “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


Topics covered: Manipulating the output of cout statements using the iomanip library and some of its member functions.

Source code available here.


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 54.271123 and used setprecision(2) it would print out: 54.27 to the console.

C++ Tutorial #5 — Simple if conditional, if else structure, variable output


Topics discussed: If statements, simple if syntax, variable output, if else structure.

Source Code available here..


If statements
If statements are the backbone of simple decision making in object oriented programming languages. The if statement checks one or more statements for a boolean(true or false) value. Their basic structure is as follows


if (condition){
    //if the above is true
}

//optional

else{ //or elseif(condition){
     //if the "if" statement above was not true
}

Below are some examples of these statements:

If statement examples:


// Examples
// Example 1

int i=9;
if (i == 9){
     cout<< "This condition is true";
} //this statement should be true

in the above example the if statement compares (using the == operator) the integer value of i, to the number 9. In that case the statement if (i==9) is true, thus the code inside the brackets is executed.


// Example 2

int i=9;
if ( i != 8){
     cout << "This condition is also true"; 
}

In the example above we see that the contents of i (which is 9) is being compared to 8 with != which means “does not equal” or “is not equal to”. Since 9 does not equal 8, the contents of the if statement will execute.


//Example 3
int i=9;
if (i == 8){
     cout << "The if statement here won't be displayed"; 
}
else{
     cout << "This will be displayed";
}


In the final above example we are checking to see if the value of i (9) is equal to 8, in this case it is no. So everything from the bracket after the conditional until the subsequent closing bracket is skipped over. Once the word else is seen, it will execute anything in the brackets after the else, unless there are further if statements to be evaluated inside the if.

Boolean operators
Below is a brief listing of the boolean operators used in testing conditionals and what each of them mean, there will be specific usage examples of each later on in the series.

  • == : Is equal to
  • != : Is not equal to
  • > : Is greater than
  • >= : Is greater than or equal to
  • < : Is less than
  • <= : Is less than or equal to
  • ! : Is not
  • && : And
  • || : Or

C++ Tutorial #7 — In depth: If statements, nested if commission problem



Topics Discussed:If statements, case study with commission sales.

Source code available here.

C++ Tutorial #8 — More data types, integreation with if, and review


Topics Discussed: More data types, integrating said data types with if, and the start of a review seen in the next section.

Source Code Available Here.


Strings
Strings are an extremely important data type in C++ because they can hold literally anything. This is extremely useful for data sanitation. The best way to think of a C++ string is to have a strong understanding of how they’re implemented. If we’re going to explicitly set a string equal to a value, we can do so like this:
string stringName = "This is the value of the string";


bool
Bools are variables that hold a value of being either true (1) or false (0). These values cannot be seen as an actual number, but will evaluate to one in boolean mathematics (covered later). Bool variables default to “false” if you don’t specify what they are explicitly:


bool variablename; // initial value is set to false
bool variableName=true; //set to true

C++ Tutorial #9 — Section 1 review


Topics discussed: Review of section 1: Lessons 1-9

Source Code Available here.

C++ Tutorial #10 — Introduction to while / for loops


Topics Discussed: For / While loops, loop structure, using counter controlled loops.

Source code available here.


Loops
In this video I discuss two or the most important constructs in the entire language of java. In the next lesson I talk a bit more about for loops, which seem to be used a little more than while and do_while which we discussed here.


While loops
While loops are statements that will be repeated until either a break statement is reached, or until the condition is no longer true. Here’s a few examples of loops using while:


//syntax while(condition){ //do stuff }
//Examples below

int i=0;
while(i < 9){
     //do stuff
     i++; //increment i-- if you remove this statement, the loop will run infinitely.
}

boolean isTrue=true;
i=0; //reset 0 before the loop

while(isTrue){//while the boolean is true
     if(i > 10){
          isTrue = false;
}//end if
     i++; // increment i
}//End loop

i=0;//reset 0 before the loop
while (i < 10){
     cout << i; //output the value of i each time through the loop
     i++; // increment i each time through the loop
}

In the examples above you’ll notice that we always have a ‘way out’ of the loop, that’s because there is a common error known as an ‘infinite loop’ in which your code executes endlessly because your loop is unable to exit.

Do_while loops
Do_while loops are the same basic idea as while loops, except that they execute the contents of the loop once before checking the conditional. These are very useful in making users enter certain values for their input, as seen below:


int aValue=0;
do{ // do these instructions
cout << "Enter a value 1 or greater for the variable: ";
cin >> aValue;
}while(aValue < 1);//until this condition is no longer true.


For loops
For loops are similar in idea to a while statement, except that it contains a few other parts that need to be talked about after I give an example of the syntax and use of for loops:


for(int i=0; i<10; i++){
cout << i;
}

The above code does 4 things, one of which is apparent, the println() statement, so we won’t talk about that. But the for statement can be a little confusing, but when we break it into parts it will make a lot more sense.

Parts of a for loop
Every loop has three parts, which are as follows:

  1. The initializer: This part of the loop is where we set or declare some variable for use. Typically you’ll see something set to 0 here if you’re counting up to a variable.
  2. The conditional:The conditional is the same as with a while loop. It should be a statement that evaluates to a true, false, or boolean(t/f) statement.
  3. The iterator:The iterator is the part of the for loop in which we increase, decrease, or otherwise change some aspect of the loop, usually to make the loop get closer to it’s conclusion.


Additional considerations of for loops
The syntax and usage of for loops can be a little tricky overall:


for(int i=0; i<10; i++){
     //null
}
cout << i; //This will error

The above is an example of an error that I see quite often among my peers in college. Despite the initializer coming before the scope bracket, it is considered part of the scope of the loop. Therefore we can thing of the scope of a for loop being like this:

for{(int i=0; i<10; i++)
     //do stuff
}
rather than its existing structure.

Incrementing numbers
Any number that is of a type that can hold a whole number value can be incremented or decremented. In MOST cases you will be using “post-incrementing” which is seen as varName++; however, there are other ways of incrementing numbers:


++i; // pre-increment
i++; // post-increment (most-common)
--i; // pre-decrement
i--; // post-decrement (common while decrementing)

Each time you hit the statement i++;, you increase the value of i by the value of 1.

Nested for structure
Nested for’s actually share syntax with nested if statements in terms of scoping and complexity. Granted there aren’t any else statements to take into consideration.