C++ Short Notes
C++ Short Notes
C++ Short Notes
By @Curious_.programmer
Copyrighted By @Curious_.programmer
Introduction to C++
History of C++a
C++ Standards
Basic Structure of a C++ Program
Functions
Defining Functions
Function Overloading
Recursion
Exception Handling
Try-catch Blocks
Throwing Exceptions
Standard Exception Classes
Templates
Function Templates
Class Templates
Template Specialization
File Handling
File Input and Output
PDF File Uploaded On Telegram ( Link In Bio)
File Pointers
Binary Files
Advanced Topics
Lambda Expressions
Move Semantics
Smart Pointers
Multithreading
History of C++:
C++ was developed by Bjarne Stroustrup at Bell Labs in 1983 as an
extension of the C language.
The first commercial implementation of C++ was released in 1985 by
AT&T.
The first ISO standard for C++ was published in 1998, and subsequent
updates have been released in 2003, 2011, 2014, 2017, and 2020.
C++ Standards:
The C++ standards define the syntax, semantics, and library specifications
for the language.
The latest C++ standard is C++20, which was published in 2020 and
includes several new features such as modules, concepts, coroutines, and
ranges.
C++ standards are developed and maintained by the ISO C++ committee,
which includes experts from industry, academia, and standards
organizations.
Declaring Variables:
To declare a variable in C++, you must specify its data type followed by its
For example, to declare an integer variable named "age", you would write
"int age;".Variables can also be initialized with an initial value using the
assignment operator, such as "int age = 30;".
Constants:
Constants are values that cannot be changed during program execution.
C++ supports two types of constants: literal constants and symbolic
constants.
Literal constants are values that are explicitly written into the program
code, such as "5" or "'A'".
Symbolic constants are defined using the "const" keyword and can be
given a meaningful name, such as "const double PI = 3.14159;".
Arithmetic Operators:
Arithmetic operators are used to perform mathematical calculations.
C++ supports several arithmetic operators, including addition (+),
subtraction (-), multiplication (*), division (/), and modulus (%).
For example, the expression "int result = 5 + 3 * 2;" would assign the value
11 to the variable "result".
PDF File Uploaded On Telegram ( Link In Bio)
Comparison Operators:
Comparison operators are used to compare values and produce a Boolean
result.
C++ supports several comparison operators, including equals (==), not
equals (!=), less than (<), less than or equal to (<=), greater than (>), and
greater than or equal to (>=).
For example, the expression "bool result = (5 > 3);" would assign the value
"true" to the variable "result".
Logical Operators:
Logical operators are used to combine Boolean values and produce a
Boolean result.
C++ supports several logical operators, including AND (&&), OR (||), and
NOT (!).
For example, the expression "bool result = (x > 0 && y < 0);" would assign
the value "true" to the variable "result" if both conditions are true.
Bitwise Operators:
Bitwise operators are used to perform operations on individual bits in a
binary number.
C++ supports several bitwise operators, including bitwise AND (&), bitwise
OR (|), bitwise XOR (^), bitwise NOT (~), left shift (<<), and right shift (>>).
For example, the expression "int result = 5 & 3;" would assign the value 1
to the variable "result", because the bitwise AND operation produces a
binary value of 000001.
For example:
Switch Statements:
For example:
For example:
Jump Statements:
Jump statements are used to transfer control to a different part of the
program. C++ supports several types of jump statements, including
break, continue, and goto.
Defining Functions:
Function Overloading:
Function overloading is the ability to define multiple functions with the
same name but with different parameter types and/or numbers. When
a function is called, the correct version of the function is selected based
on the parameters provided.
This code defines two functions named add. The first function takes two
integer parameters and returns an integer value, while the second
function takes two double parameters and returns a double value.
Declaring Arrays:
Example:
Example:
Example:
Example:
Defining Classes:
Access Control:
Single inheritance: A derived class inherits from a single base class. For
example:
PDF File Uploaded On Telegram ( Link In Bio)
Multiple inheritance: A derived class inherits from multiple base classes.
For example:
Try: This keyword is used to enclose a block of code that might throw an
exception.
The try block in this example encloses the division operation and the
catch block defines a block of code that will execute if an exception is
thrown. In this case, the catch block simply prints the error message to
the console.
std::runtime_error: This class is used for runtime errors, such as file I/O
errors or memory allocation failures.
Templates:
Function Templates:
Class Templates:
Class templates are used to define a generic class that can work with
different data types. It allows you to write a single class that can be
used for different data types. For example, the following class template
defines a stack:
Containers: STL provides a set of containers, which are classes that can
hold a collection of elements. Examples of containers include vector,
deque, list, set, and map. Containers can be used to store and manipulate
data efficiently.
Algorithms: STL also provides a set of algorithms, which are functions that
operate on containers. Examples of algorithms include sorting, searching,
and modifying elements in a container. Algorithms can be used to perform
complex operations on containers without having to write complex code.
Iterators: STL provides a set of iterators, which are objects that can be
used to traverse containers. Iterators provide a way to access the
elements in a container one at a time, and can be used in combination
with algorithms to perform operations on containers.
File Handling
File Handling: This topic covers how to read from and write to files in C++.
It includes concepts like file input/output, file pointers, and binary files.
File Input and Output: This involves reading data from and writing data to
files. In C++, files are opened using streams, which are objects that
represent input/output sources. File input involves reading data from a file
into a program, while file output involves writing data from a program to a
file.
Binary Files: Binary files are files that contain data in a binary format,
rather than a text format. They are often used to store complex data
structures, such as arrays and structures, in a compact and efficient way.
Advanced Topics :
Lambda Expressions:
Move Semantics:
Smart Pointers:
Multithreading:
PDF File Uploaded On Telegram ( Link In Bio)
Multithreading allows your program to run multiple threads of
execution simultaneously. This can be useful for tasks that can be split
into smaller pieces that can be executed in parallel. Here's an example
of creating a simple thread that prints out "Hello, world!":