Free · 118 Topics · No Signup
C++ Notes
Core C++ — OOP, templates, STL, memory and modern syntax — written by CodingNow 2.0's mentors. Free to read, structured to actually help you learn.
C++ notes by CodingNow 2.0 cover 118 topics — from c++ home to c++ projects — each explained with short definitions, syntax and runnable code examples. They are 100% free, need no signup, and work as quick revision for college exams, C++ interviews and CodingNow 2.0's mentor-led C++ course in Pitampura, Delhi.
No notes found. Try a different search term, or browse all C++ notes.
C++ Tutorial
72 of 72 topics publishedC++ HOME
C++ is used to create computer programs, and is one of the most used languages in game development.
C++ Intro
C++ is a cross-platform language that can be used to create high-performance applications.
C++ Get Started
At W3Schools, you can try C++ without installing anything.
C++ Syntax
Let's break up the following code to understand it better:
Statements
A computer program is a list of "instructions" to be "executed" by a computer.
C++ Output
The cout object, together with the << operator, is used to output values and print text.
Print Numbers
You can also use cout() to print numbers.
New Lines
To insert a new line in your output, you can use the \n character:
C++ Comments
Comments can be used to explain C++ code, and to make it more readable. It can also be used to prevent execution when testing alternative code. Comments can be
C++ Variables
Variables are containers for storing data values.
Multiple Variables
To declare more than one variable of the same type, use a comma-separated list:
Identifiers
All C++ variables must be identified with unique names.
Constants
When you do not want others (or yourself) to change existing variable values, use the const keyword (this will declare the variable as "constant", which means
Real-Life Examples
Often in our examples, we simplify variable names to match their data type (myInt or myNum for int types, myChar for char types, and so on). This is done to
C++ User Input
You have already learned that cout is used to output (print) values. Now we will use cin to get user input.
C++ Data Types
As explained in the Variables chapter, a variable in C++ must be a specified data type:
Numbers
Use int when you need to store a whole number without decimals, like 35 or 1000, and float or double when you need a floating point number (with decimals)
Booleans
A boolean data type is declared with the bool keyword and can only take the values true or false .
Characters
The char data type is used to store a single character. The character must be surrounded by single quotes, like 'A' or 'c':
Strings
The string type is used to store a sequence of characters (text). This is not a built-in type, but it behaves like one in its most basic usage. String values
The auto Keyword
The auto keyword automatically detects the type of a variable based on the value you assign to it.
Real-Life Example
Here's a real-life example of using different data types, to calculate and output the total cost of a number of items:
C++ Operators
Operators are used to perform operations on variables and values.
Arithmetic
Arithmetic operators are used to perform common mathematical operations.
Assignment
Assignment operators are used to assign values to variables.
Comparison
Comparison operators are used to compare two values (or variables). This is important in programming, because it helps us to find answers and make decisions.
Logical
As with comparison operators, you can also test for true ( 1 ) or false ( 0 ) values with logical operators.
Precedence
When a calculation contains more than one operator, C++ follows order of operations rules to decide which part to calculate first.
C++ Strings
Strings are used for storing text/characters.
Concatenation
The + operator can be used between strings to add them together to make a new string. This is called concatenation:
Numbers and Strings
Warning: WARNING! C++ uses the + operator for both addition and concatenation. Numbers are added. Strings are concatenated.
String Length
To get the length of a string, use the length() function:
Access Strings
You can access the characters in a string by referring to its index number inside square brackets .
Special Characters
Because strings must be written within quotes, C++ will misunderstand this string, and generate an error:
User Input Strings
It is possible to use the extraction operator on cin to store a string entered by a user:
Omitting Namespace
You might see some C++ programs that run without the standard namespace library. The using namespace std line can be omitted and replaced with the std keywo
C-Style Strings
C-style strings are created with the char type instead of string .
C++ Math
C++ has many functions that allows you to perform mathematical tasks on numbers.
C++ Booleans
Very often, in programming, you will need a data type that can only have one of two values, like:
Boolean Expressions
A Boolean expression is a piece of code that compares values or variables and returns a boolean value: 1 (true) or 0 (false).
Real-Life Example
Let's use booleans in a real-life example where we want to find out if a person is old enough to vote.
C++ If...Else
You already know that C++ supports familiar comparison conditions from mathematics, such as:
else
Use the else statement to specify a block of code to be executed if the condition is false .
else if
Use the else if statement to specify a new condition to test if the first condition is false .
Short hand if..else
There is also a short-hand if...else, known as the ternary operator because it uses three operands.
Nested if
You can also place an if statement inside another if . This is called a nested if statement.
Logical Operators
You can combine or reverse conditions using logical operators. These work together with if , else , and else if to build more complex decisions.
Real-Life Examples
This example shows how you can use if..else to "open a door" if the user enters the correct code:
C++ Switch
Use the switch statement to select one of many code blocks to be executed.
C++ While Loop
Loops can execute a block of code as long as a specified condition is reached.
Do/While Loop
The do/while loop is a variant of the while loop. This loop will execute the code block once, before checking if the condition is true. Then it will repeat
Real-Life Examples
To demonstrate a practical example of the while loop, we have created a simple "countdown" program:
C++ For Loop
When you know exactly how many times you want to loop through a block of code, use the for loop instead of a while loop:
Nested Loops
It is also possible to place a loop inside another loop. This is called a nested loop.
The foreach Loop
There is also a "for-each loop" (also known as ranged-based for loop), which is used to loop through elements in an array (or other data structures):
Real-Life Examples
To demonstrate a practical example of the for loop, let's create a program that counts to 100 by tens:
C++ Break/Continue
You have already seen the break statement used in an earlier chapter of this tutorial. It was used to "jump out" of a switch statement.
C++ Arrays
Arrays are used to store multiple values in a single variable, instead of declaring separate variables for each value.
Arrays and Loops
You can loop through the array elements with the for loop.
Omit Array Size
In C++, you don't have to specify the size of the array. The compiler is smart enough to determine the size of the array based on the number of inserted values:
Get Array Size
To get the size of an array, you can use the sizeof() operator:
Real-Life Example
To demonstrate a practical example of using arrays, let's create a program that calculates the average of different ages:
Multidimensional Arrays
A multi-dimensional array is an array of arrays.
C++ Structures
Structures (also called structs) are a way to group several related variables into one place.
C++ Enums
An enum is a special type that represents a group of constants (unchangeable values).
C++ References
A reference variable is an alias for an existing variable. It is created using the & operator:
Memory Address
In the example from the previous page, the & operator was used to create a reference variable. But it can also be used to get the memory address of a variable
C++ Pointers
You learned from the previous chapter, that we can get the memory address of a variable by using the & operator:
Dereferencing
In the example from the previous page, we used the pointer variable to get the memory address of a variable (used together with the & reference operator). How
Modify Pointers
You can also change the pointer's value. But note that this will also change the value of the original variable:
C++ Memory Mgmt
Memory management is the process of controlling how much memory your program uses - and how it is used. This includes creating, using, and releasing memory when
new and delete
The new keyword lets you manage memory yourself.
C++ Functions
13 of 13 topics publishedC++ Functions
A function is a block of code which only runs when it is called.
C++ Function Parameters
Information can be passed to functions as a parameter. Parameters act as variables inside the function.
Default Parameter
You can also use a default parameter value, by using the equals sign ( = ).
Multiple Parameters
Inside the function, you can add as many parameters as you want:
Return Values
The void keyword, used in the previous examples, indicates that the function should not return a value. If you want the function to return a value, you can us
Pass By Reference
In the examples from the previous page, we used normal variables when we passed parameters to a function.
Pass Arrays
Note: Example Explained The function ( myFunction ) takes an array as its parameter ( int myNumbers 5 ), and loops through the array elements with the f
Pass Structures
You can also pass a structure to a function.
Real-Life Example
To demonstrate a practical example of using functions, let's create a program that converts a value from fahrenheit to celsius:
C++ Function Overloading
Function overloading allows multiple functions to have the same name, as long as their parameters are different in type or number:
C++ Scope
Now that you understand how functions work, it is important to learn how variables act inside and outside of functions.
C++ Recursion
Recursion is the technique of making a function call itself.
C++ Lambda
A lambda function is a small, anonymous function you can write directly in your code. It's useful when you need a quick function without naming it or declaring
C++ Classes
17 of 17 topics publishedC++ OOP
OOP stands for Object-Oriented Programming.
C++ Classes/Objects
C++ is an object-oriented programming language.
C++ Class Methods
Methods are functions that belongs to the class.
C++ Constructors
A constructor is a special method that is automatically called when an object of a class is created.
Constructor Overloading
In C++, you can have more than one constructor in the same class. This is called constructor overloading.
C++ Access Specifiers
Access specifiers control how the members (attributes and methods) of a class can be accessed.
C++ Encapsulation
The meaning of Encapsulation, is to make sure that "sensitive" data is hidden from users.
C++ Friend Functions
Normally, private members of a class can only be accessed using public methods like getters and setters. But in some cases, you can use a special function cal
C++ Inheritance
Inheritance allows one class to reuse attributes and methods from another class. It helps you write cleaner, more efficient code by avoiding duplication.
Multilevel Inheritance
A class can also be derived from one class, which is already derived from another class.
Multiple Inheritance
A class can also be derived from more than one base class, using a comma-separated list:
Access Specifiers
You learned from the Access Specifiers chapter that there are three specifiers available in C++.
C++ Polymorphism
Polymorphism means "many forms", and it occurs when we have many classes that are related to each other by inheritance.
Virtual Functions
A virtual function is a member function in the base class that can be overridden in derived classes.
C++ Templates
Templates let you write a function or class that works with different data types.
C++ Files
The fstream library allows us to work with files.
C++ Date
The <ctime library allows us to work with dates and times.
C++ Errors
4 of 4 topics publishedC++ Errors
Even experienced C++ developers make mistakes. The key is learning how to spot and fix them!
C++ Debugging
Debugging is the process of finding and fixing errors (bugs) in your program.
C++ Exceptions
As mentioned in the C++ Errors chapter, different types of errors can occur while running a program - such as coding mistakes, invalid input, or unexpected situ
C++ Input Validation
When users enter data into a program, they might type something unexpected. Input validation makes sure the input is correct before your program continues.
C++ Data Structures
10 of 10 topics publishedC++ Data Structures & STL
Data structures are used to store and organize data. An array is an example of a data structure, which allows multiple elements to be stored in a single variabl
C++ Vectors
A vector in C++ is like a resizable array.
C++ List
A list is similar to a vector in that it can store multiple elements of the same type and dynamically grow in size.
C++ Stacks
A stack stores multiple elements in a specific order, called LIFO.
C++ Queues
A queue stores multiple elements in a specific order, called FIFO.
C++ Deque
In the previous page, your learned that elements in a queue are added at the end and removed from the front.
C++ Sets
- Are sorted automatically in ascending order.
C++ Maps
A map stores elements in "key/value" pairs.
C++ Iterators
Iterators are used to access and iterate through elements of data structures (vectors, sets, etc.), by "pointing" to them.
C++ Algorithms
In the previous chapters, you learned that data structures (like vectors, lists, etc) are used to store and organize data.
C++ Namespaces
1 of 1 topics publishedC++ Projects
1 of 1 topics publishedReady to go from notes to a real career?
Join CodingNow 2.0's C++ course — live mentorship, hands-on projects, and 100% placement support in Delhi NCR.
Enroll Now — Free Demo AvailableC++ Notes – FAQs
What students search before reading C++ notes.