Thursday, October 21, 2010

Lesson 4: Variables

A variable is something that can change. Many different types of variable exist; these are the most common: int, bool, string, and char.

We'll start with integers (int).
First, at the beginning of the program, you must declare the variable: we'll call it "firstvariable." This is how you declare it:
int firstvariable;


Then, after it is declared, you can change its value:
firstvariable=22;


Alternatively, you can declare the variable with a value already assigned (we'll call this "secondvariable"):
int secondvariable=22;


Now that the variable is declared, you can display it using cout:
cout<<firstvariable<<endl;
*Note that no quotation marks were used since it is a variable; this displays "22."

With quotation marks, it displays what is typed within them:
cout<<"firstvariable";


You can also assign a value to your variable using cin:
cin>>firstvariable;
cout<<"The value of your variable is "<<firstvariable;
cout<<firstvariable;



To make a new line, use "\n," or endl:
cout<<"\nThat value again is "<<firstvariable<<"\n";
cout<<"That value yet again is "<<firstvariable<<endl;


To make a quotation mark, use \":
cout<<"Here is a word in quotes: \"a word\""<<endl;


Now, lets get two variables and add them:
cout<<"Enter any number\n>";
cin>>firstvariable;
cout<<"Okay, now enter another\n>";
cin>>secondvariable;
cout<<"The sum of the two numbers is "<<firstvariable+secondvariable<<endl;

Alternatively, you could add the variables first and then print them:
int sum=secondvariable+firstvariable;
cout<<"The number remains the same: \""<<sum<<"\"";
cout<<"The product is "<<firstvariable*secondvariable<<endl;
cout<<"The quotient is "<<firstvariable/secondvariable<<endl;
cout<<"The difference is "<<firstvariable-secondvariable<<endl;

So, "int" is an integer (e.g. -1000, 993, 12, 0):
int firstinteger=99;

And, "bool" is a true or false statement:
bool firstbool=true;
bool secondbool=false;

Then, "string" is a string of characters:
string firststring="Hey there, this is your string";
cout<<firststring<<endl;

Also, "char" can be used for text but it generally isn't. It may be avoided.

Finally, "void" is actually the absense of a variable; you'll see its use in Lesson 5 on functions, however.


The Code
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
using std::string;
int main ()
{
/*A variable is something that can change. Many different types of variable exist; these are the most common: int, bool, string, and char.
We'll start with integers (int).
First, at the beginning of the program, you must declare the variable: we'll call it firstvariable. This is how you declare it:*/
int firstvariable;
//Then, after it is declared, you can change its value:
firstvariable=22;
//Alternatively, you can declare the variable with a value already assigned (we'll call this secondvariable):
int secondvariable=22;
//Now that the variable is declared, you can display it using cout:
cout<<firstvariable<<endl; //note that no quotation marks were used since it is a variable; this displays "22."
cout<<"firstvariable";//quotes were used to demonstrate that it will display "firstvariable."
//You can also assign a value to your variable using cin:
cout<<"\nEnter the value for your variable\n>";//"\n" places a new line after the displayed text.
cin>>firstvariable;
cout<<"The value of your variable is ";
cout<<firstvariable;
cout<<"\nThat value again is "<<firstvariable<<"\n";
cout<<"That value yet again is "<<firstvariable<<endl; //endl makes a new line like "\n"
cout<<"Notice that when a quotation mark is typed, it will end what is displayed..."<<endl;
//To make a quotation, use \"
cout<<"Here is a word in quotes: \"a word\""<<endl;
cout<<"Now, lets get two variables and add them\nEnter any number\n>";
cin>>firstvariable;
cout<<"Okay, now enter another\n>";
cin>>secondvariable;
cout<<"The sum of the two numbers is "<<firstvariable+secondvariable<<endl;
cout<<"Alternatively, you could add the variables first and then print them:"<<endl;
int sum=secondvariable+firstvariable;
cout<<"The number remains the same: \""<<sum<<"\"";
cout<<"The product is "<<firstvariable*secondvariable<<endl;
cout<<"The quotient is "<<firstvariable/secondvariable<<endl;
cout<<"The difference is "<<firstvariable-secondvariable<<endl;
//So, int is an integer (e.g. -1000, 993, 12, 0)
int firstinteger=99;
//bool is a true or false statement
bool firstbool=true;
bool secondbool=false;
//string is a string of characters
string firststring="Hey there, this is your string";
cout<<firststring<<endl;
/*char can be used for text but it generally isn't. It may be avoided.
void is actually the absense of a variable; you'll see why it was included in the variables section in Lesson 5 on functions, however.
void, int, bool, string, and char.*/
cin.ignore();cin.get(); //remember, this makes your program not close without input
return 0;
}

Tuesday, October 19, 2010

Lesson 3: Comments



The Introduction: Comments
Comments are very simple yet very useful. A comment is a note you make to the programmer (most often yourself) within a script that does not affect how the program compiles. It takes on two forms, the single-line comment, and the multiple-line comment:
//This is a single-line comment
/*This, however is not.
This is a multiple-line comment*/

The Code: Comments
#include <iostream>
using namespace std;
int main()
{ //Brackets act as a nice container
cout<<"Hello World!";
cin.get();
/*Without cin.get(); our program would
close without giving us a chance to see it.*/
return 0;
}

Monday, October 18, 2010

Lesson 2: Libraries & Namespaces

The Introduction: Namespaces
In order to use cout, cin, or endl, you must first declare them.
In our Hello World example, they were declared upon use with std::cout<<..
However, you can instead declare each one at the beginning of your script with the following and not have to declare it in middle of your program:
using std::cout;
using std::endl;
using std::cin;

But as most commonly used, the whole standard namespace may be declared with the following:
using namespace std;

Therefore, the same script above could be written as either of the following in addition to the initial way.

The Code: Namespaces
#include <iostream>
using std::cout;
using std::endl;
using std::cin;
int main()
{
cout<<"Hello World!";
cin.get();
return 0;
}

OR

#include <iostream>
using namespace std;
int main()
{
cout<<"Hello World!";
cin.get();
return 0;
}

Libraries & Namespaces
Namespaces are very similar to Libraries: declaring either one enables you to use the functions that lie inside of them. They are not the same, however.

#include <iostream>
Using a library like IOStream will let you use any function within the library; however, if a function or variable within the file conflicts with a function or variable in another library or in your program, your program will not compile.

std::cout<<"..."
With a namespace, on the other hand, you can use any program from the std namespace, but at any time, you could simply choose to use "cout" from a different namespace. Although another "cout" function is unlikely, one might look like this:
dffrnt::cout<<".."
And this version of cout might do something completely different.

Sunday, October 17, 2010

Lesson 1: Introduction to C++: Hello World

The Introduction:
The classic "Hello World" program for C++ is simple. It simply opens command prompt and writes "Hello World" for you to see (and in our example, it waits for any input of the user before it allows the command prompt window to close.)

The Code:
#include <iostream>
int main()
{
std::cout<<"Hello World!";
std::cin.get();
return 0;
}

The Breakdown:
#include <iostream>
IOStream enables you to use functions that exist in the "iostream library." This includes Input and Output functions. Input functions include cin.get(); .  Output functions include cout<<. (Another input function is cin>>...; with the signs pointing a different direction, but we will get to that later.)

int main(){....return 0;}
This is the main function, meaning that when your program first launches, anything within the brackets is executed in the order in which it is listed.
-The reason for it being "int" or having "return 0;" is irrelevant as of now; that is just the way it is (note that some compilers do not require main to be "int," but it usually necessary.
-Since main is declared as an "integer," it must return an integer as well, whether is is 0, 1, or 93. 
Therefore int examplefunction() must also return an integer and string examplefunction() must return a string, but void examplefunction() need not return anything... but we will get to other functions like "string" and "void" later.
-The brackets, { and }, are used almost as a container for everything inside of the main function. It makes it visually easier to manage as well.

std::cout<<"Hello World!";
This function, cout lets you write words or numbers to command prompt. (We'll explain "std" later.)

std::cin.get();
This function, cin makes your program wait for an input (anything followed by the Enter key) before it continues to execute.

The Screenshot