Topic : C++ Tutorial
Author : Unknown
Page : 1 Next >>
Go to page :


Beginning C++


Now, in order to start with C++, you have to have a little programming knowledge. I am not going over the EXTREME basics, but I will teach the basics. For instance, you need to have a starting point to your program.

int main()
{
  return 0;
}


Now, I dont expect you to know what that means, so I am going to explain it. When you compile a program, you are basically turning what code you wrote, called source, into machine language. When doing this, you are going to have a LOT of code, and in all this code, your compiler needs to know where to start, basically like the entrance to a building, you need one to get into it. In C++, this is a function, called main(). The int is basically a data type, saying that it is a " int ( integer ) function ". Now, when whenever we write a statement, we HAVE to have a ; at the end of each line. If not, we get an error. Now, like my example before, we have to have have an exit to get out of the building, that is where return 0; comes in. That is saying that we are done, and we are giving that function a value, for instance 0 will exit this program. When we code things, we have to have { and } there. Basically, that is saying, " This is where the code is going ". Now, if we were to compile this, we would see a little window pop up and then it would close. That is not useful at all. Lets at least output something. Now, in order to do this, we need a little help. We need to

#include <iostream.h>

iostream is Header file, meaning that it is a file, that has some code that was written by someone else, that we can use. In iostream, it gives us basic io ( Input / Output ) routines. Now, the compiler needs to know where to find these files, regardless of what operating system you are running, you have a LOT of folders. When we use the < and the >, we are telling the compiler to look in a specific folder for these files, normally named INCLUDE. Now, lets get some things on the screen shall we ? Lets look at this code segment.


int main()
{
  cout<<" Hello World! "<<endl;
  return 0;
}


Now, lets examine that code and see what it does. We know what the int main does and what the { and } mean, and what the return 0; means. Lets look at the cout. code is a reserved word that allows us to output to the screen. The << is a operator, just like + * / - and much more. Now, when we use cout<< it allows us to output what we specify. In our case, we want to output " Hello World! ". We have to keep that in quotes so C++ does not think that it is a variable. We can have as many of << on one line as we want. So, for instance, we could do this:

cout<<"Hello"<<"to"<<"the"<<"programmer that"<<"decided"<<"to"<<"write"<<"this lovely"<<" yet boring"<<"line";

Now, that is over doing it a little, but it gets the point across. But you HAVE to use cout in order to write to the screen. Now, the endl is basically like saying that we want to go to the next line. So, if we did this:

cout<<" Hello "<<endl<<" World ";

Would look like this outputed:

Hello
World

Thats it. Now, we know how to output stuff, lets get some input shall we ?
Just like output, we have cout, we have cin to get information. Now, when we have to get input, we have to store that information in a variable. In laments terms, a variable is a memory location that has a name, that stores certain information. With EVERY variable, we have to have a data type. A data type is saying what kind of variable we are having. Like, we can have int, stands for integer, double, means decimals, char, means character, and such. So if we want to store someone's age, we would make an int variable. Now, first we have to declare a variable, with its data type. Lets do an int to make it simple.
int age; . Now, we have a variable, wow! Now, we have to get the user to input there age in this example. Its almost like cout<<, except it looks like this
cin>>
Now, lets get there age!

cout<<" Input your age: ";
cin>>age;
cout<<" You are "<<age<<" years old "<<endl;


Now, when you do this, basically, you are outputing (cout) a message, saying input your age, then you are getting a value from the user. When you use cin the computer will wait, and wait for a user to input a value. Lets say that we are 18 ( ME ! ), and we input 18. Now, the variable age is holding the integer value of 18. Now, when we output " You are " and then we have age, NO quotes, just age, cout will output the value that is stored in age, in our case, thats 18. So it will output " You are 18 years old " and then go down to the next line. Now, if we were to do this:
cout<<" You are "<<"age"<<" years old "<<endl;

Then we will get this for an output.

You are age years old

Now, that is not very useful. That is why we have to use no quotes, to successfully output the variable age.
Now, there are a lot of types, and I am not going to list them, but I will go over the basics

int - Will hold a numerical value

long - Will hold a numerical value, bigger than int

double - Will hold a numerical value, with decmials

float - Will hold a numerical value, with decimals, bigger than double

char - Will hold a character value, ONE character only

**unsigned - Will FORCE positive values only

**signed - Will FORCE negative values only

**const - Will FORCE a variable to remain the same value, CANNOT be changed


Now, those are the basic data types, for more in depth information, check the help guides with your compiler. I have starred signed and unsigned which means that you can use those with the other data types. For example, you can declare a variable like this

unsigned int age;

Now, that means that age HAS to be a positive number, and that makes sense, cant be -18 years old can we ??
Now, to finish off this lesson, I will give you some basic rules on naming variables.

Variable names can not have any spaces WHATSOEVER, but can have _ ( underscore ).
Variable names can not have any special characters, like !@#$%^&*()
Variable names must start with a letter, can contain numbers, but MUST start with letter

Those are the rules to go by, and if you break one, you will get an error. With that, I end my first lesson on C++, and hope that you will tune in next week for some more fun stuff with C++.


C++ - Conditional Statements


Welcome to another great tutorial written by me :) Now that we have the basic io functions down, and we can output to the screem and we can get some information from the user, we are going to learn about conditional statements. Now, what is a Conditional statement you ask ? Easy, its basically like this. You can perform and output certain things based on current conditional. For example, lets do a basic example:

cout<<" Enter in the tempature: ";
cin>>tempature;
if(tempature > 80)
{
  cout<<" Get some shorts !"<<endl;
}
else if(tempature > 50)
{
  cout<<" Its a little chilly out !"<<endl;
}
else
{
  cout<<" Its COLD !"<<endl;
}


Now, that looks easy right ? But I will explain it in depth, just so there is no confusion.
First off, there is another data type that has to be discussed, which is called Boolean. A Boolean ( called bool for short ) is a variable that can contain only 2 values, true and false. Kinda like, right and wrong, there is only two situations, and two only. Now, the if( statement ) statement inside of an if statement, MUST be a boolean. Now, if the statement evaluates to false, then what is inside of the brakets are NOT executed. So if the user input 76, 76 is not greater than 80, so it would go to the next if statment. Now, in that if statement, we are checking for values greater than 60, and 76 is greater than 60, so it will execute what is inside of the brakets. Now, you ask, what about the last if statment ? Well, see the else ? That means that it will check the first if statement, then if and only if that is false, it will check the else if, and if that is false it will go to the next

Page : 1 Next >>