ChEaTdEViCe
WELCOME TO CHEAT DEVICE
GTAOnline popping here



 
HomeHomeCalendarGalleryFAQSearchUsergroupsRegisterLog in

Share | 
 

 Lesson 1 - Structure

View previous topic View next topic Go down 
AuthorMessage
DarK_DemoN
Member
avatar

Points Points : 6144
Posts : 381
Reputation : 25
Join date : 2011-01-25
Location : Hacking Kingdom

PostSubject: Lesson 1 - Structure   Tue Jan 25, 2011 12:00 pm

Lesson 1: Structure

In this lesson I will teach you how a C++ program works and compiles. It is very basic in essence. When writing a C++ program just remember these 3 pointers.

1) A C++ program is divided into functions. I will teach you how to add extra functions in later lessons.
  • The code starts off with the library(s) and namespace(s). Following these things is the main function.
    Code:
    int main()


2) All C++ programs have:
Code:
int main()
as their first/main function.
  • You will sometimes see a program that has a first
    function other than main(), but for the most part it is standard to have main() as your beginning function.
  • The four basic function declarations are int, char, string, and void. I will teach you about them in Lesson 2: Variables and Data Types.


3) All functions group their code in curly braces ( {} ).
Code:
int main()
{             
    code         
    code         
    return 0;     
}             

//I call this the dead man's land.

char add(int x, int y)
{             
    code         
    code         
    return x+y;   
}

NOTE::DON'T WORRY ABOUT WHAT THAT CODE MEANS RIGHT NOW. I'M SIMPLY SHOWING YOU AN EXAMPLE OF TWO FUNCTIONS AND HOW THEY GROUP CODE.

4) All statements, at the end of a line of code, have to have a ';' after them.
Code:
cout << "Hello, World!" << endl;
NOTE::Statements are program elements that control how and in what order objects are manipulated.
  • Expression Statements - These statements evaluate an expression for its side effects or for its return value.
  • NULL Statements - These statements can be provided where a statement is required by the C++ syntax but where no action is to be taken.
  • Compound Statements - These statements are groups of statements enclosed in curly braces ({ }). They can be used wherever a single statement may be used (if Statements and while statements and such... learn this in a future lesson).
  • Selection Statements - These statements perform a test; they then execute one section of code if the test evaluates to true (nonzero). They may execute another section of code if the test evaluates to false (if and switch).
  • Iteration Statements - These statements provide for repeated execution of a block of code until a specified termination criterion is met (in other words, loops).
  • Jump Statements - These statements either transfer control immediately to another location in the function or return control from the function (more loops).
  • Declaration Statements - Declaration statements introduce new names into the current scope.

NOTE::I will do an entire lesson on statements. It might be going against my ways of short lessons though. So, I might split it into 2 or 3 lessons.

The Structure

Code:
#include <iostream>
The '#' symbol means to direct the command at the preprocessor. The word 'include' means to include the following library of functions and statements. Of course the "following library" would be 'iostream'. IOStream is an I/O library (Input/Output library).
[You must be registered and logged in to see this image.]

Code:
using namespace std;
This next part of the code means to use the standard (std) namespace. We use this namespace so we don't have to type in 'std::' before we enter a statement.
Code:
std::cout << "Hello, World!" << std::endl;
//instead of doing this...

cout << "Hello, World!" << endl;
//we can do this...

Code:
int main()
{
//code
}
This line is initializing your main function. In our case, it is an integer (int). This function is the function in your program that does all of the displaying. Of course, your other functions will display also, but this function is usually used to display the other functions. The parentheses are used to take parameters (i will explain that later). Those parentheses followed by { } mean that it is a function. As I said earlier, a function can be declared as an int, char, string, or void.

Code:
cout << "Hello, World!" << endl;
The first part of this line is 'cout'. This means to display the following text in quotation marks. This is the basic output function that comes with the 'iostream' library. Following 'cout' is the operator '<<'. This is an operator allows the output of a string (the stuff in between the "s) to the screen. Last is the 'endl'. This is a statement that means end the line and go to the next one.
Ex:
Code:
cout << "Hi" << endl;
    cout << "I'm Tyler" << endl;
When this is displayed to the screen, it will look like this:
Code:
Hi
I'm Tyler

If you take away the endls....
Code:
cout << "Hi";
cout << "I'm Tyler";
it would display this when run:
Code:
HiI'm Tyler
As you can see, the words are piled up on one single line. That is why we use endl. Notice at the end of the 'endl' there is a ';'.

The final part of this program is the 'return' statement. The return statement causes the main function to finish. return may be followed by a return code (in our example it is followed by the return code with a value of zero). A return code of 0 for the main function is generally interpreted as the program worked as expected without any errors during its execution. This is the standard way of ending a C++ program.
[You must be registered and logged in to see this image.]
The above picture has a program that didn't execute properly. It's return value was NOT zero. So, therefore, an error occurred while trying to execute that program.

NOTE::A program can have a return value of almost anything. If the program returns the number that is equivalent to your number you typed in, then it all executed properly. Zero is just the standard way.

Lesson 2 will be on Variables and Data Types. We will learn all about int, char, void, and other data types.

For the video tutorial on this lesson please visit: [You must be registered and logged in to see this link.]
Back to top Go down
View user profile http://1337codez.co.cc
 

Lesson 1 - Structure

View previous topic View next topic Back to top 
Page 1 of 1

Permissions in this forum:You cannot reply to topics in this forum
ChEaTdEViCe ::  :: Programming :: C++ Programinng Basics-
Free forum | © phpBB | Free forum support | Contact | Report an abuse | Free forum