Sie sind auf Seite 1von 2

C++ for beginners Cheat Sheet

by jofre pla via cheatography.com/26595/cs/7478/

Include Headers Condit​ionals Control Flow

#include <he​ade​rfi​le> A == B if A is equal to B, this is true; if sentence

Common Headers / Librar​ies otherwise, it’s false


if ( conditional ) {
A != B if A is NOT equal to B, this is true; // do something
#include <stdio.h> I / O functions
otherwise, it’s false }
#include <string.h> string functions
A<B if A is less than B, this is true; else if ( another_conditional ) {
#include <time.h> time functions // do something else
otherwise, it’s false
#include <stdlib.h> memory, rand, ... }
A>B if A is greater B, this is true;
else {
#include <math.h> math functions otherwise, it’s false
// do something as default
#include <iostream.h> A <= B if A is less than or equal to B, this is
}
true; otherwise, it’s false
#include <fstream.h> I / O file functions
while sentence
A >= B if A is greater or equal to B, this is
#include "myfile.h" Insert file in current
true; otherwise, it’s false while ( condit​ional ) {
directory
// do something
A!B if A
}
Namespaces A && B if condition A and condition B are placing “bre​ak;” breaks out of the loop.
true, this is true; otherwise, it's false.
using namespace std; placing “con​tin​ue;” jumps to next loop.
A || B if condition A or condition B is true,
for sentence
Comments this is true; otherwise, it's false.
for ( init; test; command ) {
Boolean expres​​sions in C++ are evaluated left t​
// One line comment text // do something
o r​ight!
/* multiple line block comment text */ }
"b​rea​k;" and "c​ont​inu​e;" identical
Arrays
Basic Variable Types effects.
type array_name [ # of elements ]; do while sentence
NUMBER
int price [10]; do {
int a; float a;
type array_name [# elements] [# elements]; ​ ​ //do something
CHARACTER } while (bool expres​sion);
int price [5] [10];
char car; string s; switch case sentence
char car = ‘c’; string s = “hola mon”; · Array index starts at 0.
switch ( variable )
· Ex: Access 3rd element : cout<<​price [2];
BOOL {
bool b = false/true; case value1:
​ ​ // do something;
​ ​ ​break;
Basic input / Output Operators
case value2:
cin cin >> var ​ ​ // do something else;
cout cout<<"The variable has"<<var ​ ​ ​break;
[def​ault:

Basic Operators / Math Operators ​ ​ // do something by default:


​ ​ ​break; ]
+ Add - Less }
* Mult / Div

% Mod

++var / --var var++ / var--

By jofre pla Published 4th April, 2016. Sponsored by ApolloPad.com


cheatography.com/jofre-pla/ Last updated 19th May, 2016. Everyone has a novel in them. Finish Yours!
Page 1 of 2. https://apollopad.com
C++ for beginners Cheat Sheet
by jofre pla via cheatography.com/26595/cs/7478/

File Input / Output Procedures Structures

#include <fstream.h> //Declaration Structure declar​ation :


ifstream file; //read buffer void Proced​ure​Nam​e() struct <st​ruc​tur​e_n​ame>
ofstream file; //write buffer { {
file.open ("fi​len​ame​", [file mode ​ ​ // do something <ty​pe> <na​me>, <na​me>, ... ;

consta​nt]); } <ty​pe> <na​me>, <na​me>, ... ;


}
//Test if the file was created //Call to procedure
if(fs.i​s_​ope​n()​) if(fs) Proc​edu​reN​ame​(); Var declar​ation with structure type :

//Rea​ds/​Writes like cin and cout <st​ruc​tur​e_n​ame> var_name;


In the procedures we don't receive variables
file >> var; //Read
and don't return other variable. Acces to structure :
file << ''Text: "​<< var << endl; var_na​me.n​ame;
//Write Functions
//Read Entire line
//Declaration
getline (file,​Str​ing);
[retu​rnT​ype] functi​onName (
//Read until it arrives at the end
[inpu​t1Type input1​Name,
of file
input​2Type input2​Name, ....] )
while(​fil​e.e​of())
{
//Detect if the read/write fail
​ ​ // do something
if(fil​e.f​ail())
​ ​ ​return value; // value must be
//Close File
of type returnType
file.c​lose();
}
File Mode Consta​nts //Call to function
ios::in //Opens file for reading [ret​urntype var =] functi​onName
ios::out //Opens file for writing ([inp​ut1​Type input1​Name,
ios::app //Causes output to be appended at
input​2Type input2​Name, ....])
EOF
ios::trunc //Destroys the previous contents We have two methods to create and call
ios::n​ocreate //Causes open() to fail if file functions:
doesn't already exist passed with values and passed for reference.
ios::n​ore​place //Causes open() to fail if file Pass by refere​nce : we put & before variable
already exists in the declar​ation.

By jofre pla Published 4th April, 2016. Sponsored by ApolloPad.com


cheatography.com/jofre-pla/ Last updated 19th May, 2016. Everyone has a novel in them. Finish Yours!
Page 2 of 2. https://apollopad.com

Das könnte Ihnen auch gefallen