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

CPP

From NetSec
Revision as of 09:35, 23 November 2011 by EnriqueBlackston (Talk | contribs)

Jump to: navigation, search

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


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.

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

 
int five = 5;
 


Here is a table of some common datatypes, and their descriptions.

Type Description
char character or small number
short int short integers
long int long integers
bool boolean variable, can take two values 1 or 0 (True, or False)
float floating point number
 
  char A = 65;
  short int sint = 65535;
  long int lint = 4294967295;
  bool mybool = true;
  float myfloat = 1234.500;
 

Operators

Assignment

The Assignment operator in C++ is =.

 
int myInteger = 20;
 

The example above declares a Variable named myInteger and Assigns (=) it a value of 20.

Arithmetic

Operator Description
+ Addition
- Subtraction
/ Division
* Multiplication
 % Modulo (Division Remainder)

Logical

Increment/Decrement

Conditional


If & Else

If:

 
bool trueFalse;
 
if (trueFalse != true) //If trueFalse is False (0)
 {
//execute some code
 }
 


If Else:

 
bool trueFalse;
 
if (trueFalse != false) // If trueFalse is True (1)
 {
    //execute code for True
 } else {
    //execute code or false.
   }
 

If Else if, Else:

 
int x = 0;
 
if (x < 0) //if x is less than zero
 {
 //execute code for that condition.
 } else if (x == 12) //if the value of x is equal to 12
   {
          //execute code for that condition
   } else //if x is not less than zero, and isn't equal to 12
     {
          //execute code for that condition
     }
 

Functions

"What is a function?" A Function is a block of code that executes the commands inside of it when it is called.


Loop Functions

In C++, there are three different types of loops. these are the 'for loop', the 'while loop' and the 'do-while loop'. here are examples of these three loops.

For loop:

 
#include <iostream>
int i;
for(i = 0; i < 10; i++) //integer "i" equals 0. when "i" is less then 10 increment "i" by one
{
   std::cout << "integer 'i' is less than 10." << std::endl;
}
 


While loop:

 
#include <iostream>
 
char myBool;
while(myBool != true) //While "myBool" is false
{
   std::cout << "myBool is False!" << std::endl;
}
 

Do-while loop:

 
#include <iostream>
 
do {
   x = x + 1;   //variable x equals itself plus one (if x equals 0 then x = 0 + 1)
} while(x < 2); //check to see if condition to stop looping is met
 



Your first program: 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






C++
is part of a series on

compiled languages

Visit the compiled languages Portal for complete coverage.