C++ for beginners Cheat Sheet

C++ for beginners studients

83,789 views 17,655 downloads 4 Apr 2016 History
4.9 avg · 2 ratings

Include Headers

#include
<he­ade­rfi­le>
Common Headers / Libraries
#include <st­dio.h>
I / O functions
#include <st­rin­g.h>
string functions
#include <ti­me.h­>
time functions
#include <st­dli­b.h>
memory, rand, ...
#include <ma­th.h­>
math functions
#include <io­str­eam.h>
#include <fs­tre­am.h­>
I / O file functions
#include "­myf­ile.h"
Insert file in current directory

Namespaces

using namespace std;

Comments

// One line comment text
/* multiple line block comment text */

Basic Variable Types

NUMBER
int a; float a;
CHARACTER
char car; string s;
char car = ‘c’; string s = “hola mon”;
BOOL
bool b = false/true;

Basic input / Output Operators

cin
cin >> var
cout
cout<<­"The variable has"­<<var

Basic Operators / Math Operators

+                  Add
-                  Less
*                  Mult
/                  Div
%                  Mod
++var / --var
var++ / var--

Condit­ionals

A == B
if A is equal to B, this is true; otherwise, it’s false
A != B
if A is NOT equal to B, this is true; otherwise, it’s false
A < B
if A is less than B, this is true; otherwise, it’s false
A > B
if A is greater B, this is true; otherwise, it’s false
A <= B
if A is less than or equal to B, this is true; otherwise, it’s false
A >= B
if A is greater or equal to B, this is true; otherwise, it’s false
A ! B
if A
A && B
if condition A and condition B are true, this is true; otherwise, it's false.
A || B
if condition A or condition B is true, this is true; otherwise, it's false.
Boolean expres­​sions in C++ are evaluated left t​o r​ight!

Arrays

type array_name [ # of elements ];
int price [10];
type array_name [# elements] [# elements];
int price [5] [10];
· Array index starts at 0.
· Ex: Access 3rd element : cout<<­price [2];

Control Flow

if sentence
 
if ( condit­ional ) {
 ­ ­ // do something
}
else if ( anothe­r_c­ond­itional ) {
 ­ ­ // do something else
}
else {
   // do something as default
}
while sentence
 
while ( condit­ional ) {
// do something
}
placing “break;” breaks out of the loop.
placing “conti­nue;” jumps to next loop.
for sentence
 
for ( init; test; command ) {
   // do something
}
"­bre­ak;­" and "­con­tin­ue;­" identical effects.
do while sentence
 
do {
 ­ ­ //do something
} while (bool expres­sion);
switch case sentence
 
switch ( variable )
{
case value1:
 ­ ­ // do something;
 ­ ­ ­break;
case value2:
 ­ ­ // do something else;
 ­ ­ ­break;
[default:
 ­ ­ // do something by default:
 ­ ­ ­break; ]
}

File Input / Output

#include <fstream.h>

ifstream file; //read buffer
ofstream file; //write buffer

file.open ("filename", [file mode constant]);

* //Test if the file was created *
if(fs.is_open())&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(fs)

*//Reads/Writes like cin and cout*
file >> var; //Read
file << ''Text: "<< var << endl; //Write

*//Read Entire line*
getline (file,String); 

*//Read until it arrives at the end of file*
while(file.eof())

*//Detect if the read/write fail*
if(file.fail())

*//Close File*
file.close(); 
File Mode Constants
ios::in //Opens file for reading
ios::out //Opens file for writing
ios::app //Causes output to be appended at EOF
ios::trunc //Destroys the previous contents
ios::n­ocreate //Causes open() to fail if file doesn't already exist
ios::n­ore­place //Causes open() to fail if file already exists

Procedures

* //Declaration*
** *void* ProcedureName()**
{
&nbsp;&nbsp;&nbsp;// do something
}

*//Call to procedure*
**ProcedureName();**
In the procedures we don't receive variables and don't return other variable.

Functions

*//Declaration*
** [*returnType*] functionName ( [*input1Type* input1Name, *input2Type* input2Name, ....] ) **
{
&nbsp;&nbsp;&nbsp;// do something
&nbsp;&nbsp;&nbsp;return value; // value must be of type returnType
}

*//Call to function*
**[returntype var =] functionName ([*input1Type* input1Name, *input2Type* input2Name, ....])**
We have two methods to create and call functions:
passed with values and passed for reference.
Pass by reference : we put & before variable in the declar­ation.

Structures

Structure declar­ation :
struct <st­ruc­tur­e_n­ame>
{
    <ty­pe> <na­me>, <na­me>, ... ;
    <ty­pe> <na­me>, <na­me>, ... ;
}
Var declar­ation with structure type :
<st­ruc­tur­e_n­ame> var_name;
Acces to structure :
var_na­me.n­ame;

Badges

Useful Cheat Sheet Great Cheat Sheet Nice Cheat Sheet Good Cheat Sheet Downloaded from Unusual Locations × 12

Comments

No comments yet.

Cheatography n. The art of mapping complex subjects into clear, instantly navigable reference guides.