Questions about this topic? Sign up to ask in the talk tab.

Difference between revisions of "CPP"

From NetSec
Jump to: navigation, search
Line 37: Line 37:
 
</source>
 
</source>
 
}}
 
}}
 +
----
 +
 +
 +
 +
==Variables and Data Types==
 +
In C++ values are stored in Variables.
 +
To declare a variable in C++, you declare it's datatype, and then it's name.
 +
 +
We'll declare an integer named 'five' and assign the value of 5 to it.
 +
 +
{{code|text=
 +
<source lang ="cpp">
 +
int five = 5;
 +
</source>
 +
}}
 +
 +
 +
 +
 +
 +
 
=Hello World=
 
=Hello World=
 
Here is a quick example of a "Hello World" application in C++, and how to compile it.
 
Here is a quick example of a "Hello World" application in C++, and how to compile it.

Revision as of 19:50, 22 November 2011

C++ is a compiled low-level programming language. It is an enhancement of the language C.

The name 'C++' comes from the increment operator in C, which is "++" therefore C++

Because C++ is a compiled language, you will need a compiler to compile your code. Most Linux distributions come with the proper tools such as gcc, and gdb installed. If you do not have these installed, you can install through your respective package manager.

 Debian/Ubuntu:   apt get build-essential
 Arch Linux:      pacman -S base-devel


Basic Syntax

Includes

 
#include <library>
//tells the compiler to look for 'library' in default library path.
 
#include "/path/to/library"
//tells the compiler to look for 'library' in a specific path.
 

Main function

in a nutshell the main() function is the entry point of the application. main() is where the program starts executing code.

 
int main()  
{
 
}
 


Variables and Data Types

In C++ values are stored in Variables. To declare a variable in C++, you declare it's datatype, and then it's name.

We'll declare an integer named 'five' and assign the value of 5 to it.

 
int five = 5;
 




Hello World

Here is a quick example of a "Hello World" application in C++, and how to compile it.

The code

 
 #include <iostream> //preprocessor directive, tells the compiler to 'include' the file "iostream"
//This is a comment.
/*
so is this.
except
it's
a multi-line
comment.
*/
//The compiler ignores comments.
 
//here is our "main" function.
int main(int argc, char *argv[])  //argc and argv stand for "Argument Count" and "Argument Vector"
{
 
    std::cout << "Hello World!" << std::endl; //use cout to print the text "Hello World!" to the console.
   return 0; //return 0 and exit.
}
 

Compiling the Hello World

Assuming you have GCC installed, Save the previous code as "helloworld.cpp". In the same directory that you saved your source file, open a terminal and type the following:

 gcc helloworld.cpp -o helloworld

Now GCC has compiled your source code into an executable file.

execute your hello world application with:

 ./helloworld