C++ Cheatsheet - CodeWithHarry 20 Pge
C++ Cheatsheet - CodeWithHarry 20 Pge
C++ Cheatsheet
Haris Ali Khan · August 18, 2023 · 6 min read
Basics
Basic syntax and functions from the C++ programming language.
Boilerplate
#include <iostream>
using namespace std;
int main() {
cout << "Welcome To CodeWithHarry";
return 0;
}
cout <<
It prints output on the screen used with the insertion operator
https://www.codewithharry.com/blogpost/cpp-cheatsheet/ 1/27
22/08/2024, 00:05 C++ Cheatsheet | CodeWithHarry
cin >>
It takes input from the user used with the extraction operator
Data types
The data type is the type of data
Character type
Typically a single octet(one byte). It is an integer type
char variable_name;
Integer type
The most natural size of integer for the machine
int variable_name;
Float type
A single-precision floating-point value
https://www.codewithharry.com/blogpost/cpp-cheatsheet/ 2/27
22/08/2024, 00:05 C++ Cheatsheet | CodeWithHarry
float variable_name;
Double type
A double-precision floating-point value
double variable_name;
Void type
Represents the absence of the type
void
Boolean type
bool
Escape Sequences
It is a sequence of characters starting with a backslash, and it doesn't represent itself when used inside
string literal.
Alarm or Beep
It produces a beep sound
https://www.codewithharry.com/blogpost/cpp-cheatsheet/ 3/27
22/08/2024, 00:05 C++ Cheatsheet | CodeWithHarry
cout<<"\a";
Backspace
It adds a backspace
cout<<"\b";
Form feed
cout<<"\f";
Newline
Newline Character
cout<<"\n";
Carriage return
cout<<"\r";
Tab
https://www.codewithharry.com/blogpost/cpp-cheatsheet/ 4/27
22/08/2024, 00:05 C++ Cheatsheet | CodeWithHarry
cout<<"\t";
Backslash
It adds a backslash
cout<<"\\";
Single quote
It adds a single quotation mark
cout<<"\'";
Question mark
It adds a question mark
cout<<"\?";
Octal No.
It represents the value of an octal number
cout<<"\nnn";
https://www.codewithharry.com/blogpost/cpp-cheatsheet/ 5/27
22/08/2024, 00:05 C++ Cheatsheet | CodeWithHarry
Hexadecimal No.
It represents the value of a hexadecimal number
cout<<"\xhh";
Null
The null character is usually used to terminate a string
cout<<"\0";
Comments
A comment is a code that is not executed by the compiler, and the programmer uses it to keep track of
the code.
Multi-line comment
/* It's a
multi-line
https://www.codewithharry.com/blogpost/cpp-cheatsheet/ 6/27
22/08/2024, 00:05 C++ Cheatsheet | CodeWithHarry
comment
*/
Strings
It is a collection of characters surrounded by double quotes
Declaring String
// String variable
string variable1 = "Hello World";
append function
It is used to concatenate two strings
length function
It returns the length of the string
https://www.codewithharry.com/blogpost/cpp-cheatsheet/ 7/27
22/08/2024, 00:05 C++ Cheatsheet | CodeWithHarry
Maths
C++ provides some built-in math functions that help the programmer to perform mathematical
operations efficiently.
max function
It returns the larger value among the two
min function
It returns the smaller value among the two
https://www.codewithharry.com/blogpost/cpp-cheatsheet/ 8/27
22/08/2024, 00:05 C++ Cheatsheet | CodeWithHarry
sqrt function
It returns the square root of a supplied number
#include <cmath>
ceil function
It returns the value of x rounded up to its nearest integer
double a=ceil(1.9);
floor function
It returns the value of x rounded down to its nearest integer
double a=floor(1.02);
pow function
It returns the value of x to the power of y
If Statement
if (condition) {
// This block of code will get executed, if the condition is True
}
If-else Statement
if (condition) {
// If condition is True then this block will get executed
} else {
// If condition is False then this block will get executed
}
if else-if Statement
if (condition) {
// Statements;
}
else if (condition){
// Statements;
}
else{
https://www.codewithharry.com/blogpost/cpp-cheatsheet/ 10/27
22/08/2024, 00:05 C++ Cheatsheet | CodeWithHarry
// Statements
}
Ternary Operator
It is shorthand of an if-else statement.
switch (expression)
{
case constant-expression:
statement1;
statement2;
break;
case constant-expression:
statement;
break;
...
default:
statement;
}
Iterative Statements
https://www.codewithharry.com/blogpost/cpp-cheatsheet/ 11/27
22/08/2024, 00:05 C++ Cheatsheet | CodeWithHarry
Iterative statements facilitate programmers to execute any block of code lines repeatedly and can be
controlled as per conditions added by the programmer.
while Loop
It iterates the block of code as long as a specified condition is True
do-while loop
It is an exit controlled loop. It is very similar to the while loop with one difference, i.e., the body of the do-
while loop is executed at least once even if the condition is False
do
{
/* code */
} while (/* condition */);
for loop
It is used to iterate the statements or a part of the program several times. It is frequently used to traverse
the data structures like the array and linked list.
/* code */
}
Break Statement
break keyword inside the loop is used to terminate the loop
break;
Continue Statement
continue keyword skips the rest of the current iteration of the loop and returns to the starting point of the
loop
continue;
References
Reference is an alias for an already existing variable. Once it is initialized to a variable, it cannot be
changed to refer to another variable. So, it's a const pointer.
Creating References
Pointers
https://www.codewithharry.com/blogpost/cpp-cheatsheet/ 13/27
22/08/2024, 00:05 C++ Cheatsheet | CodeWithHarry
Declaration
datatype *var_name;
var_name = &variable2;
Function Definition
Function Call
function_name(arguments);
Recursion
https://www.codewithharry.com/blogpost/cpp-cheatsheet/ 14/27
22/08/2024, 00:05 C++ Cheatsheet | CodeWithHarry
Recursion is when a function calls a copy of itself to work on a minor problem. And the function that calls
itself is known as the Recursive function.
void recurse()
{
... .. ...
recurse();
... .. ...
}
Object-Oriented Programming
It is a programming approach that primarily focuses on using objects and classes. The objects can be
any real-world entities.
class
class Class_name {
public: // Access specifier
// fields
// functions
// blocks
};
object
Class_name ObjectName;
https://www.codewithharry.com/blogpost/cpp-cheatsheet/ 15/27
22/08/2024, 00:05 C++ Cheatsheet | CodeWithHarry
Constructors
It is a special method that is called automatically as soon as the object is created.
int main() {
className obj_name;
return 0;
}
Encapsulation
Data encapsulation is a mechanism of bundling the data, and the functions that use them and data
abstraction is a mechanism of exposing only the interfaces and hiding the implementation details from
the user.
#include<iostream>
using namespace std;
class ExampleEncap{
private:
/* Since we have marked these data members private,
* any entity outside this class cannot access these
* data members directly, they have to use getter and
https://www.codewithharry.com/blogpost/cpp-cheatsheet/ 16/27
22/08/2024, 00:05 C++ Cheatsheet | CodeWithHarry
* setter functions.
*/
int num;
char ch;
public:
/* Getter functions to get the value of data members.
File Handling
File handling refers to reading or writing data from files. C provides some functions that allow us to
manipulate data in the files.
#include <iostream>
#include <fstream>
using namespace std;
int main() {
// Create and open a text file
ofstream MyFile("filename.txt");
getline()
Opening a File
It opens a file in the C++ program
in
Opens the file to read(default for ifstream)
out
Opens the file to write(default for ofstream)
binary
Opens the file in binary mode
https://www.codewithharry.com/blogpost/cpp-cheatsheet/ 18/27
22/08/2024, 00:05 C++ Cheatsheet | CodeWithHarry
app
Opens the file and appends all the outputs at the end
ate
Opens the file and moves the control to the end of the file
trunc
Removes the data in the existing file
Copy
fs.open ("test.txt", std::fstream::trunc)
nocreate
Opens the file only if it already exists
noreplace
Opens the file only if it does not already exist
https://www.codewithharry.com/blogpost/cpp-cheatsheet/ 19/27
22/08/2024, 00:05 C++ Cheatsheet | CodeWithHarry
Closing a file
It closes the file
myfile.close()
Exception Handling
An exception is an unusual condition that results in an interruption in the flow of the program.
try {
// code to try
throw exception; // If a problem arises, then throw an exception
}
catch () {
// Block of code to handle errors
}
https://www.codewithharry.com/blogpost/cpp-cheatsheet/ 20/27