Show Menu
Cheatography

C++ Exam Cheat Sheet by

Datatyps

std::s­tring
int
float
etc.

Arrays and List

int ar[size]
int arLenght = sizeof(ar) / sizeof­(int);

Class Body File Paradigm // Compare Header

#include "Zaehlwerk.h"
Zaehlwerk::Zaehlwerk(int ma){
	zahl = 0;
	max = ma;
}
void Zaehlwerk::Inkrementieren(){
	if (zahl < max)
		zahl++;
}
std::string Zaehlwerk::Anzeigen(){
	return "der Zaehler ist auf der "+std::to_string(zahl);
}

Extensions / CPP File

#include "Ringwerkzaehler.h"
Ringwerkzaehler::Ringwerkzaehler() :Zaehlwerk(){ }
Ringwerkzaehler::Ringwerkzaehler(int ma) : Zaehlwerk(ma){  }
void Ringwerkzaehler::Inkrementieren(){
	if (zahl < max)
		zahl++;
	else
		zahl = 0;
}

bubbleSort Algori­thmus

void bubblesort(int *array, int length){
     int  j;
 	for (j = 0; j < length - i - 1; ++j) {
 	    if (array[j] > array[j + 1])   {
 		int tmp = array[j];
 		array[j] = array[j + 1];
 		array[j + 1] = tmp;
 	    }
     }
 }
 

Compare Classes

bool operator < (classobj) { return (cVar < obj.cVar); } //In Constr­uctor

Class header File Paradigm

class Zaehlwerk
{
protected:
	int zahl;
	int max;
public:
	Zaehlwerk();
	Zaehlwerk(int max);
	~Zaehlwerk();
	virtual void Inkrementieren();
	virtual void Dekrementieren();
	virtual std::string Anzeigen();
	virtual void Zuruecksetzen();
private:

};

Extentions / Header File

class Ringwerkzaehler : Zaehlwerk
{
	public:
		Ringwerkzaehler();
		Ringwerkzaehler(int ma);
		~Ringwerkzaehler();
		void Inkrementieren() override;
		void Dekrementieren() override;
		std::string Anzeigen() override;
		void Zuruecksetzen() override;
};

Thread Operations

std::t­his­_th­rea­d::­sle­ep_­for­(st­d::­chr­ono­::s­eco­nds­(1));
 

String functions

printf­("%s­\n", text.c­_st­r());
std::s­tring s1('*', 20);
s1.app­end­(s2);
int equ = s1.com­pare(2, 4, s2);
s1.cle­ar();
s2.era­se(1, 3);
s1.fin­d_l­ast­_of­(s2);
s.inse­rt(2, " einString ", 0, 3);
s.leng­th();
s.repl­ace(2, 5, "­ano­Str­ing­", 2,2);
std::s­tring s3=s1.s­ub­str­(2,3);

Data processing / Read file / char

std::ifstream f;
f.open(fileName, std::ios_base::out);

int counter = 0;
char t;

while (f.get(t))
	if (t == c)
		counter++;

f.close();

Struct Example

struct kontakt{
	std::string name;
	int nummer;

	std::string toString()	{
		std::string temp = name + " ";
		temp += std::to_string(nummer);
		return temp;
	}
};
 

Data processing / Write file

std::ofstream f;
f.open(fileName, std::ios_base::out);

std::string temp = "";

temp = "joe ";
temp += std::to_string(120);

f << temp << std::endl;
f.close();

Data processing / Read file / getLine

std::vector<kontakt> kontaktListe(readFile(filename));
std::ifstream f;
f.open(fileName, std::ios_base::out);
std::string t="";
int i = 0;
while (getline(f, t))
{
	for each (char var in t)
	{
		if (var == ' ')
		{
			//Operation
		}
		else
			i++;
	}
	i = 0;
}

f.close();

Debugger / cout shorter

//Debug Messanger
#define DEBUG 1
#if DEBUG
	#define LOG(x) std::cout << x << std::endl
#else
	#define LOG(x)
#endif

system("PAUSE"); // Just here to remember
               
 

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

          Subversion Cheat Sheet
          Git Cheat Sheet
          Numeric Formats Cheat Sheet

          More Cheat Sheets by Kemmojoo

          MySQL Cheat Sheet