C++ tutorial

Go down

C++ tutorial

Post by KristisGTR on Mon Jul 25, 2011 7:54 am

Hello guys I learn a lot from Bucky Roberts - TheNewBoston.
I prefer him because He explains all stuff very clearlly and few times (every time is different,that everybody could understand ).
Also I am posting tutorial for basic C++, for understand of the code and few variables explanations (Also from TheNewBoston, but posting who don't like videos Very Happy)
C++ Basic Tutorial
Downloading the best program.
First of all you need a programming language editor like dev-c++, microsoft C++, Code::Blocks. The last editor I mentioned is the simpliest and the best, because ex. microsoft C++ is almost the same as Code::Blocks, just uses more free space and installs few more programs (usless for beginners).So you can download Code::Blocks . With this program you can write with C++ C languages

Understanding the Code
When we start the program and create new console application, there will be a file main.cpp created,so click it twice.
Now you see:
Code:
#include <iostream>

using namespace std;

int main()
{
    cout << "Hello world!" << endl;
    return 0;
}
Yes this is the simplest Hello World program.So first of all for program to run there needs to be 7 lines.And there is one problem.If you understand this tutorial the other Buckys videos will be very simple.

Code:
#include <iostream>
This is preprocessor directive,This specific file (iostream) includes the declarations of the basic standard input-output library in C++.In Normal language it means that it will include a file that is needed to run the program.
The second thing you see is a blink/free line.That is needed for a cleaner code that you can understand it, you can delete it or put them more, millions,thousands...The same is with the Tabs.

Code:
using namespace std;
The std means standart library, that is used in simple programs nothing fancy and advanced Wink.So these two first lines include things in the program.

Code:
int main()
{
    cout << "Hello world!" << endl;
    return 0;
}

This is the programs function.The functions is needed to computer to do.
The Main() is needed for computer to understand from what to start. Int is some type of data we will be working with (first just with intigers).
{
} between them you need to write a statment/instruction. like :
Code:
cout << "Hello world!" << endl;
    return 0;
And every instruction needs to end with ; how you can see every line ends with them.Statmenst make a function ex.
open car doors;
sit in the car;
start the engine;
drive to the poin;
stop the engin; and the last is
get out of the car; now you have a fully working function Smile

So the
Code:
cout << "Hello world!" << endl
means pronounse Hello world on the screen and
Code:
return 0;
.(Exit)
now, the cout means output stream object, it puts information to PC.With cout we use <<. This is called stream insertion operator,this is needed to take the words and print it out on PC screen.
We write all words in the "", to computer understand the words, and the endl is end line or go to the next line and ends with ; like I told you. (Also end line could be used in the "" with the words by writing ex "Hello world \n" the \n means new line. so it is the same as endl) also if you write text next to the endl you would get an error.
And return 0 this means when you run it that everything above it is running fine, and 0 means you are good to go, the main runs fine.
Also about comments you can add a comment like first writing
Code:
/* words words words and ending like this*/

Variables
Variables basically are just place orders.Thats all they are! For the start ex.

Code:
#include <iostream>

using namespace std;

int main()
{
  x = car
  I love x
    return 0;
}
This is not the correct code this is just an example. Like x = car so when we write I love x, so it means I love car.The same with the numbers
Code:
#include <iostream>

using namespace std;

int main()
{
  x = 8;
  12 + x;


    return 0;
}
So it will be 12 + 8.
First of all to work with the variables in c++ you need to yous a data base,so we will use a simple DB for the start.That is int (integer, with it we can write numbers like 7,6,28.).So it should look like this:
Code:
#include <iostream>

using namespace std;

int main()
{
  int car = 7; /*after int you need to name your variable, then add an equel sign and a value*/


    return 0;
}

So now c++ knows that car is value of 7. and now if you write cout<< car; and build it will show only number 7.So the whole code:
Code:
#include <iostream>

using namespace std;

int main()
{
  int car = 7; /*after int you need to name your variable*/
    cout<< car;

    return 0;
}
And now then you know a little bit of simple variables (Basics), i will show how to calculate the numbers automatically.

Code:
#include <iostream>

using namespace std;

int main()
{
  int a = 7;
  int b = 8;

  int sum = a + b;

  cout << sum;

    return 0;
}
Here you can see integer a is equel to 7, other integer is equel to 8 and sum equel a+b, so 7+8,so when there is cout << sum; the PC will automatically calculate these numbers. So these are the basics of variables

Building and running the program
The easiest part is building a program and running, when you finish writing a code you need to press a green arrow,pinion and then it will start the building and running progress.If everything is alright, a black screen will pop out.Ex. if you did a automatic calculate,how i showed to you, then it will write just a number that is calculated.
But if there are an error, nothing will pop out.First the program will show why it couldn't start (reason) in the logs & others window down the program.
Ex. This is a bad code.
Code:
#include <iostream>

using namespace std;

int main()
{
  int a;
  int b;

  int sum;
  cout << "Enter a number \n"
  cin >> a;

  cout << "Enter another number \n";
  cin >> b;

  sum = a + b;
  cout << "The sum of these numbers are/is" << sum <<endl;

  cout << sum;

    return 0;
}

why? Because i mised this ; after the words "enter a number \n". And the program will show you this:
my computer stuff\main.cpp|12|error: expected ';' before 'cin'|.
||=== Build finished: 1 errors, 0 warnings ===|

So the you need to fix it and build it again, and after that it works!
Here is how to build a program.

P.S. who wants to. Calculator code, try to understand it and make it by yourself.Also cin >> (input stream object) is operator to take the information from PC.
Code:
#include <iostream>

using namespace std;

int main()
{
  int a;
  int b;

  int sum;
  cout << "Enter a number \n";
  cin >> a;

  cout << "Enter another number \n";
  cin >> b;

  sum = a + b;
  cout << "The sum of these numbers are/is" << sum <<endl;

  cout << sum;

    return 0;
}
avatar
KristisGTR

Posts : 7
Join date : 2011-07-25
Age : 27
Location : Lithuania

Fighters
Info: 3rd Recruit
HP:
100/100  (100/100)
Lv.:
6/100  (6/100)

Back to top Go down

Re: C++ tutorial

Post by MHX AIR on Mon Jul 25, 2011 8:18 am

Fixed your html code, My site runs on real text size
[size=150] is [size=18] on here

Great tutorial Very Happy
avatar
MHX AIR
Admin
Admin

Posts : 128
Join date : 2011-07-22
Age : 24
Location : Diamondsion

Fighters
Info: The Hero
HP:
100/100  (100/100)
Lv.:
5089/9000  (5089/9000)

http://diamondsionelites.forumotion.com

Back to top Go down

Re: C++ tutorial

Post by KristisGTR on Tue Jul 26, 2011 5:30 am

Thanks man Smile
avatar
KristisGTR

Posts : 7
Join date : 2011-07-25
Age : 27
Location : Lithuania

Fighters
Info: 3rd Recruit
HP:
100/100  (100/100)
Lv.:
6/100  (6/100)

Back to top Go down

Re: C++ tutorial

Post by Sponsored content


Sponsored content


Back to top Go down

Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum