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

Difference between revisions of "CPP"

From NetSec
Jump to: navigation, search
 
(40 intermediate revisions by 5 users not shown)
Line 1: Line 1:
C++ is a compiled low-level programming language. It is an enhancement of the language [[C]].
+
'''C++''' is a [[compiled language|compiled]] low-level programming language. It is an enhancement of the language [[C]].
  
<i>The name 'C++' comes from the increment operator in C, which is "++" therefore C++</i>
+
''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.
 
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.
 
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
+
<pre style="border:none; background:transparent;">
 
+
   Debian/Ubuntu:  apt-get install build-essential
 
   Arch Linux:      pacman -S base-devel
 
   Arch Linux:      pacman -S base-devel
 
+
</pre>
 
+
<font size="-2">Special thanks to [[User:Trep|Trep]] for his contributions to this article.</font>
 
=Syntax=
 
=Syntax=
  
 
==Includes==
 
==Includes==
{{code|text=
 
<source lang="cpp">
 
#include <library>
 
//tells the compiler to look for 'library' in default library path.
 
  
#include "/path/to/library"
+
{{:CPP/Syntax/Includes}}
//tells the compiler to look for 'library' in a specific path.
+
</source>
+
}}
+
 
+
----
+
  
 
==Main function==
 
==Main function==
Line 40: Line 31:
  
  
===Variables and Data Types==
+
==Variables and Data Types==
 
In C++ values are stored in Variables.
 
In C++ values are stored in Variables.
 
To declare a variable in C++, you declare it's datatype, and then it's name.
 
To declare a variable in C++, you declare it's datatype, and then it's name.
Line 89: Line 80:
 
==Operators==
 
==Operators==
 
===Assignment===
 
===Assignment===
The Assignment operator in C++ is ''=''.
+
The Assignment operator in C++ is the equal sign (=).
 
{{code|text=
 
{{code|text=
 
<source lang="cpp">
 
<source lang="cpp">
Line 96: Line 87:
 
}}
 
}}
 
The example above declares a Variable named myInteger and Assigns (=) it a value of 20.
 
The example above declares a Variable named myInteger and Assigns (=) it a value of 20.
 +
 +
====Compound Assignment====
 +
Compound assignment is using arithmetic operators with an assignment operator to modify the value of the variable.
 +
{|class="wikitable"
 +
|-
 +
!Expression   
 +
!Equivalent
 +
|-
 +
| x += y;
 +
| x = x+y;
 +
|-
 +
| x -= 1;
 +
| x = x-1;
 +
|-
 +
| x /= 3;
 +
| x = x/3;
 +
|-
 +
| x *= 5;
 +
| x = x*5;
 +
|-
 +
|}
 +
 
===Arithmetic===
 
===Arithmetic===
 
{|class="wikitable"
 
{|class="wikitable"
Line 118: Line 131:
 
|-
 
|-
 
|}
 
|}
 +
===Relational===
 +
{|class="wikitable"
 +
|-
 +
!Operator   
 +
!Description 
 +
|-
 +
| ==
 +
| Equal To
 +
|-
 +
| !=
 +
| Not Equal
 +
|-
 +
| >
 +
| Greater Than
 +
|-
 +
| <
 +
| Less Than
 +
|-
 +
| >=
 +
| Greater Than or Equal To
 +
|-
 +
| <=
 +
| Less than or Equal To
 +
|-
 +
|}
 +
 
===Logical===
 
===Logical===
===Increment/Decrement===
+
{|class="wikitable"
===Conditional===
+
|-
 
+
!Operator   
 +
!Description
 +
|-
 +
| !
 +
| Not
 +
|-
 +
| &&
 +
| And
 +
|-
 +
| &#124;&#124;
 +
| Or
 +
|}
  
 +
===Increment/Decrement===
 +
{|class="wikitable"
 +
|-
 +
!Operator   
 +
!Description
 +
|-
 +
| ++
 +
| Increment
 +
|-
 +
| --
 +
| Decrement
 +
|-
 +
|}
 
----
 
----
  
Line 178: Line 241:
 
"What is a function?"
 
"What is a function?"
 
A Function is a block of code that executes the commands inside of it when it is called.
 
A Function is a block of code that executes the commands inside of it when it is called.
 +
 +
here is the basic layout of a function:
 +
{{code|text=
 +
<source lang="cpp">
 +
Type functionName(Type Parameter, Type Parameter)
 +
 +
 +
//do stuff here.
 +
 +
return returnValue;
 +
 +
} </source>
 +
}}
 +
 +
When ''Type''is stated above, it refers to Datatypes.
 +
 +
''functionName'' is the name of the function.
 +
 +
''Parameter'' refers to the Parameters, or Arguments of a function.
 +
 +
''returnValue'' is the data that the function returns, or gives out after it is done executing.
 +
all functions will return something, unless that function is of the datatype ''Void.''
 +
 +
 +
  
 
----
 
----
 +
 
==Loop Functions==
 
==Loop Functions==
 
In C++, there are three different types of loops. these are the 'for loop', the 'while loop' and the 'do-while loop'.
 
In C++, there are three different types of loops. these are the 'for loop', the 'while loop' and the 'do-while loop'.
Line 224: Line 313:
  
  
 +
 +
 +
 +
 +
 +
==Classes==
 +
Just as a Function holds instructions; Classes hold Methods.
 +
A Method is a Function that performs actions on data associated with its parent Class.
 +
 +
 +
Example: Declare a class called myClass and declare a method within that class.
 +
{{code|text=
 +
<source lang="cpp">
 +
 +
class myClass
 +
{
 +
    int addTwoNumbers(int x, int y);
 +
};
 +
</source>
 +
}}
 +
 +
Classes can help organize your code as they can be in separate source/header files.
 +
 +
for example, you could have the following:
 +
 +
 +
 +
myClass.h
 +
{{code|text=
 +
<source lang="cpp">
 +
#ifndef MYCLASS_H
 +
#define MYCLASS_H
 +
 +
class myClass
 +
{
 +
public: //give our method a public access modifier
 +
    myClass();
 +
    //declare our method
 +
    int addTwoNumbers(int x, int y);
 +
};
 +
 +
#endif // MYCLASS_H
 +
 +
</source>
 +
}}
 +
 +
 +
 +
myClass.cpp
 +
{{code|text=
 +
<source lang="cpp">
 +
#include "myclass.h"
 +
 +
myClass::myClass()
 +
{
 +
 +
}
 +
int myClass::addTwoNumbers(int x, int y)
 +
{
 +
    int retval;
 +
    retval = (x + y);
 +
    return retval;
 +
}
 +
 +
</source>
 +
}}
 +
 +
main.cpp
 +
{{code|text=
 +
<source lang="cpp">
 +
 +
#include <iostream>
 +
#include "myClass.h"
 +
int main(int argc, char *argv[])
 +
{
 +
 +
    int x, y;
 +
    int additionoutput;
 +
 +
    // create an Object called cClass to reference myClass.
 +
    myClass cClass;
 +
 +
 +
      std::cout << "Enter an integer:" << std::endl;
 +
      std::cin >> x;
 +
      std::cout << "And another:" << std::endl;
 +
      std::cin >> y;
 +
 +
      //here we assign additionoutput to the return value of addTwoNumbers (which is part of myClass)
 +
      additionoutput = cClass.addTwoNumbers(x,y);
 +
 +
 +
      std::cout << "The sum of your two numbers is: " << additionoutput  << std::endl;
 +
      std::cin.ignore();
 +
      std::cin.get();
 +
 +
}
 +
</source>
 +
}}
 +
 +
This would be in three different files, but the #include directives tell the compiler to 'include' those files.
  
 
=Your first program: Hello World=
 
=Your first program: Hello World=
Line 261: Line 451:
 
   ./helloworld
 
   ./helloworld
  
 +
and you will get the output
 +
  Hello World!
 +
----
  
  
  
  
 +
=Example Program: Functions=
 +
This example makes use of functions.
  
 +
==The code==
 +
{{code|text=
 +
<source lang = "cpp">
 +
#include <iostream>  //give our application Input/Output capability.
 +
 +
//declare our function.
 +
int addTwoNumbers(int x, int y)
 +
{
 +
    int retval;
 +
    retval = (x + y);
 +
    return retval;
 +
}
 +
 +
int main(int argc, char *argv[])
 +
{
 +
 +
  int x, y;
 +
  int additionoutput;
 +
 +
    std::cout << "Enter an integer:" << std::endl;
 +
    std::cin >> x;
 +
    std::cout << "And another:" << std::endl;
 +
    std::cin >> y;
 +
    additionoutput = addTwoNumbers(x,y);
 +
    std::cout << "The sum of your two numbers is: " << additionoutput  << std::endl;
 +
    std::cin.ignore();
 +
    std::cin.get();
 +
}
 +
</source>
  
{{series
 
| Name = C++
 
| PartOf = compiled languages
 
 
}}
 
}}
 +
 +
==Compiling Example Program==
 +
  gcc functionexample.cpp -o functionexample
 +
 +
Then execute it with:
 +
  ./functionexample
 +
 +
You should get an output similar to this:
 +
  <pre>
 +
Enter an integer:
 +
6
 +
And another:
 +
6
 +
The sum of your two numbers is: 12</pre>
 +
 +
=Example Program: Classes=
 +
This example program will do the same thing as the Functions example, except this example makes use of classes.
 +
==The code==
 +
{{code|text=
 +
<source lang ="cpp">
 +
#include <iostream>
 +
 +
//declare our class
 +
class myClass
 +
{
 +
 +
    //give our method a public access modifier.
 +
public:
 +
 +
    int addTwoNumbers(int x, int y);
 +
 +
};
 +
 +
int myClass::addTwoNumbers(int x, int y)
 +
{
 +
 +
    int retval;
 +
    retval = (x + y);
 +
    return retval;
 +
 +
}
 +
 +
int main(int argc, char *argv[])
 +
{
 +
 +
    int x, y;
 +
    int additionoutput;
 +
   
 +
    // create an Object called cClass to reference myClass.
 +
    myClass cClass;
 +
 +
 +
      std::cout << "Enter an integer:" << std::endl;
 +
      std::cin >> x;
 +
      std::cout << "And another:" << std::endl;
 +
      std::cin >> y;
 +
     
 +
      //here we assign additionoutput to the return value of addTwoNumbers (which is part of myClass)
 +
      additionoutput = cClass.addTwoNumbers(x,y);
 +
     
 +
     
 +
      std::cout << "The sum of your two numbers is: " << additionoutput  << std::endl;
 +
      std::cin.ignore();
 +
      std::cin.get();
 +
 +
}
 +
</source>
 +
}}
 +
==Output==
 +
 +
After compiling this, and running it you should get an output similar to this:
 +
<pre>
 +
Enter an integer:
 +
6
 +
And another:
 +
6
 +
The sum of your two numbers is: 12
 +
</pre>
 +
 +
 +
=Integrated Development Environment=
 +
An integrated Development Environment is an application suite that contains things such as a text-editor, debugger, compiler, and other things all-in-one.
 +
*[http://www.microsoft.com/visualstudio/en-us/products/2010-editions/visual-cpp-express| Microsoft Visual C++ Express] (Windows Only)
 +
*[http://qt.nokia.com| Qt Creator] (Cross platform, includes QtSDK)
 +
*[http://www.anjuta.org Anjunta Dev Studio]
 +
----
 +
 +
 +
{{programming}}{{social}}
 +
[[Category:Compiled languages]]

Latest revision as of 03:43, 20 September 2012

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 install build-essential
  Arch Linux:      pacman -S base-devel

Special thanks to Trep for his contributions to this article.

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 the equal sign (=).

 
int myInteger = 20;
 

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

Compound Assignment

Compound assignment is using arithmetic operators with an assignment operator to modify the value of the variable.

Expression Equivalent
x += y; x = x+y;
x -= 1; x = x-1;
x /= 3; x = x/3;
x *= 5; x = x*5;

Arithmetic

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

Relational

Operator Description
== Equal To
 != Not Equal
> Greater Than
< Less Than
>= Greater Than or Equal To
<= Less than or Equal To

Logical

Operator Description
 ! Not
&& And
|| Or

Increment/Decrement

Operator Description
++ Increment
-- Decrement

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.

here is the basic layout of a function:

 
Type functionName(Type Parameter, Type Parameter)
{  
 
//do stuff here.
 
return returnValue;
 
} 

When Typeis stated above, it refers to Datatypes.

functionName is the name of the function.

Parameter refers to the Parameters, or Arguments of a function.

returnValue is the data that the function returns, or gives out after it is done executing. all functions will return something, unless that function is of the datatype Void.




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
 





Classes

Just as a Function holds instructions; Classes hold Methods. A Method is a Function that performs actions on data associated with its parent Class.


Example: Declare a class called myClass and declare a method within that class.

 
 
class myClass
{
    int addTwoNumbers(int x, int y);
};
 

Classes can help organize your code as they can be in separate source/header files.

for example, you could have the following:


myClass.h

 
#ifndef MYCLASS_H
#define MYCLASS_H
 
class myClass
{
public: //give our method a public access modifier
    myClass();
    //declare our method
    int addTwoNumbers(int x, int y);
};
 
#endif // MYCLASS_H
 
 


myClass.cpp

 
#include "myclass.h"
 
myClass::myClass()
{
 
}
int myClass::addTwoNumbers(int x, int y)
{
    int retval;
    retval = (x + y);
    return retval;
}
 
 

main.cpp

 
 
#include <iostream>
#include "myClass.h"
int main(int argc, char *argv[])
{
 
    int x, y;
    int additionoutput;
 
    // create an Object called cClass to reference myClass.
    myClass cClass;
 
 
      std::cout << "Enter an integer:" << std::endl;
      std::cin >> x;
      std::cout << "And another:" << std::endl;
      std::cin >> y;
 
      //here we assign additionoutput to the return value of addTwoNumbers (which is part of myClass)
      additionoutput = cClass.addTwoNumbers(x,y);
 
 
      std::cout << "The sum of your two numbers is: " << additionoutput  << std::endl;
      std::cin.ignore();
      std::cin.get();
 
}
 

This would be in three different files, but the #include directives tell the compiler to 'include' those files.

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

and you will get the output

 Hello World!



Example Program: Functions

This example makes use of functions.

The code

 
#include <iostream>  //give our application Input/Output capability.
 
//declare our function.
int addTwoNumbers(int x, int y)
{
    int retval;
    retval = (x + y);
    return retval;
}
 
int main(int argc, char *argv[])
{
 
  int x, y;
  int additionoutput;
 
    std::cout << "Enter an integer:" << std::endl;
    std::cin >> x;
    std::cout << "And another:" << std::endl;
    std::cin >> y;
    additionoutput = addTwoNumbers(x,y);
    std::cout << "The sum of your two numbers is: " << additionoutput  << std::endl;
    std::cin.ignore();
    std::cin.get();
}
 

Compiling Example Program

 gcc functionexample.cpp -o functionexample 

Then execute it with:

 ./functionexample

You should get an output similar to this:

Enter an integer:
6
And another:
6
The sum of your two numbers is: 12

Example Program: Classes

This example program will do the same thing as the Functions example, except this example makes use of classes.

The code

 
#include <iostream>
 
//declare our class
class myClass
{
 
    //give our method a public access modifier.
public:
 
    int addTwoNumbers(int x, int y);
 
};
 
int myClass::addTwoNumbers(int x, int y)
{
 
    int retval;
    retval = (x + y);
    return retval;
 
}
 
int main(int argc, char *argv[])
{
 
    int x, y;
    int additionoutput;
 
    // create an Object called cClass to reference myClass.
    myClass cClass;
 
 
      std::cout << "Enter an integer:" << std::endl;
      std::cin >> x;
      std::cout << "And another:" << std::endl;
      std::cin >> y;
 
      //here we assign additionoutput to the return value of addTwoNumbers (which is part of myClass)
      additionoutput = cClass.addTwoNumbers(x,y);
 
 
      std::cout << "The sum of your two numbers is: " << additionoutput  << std::endl;
      std::cin.ignore();
      std::cin.get();
 
}
 

Output

After compiling this, and running it you should get an output similar to this:

Enter an integer:
6
And another:
6
The sum of your two numbers is: 12


Integrated Development Environment

An integrated Development Environment is an application suite that contains things such as a text-editor, debugger, compiler, and other things all-in-one.



CPP is part of a series on programming.
<center>
</center>