Show Menu

C++ for beginners Cheat Sheet by

C++ for beginners studients
c-     beginners     studients

Include Headers

#include
<he­ade­rfi­le>
Common Headers / Librar­ies
#include <stdio.h>
I / O functions
#include <string.h>
string functions
#include <time.h>
time functions
#include <stdlib.h>
memory, rand, ...
#include <math.h>
math functions
#include <iostream.h>
#include <fstream.h>
I / O file functions
#include "myfile.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 ( conditional ) {
   // do something
}
else if ( another_conditional ) {
   // do something else
}
else {
   // do something as default
}
while sentence
 
while ( condit­ional ) {
// do something
}
placing “bre­ak;” breaks out of the loop.
placing “con­tin­ue;” jumps to next loop.
for sentence
 
for ( init; test; command ) {
   // do something
}
"b­rea­k;" and "c­ont­inu­e;" 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;
[def­ault:
 ­ ­ // 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())     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 Consta­nts
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()
{
   // 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, ....] )
{
   // do something
   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 refere­nce : 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;

Download the C++ for beginners Cheat Sheet

2 Pages
//media.cheatography.com/storage/thumb/jofre-pla_c-for-beginners.750.jpg

PDF (recommended)

Alternative Downloads

Share This Cheat Sheet!

 

Comments

No comments yet. Add yours below!

Add a Comment

Your Comment

Please enter your name.

    Please enter your email address

      Please enter your Comment.

          Related Cheat Sheets

          Numeric Formats Cheat Sheet
          Basic Vim Cheat Sheet
          C# & Unity MonoBehaviour Cheat Sheet