0% found this document useful (0 votes)
123 views

C++ Cheatsheet Cheat Sheet: by Via

This document provides a C++ cheat sheet covering common data types, file I/O, classes, operators, pointers, references, functions, and prototypes. Key topics include defining classes with public and private members, opening and writing to files, declaring pointers and references, defining function prototypes, and calling functions from main(). The cheat sheet is intended to provide a concise overview of essential C++ concepts and syntax.

Uploaded by

Vivek Karthik
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
123 views

C++ Cheatsheet Cheat Sheet: by Via

This document provides a C++ cheat sheet covering common data types, file I/O, classes, operators, pointers, references, functions, and prototypes. Key topics include defining classes with public and private members, opening and writing to files, declaring pointers and references, defining function prototypes, and calling functions from main(). The cheat sheet is intended to provide a concise overview of essential C++ concepts and syntax.

Uploaded by

Vivek Karthik
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

C++ Cheatsheet Cheat Sheet

by Technecure via cheatography.com/84247/cs/19902/

Common Data Types File IO Classes

bool 1 byte int main() { class some_name {


char 1 byte ​ ​ ​ // this makes a new file private:
stream ​ ​ ​ ​ int m_some​_data1;
int 4 bytes (at least 2 bytes)
​ ​ ​ ​fstream fileSt​ream; ​ ​ ​ ​ ​double m_some​_data2;
long int 4 bytes
​ ​ ​ // open text.txt to write to public:
long long int 8 bytes
​ ​ ​ ​fil​eSt​rea​m.o​pen​("te​st.t​xt​", ​ ​ ​ ​ // this is a constr​uctor
ios::out); ​ ​ ​ ​ ​som​e_n​ame(int a, double b)
Header Files & Common Includes
​ ​ ​ if (fileS​tre​am.i​s_​open()) { {
#include <fi​len​ame> ​ ​ ​ ​ ​ ​ ​ cout << "File opened​!" ​ ​ ​ ​ ​ ​ ​ ​ ​ ​m_s​ome​_data1 = a;
#include <io​str​eam> // cin & cout << endl; ​ ​ ​ ​ ​ ​ ​ ​ ​ ​m_s​ome​_data2 = b;
#include <fs​rea​m> // file streams ​ ​ ​ } ​ ​ ​ ​ }

#include <ve​cto​r> // vectors ​ ​ ​ ​ ​ ​ ​ // getters


​ ​ ​ // write a line to the file ​ ​ ​ ​ int get_so​me_​data1() {
#include <st​rin​g> // strings
​ ​ ​ ​fil​eStream << "​Hel​lo!​\n"; ​ ​ ​ ​ ​ ​ ​ ​ ​ ​return m_some​_data1;
​ ​ ​ // must close to free the ​ ​ ​ ​ }
Operators
resource ​ ​ ​ ​ ​double get_so​me_​data2() {
a+b Addition
​ ​ ​ ​fil​eSt​rea​m.c​lose(); ​ ​ ​ ​ ​ ​ ​ ​ ​ ​return m_some​_data2;
a-b Subtra​ction ​ ​ ​ ​return 0; ​ ​ ​ ​ }
a*b Multip​lic​ation } };
a/b Division Note that you must close the file before you
int main() {

a%b Modulus can open a new new one. ios:​:out means ​ ​ ​ ​ /* makes a new object

you want to write to the file and ios:​:in called name


a -= b (a-b) store in a
means you want to read from the file. You ​ ​ ​ ​ ​ ​ ​ ​which is of some_name
a += b (a+b) store in a
write to a file stream the same way you type */
a /= b (a/b) store in a
write to cout. ​ ​ ​ ​ ​som​e_name name(0, 2.1);
a *= b (a*b) store in a ​ ​ ​ ​ ​return 0;
a++ (a+1) store in a }
a-- (a-1) store in a Classes are just like user defined types like
int or doub​le. When an object is created it
calls the const​ruc​tor. The constr​uctor is a
function with the same name as the class.

By Technecure Not published yet. Sponsored by Readable.com


Last updated 15th July, 2019. Measure your website readability!
Page 1 of 2. https://readable.com

cheatography.com/technecure/
C++ Cheatsheet Cheat Sheet
by Technecure via cheatography.com/84247/cs/19902/

Comparison Operators Pointers and References Functions & Prototypes

a<b True if a is less than b int* ptr = mem_ad​dress; pointer definition void foo(); // prototype
a <= b True if a is less than or = to b int& ref = other_var; lvalue reference void bar(int i); // prototype w/
params
a>b True if a is greater than b Note: pointers hold a single memory
void foo() { // foo definition
a >= b True if a is greater than or = to b address that you can change while a
​ ​ ​ ​ ​std​::cout << "Foo functi​on​‐
reference holds a single unchan​geable
a == b True if a equals b
memory address. \n";
a && b True if a and b are true }
a || b True if a or b are true Pointers void bar(int i) { // bar

Note: If they do not meet the criteria to be definition


int main() {
true, they are false ​ ​ ​ ​ ​std​::cout << "Bar: " << i
​ ​ ​ ​ int x = 3;
<< "​\n";
​ ​ ​ ​ // & gets the memory
Pointers }
address of x
int main() { // main definition
int main() { ​ ​ ​ ​ int* pointe​r_to_x = &x;
​ ​ ​ ​ ​foo(); // calls foo
​ ​ ​ ​ int x = 3; ​ ​ ​ ​ / pointers must be derefe​‐
function
​ ​ ​ ​ // & gets the memory renced with *
​ ​ ​ ​ ​bar(2); // calls bar with 2
address of x ​ ​ ​ ​ ​ ​ ​ ​before they are
​ ​ ​ ​ ​return 0;
​ ​ ​ ​ int* pointe​r_to_x = &x; accessed. */
}
​ ​ ​ ​ / pointers must be derefe​‐ ​ ​ ​ ​ ​*po​inter = 5;
renced with * ​ ​ ​ ​ ​return 0; All programs must have a main function.
​ ​ ​ ​ ​ ​ ​ ​before they are } This is the first function that gets called. All
accessed. */ functions except main() should have a
Note that pointers only hold a memory
​ ​ ​ ​ ​*po​inter = 5; prototype.
address. They cannot store anything else.
​ ​ ​ ​ ​return 0;
In order to actually get the data at the
} address they must deref​erence it using the
Note that pointers only hold a memory * operator.
address. They cannot store anything else. In
order to actually get the data at the address
they must deref​erence it using the *
operator.

By Technecure Not published yet. Sponsored by Readable.com


Last updated 15th July, 2019. Measure your website readability!
Page 2 of 2. https://readable.com

cheatography.com/technecure/

You might also like