Basics of C++
Basic syntax and functions from the C++ programming language.
Boilerplate
cout <<
It prints output on the screen
cin >>
It takes input from the user
Data types
The data type is the type of data
Character type
Typically a single octet(one byte). It is an integer type
Integer type
The most natural size of integer for the machine
Float type
A single-precision floating-point value
Double type
A double-precision floating-point value
Void type
Represents the absence of the type
Boolean type
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
Backspace
It adds a backspace
Form feed
Newline
Newline Character
Carriage return
Tab
It gives a tab space
Backslash
It adds a backslash
Single quote
It adds a single quotation mark
Question mark
It adds a question mark
Octal No.
It represents the value of an octal number
Hexadecimal No.
It represents the value of a hexadecimal number
Null
The null character is usually used to terminate a string
Comments
A comment is a code that is not executed by the compiler, and the programmer uses it to keep track of the code.
Single line comment
Multi-line comment
Strings
It is a collection of characters surrounded by double quotes
Declaring String
append function
It is used to concatenate two strings
length function
It returns the length of the string
Accessing and changing string characters
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
sqrt function
It returns the square root of a supplied number
ceil function
It returns the value of x rounded up to its nearest integer
floor function
It returns the value of x rounded down to its nearest integer
pow function
It returns the value of x to the power of y
Decision Making Instructions
Conditional statements are used to perform operations based on some condition.
If Statement
If-else Statement
if else-if Statement
Ternary Operator
It is shorthand of an if-else statement.
Switch Case Statement
It allows a variable to be tested for equality against a list of values (cases).
Iterative Statements
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
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.
Break Statement
break keyword inside the loop is used to terminate the loop
Continue Statement
continue keyword skips the rest of the current iteration of the loop and returns to the starting point of the loop
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
Pointer is a variable that holds the memory address of another variable
Declaration
Functions & Recursion
Functions are used to divide an extensive program into smaller pieces. It can be called multiple times to provide reusability and modularity to the C program.
Function Definition
Function Call
Recursion
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.
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
object
Constructors
It is a special method that is called automatically as soon as the object is created.
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.
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.
Creating and writing to a text file
Reading the file
It allows us to read the file line by line
Opening a File
It opens a file in the C++ program
OPEN MODES
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
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
nocreate
Opens the file only if it already exists
noreplace
Opens the file only if it does not already exist
Closing a file
It closes the file
Exception Handling
An exception is an unusual condition that results in an interruption in the flow of the program.
try and catch block
A basic try-catch block in python. When the try block throws an error, the control goes to the except block