Core C++ — OOP, templates, STL, memory and modern syntax — written by Aivasu Tech Institute's mentors. Free to read, structured to actually help you learn.
C++ notes by Aivasu Tech Institute 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 Aivasu Tech Institute's mentor-led C++ course in Pitampura, Delhi.
No notes found. Try a different search term, or browse all C++ notes.
C++ is used to create computer programs, and is one of the most used languages in game development.
C++ is a cross-platform language that can be used to create high-performance applications.
At W3Schools, you can try C++ without installing anything.
Let's break up the following code to understand it better:
A computer program is a list of "instructions" to be "executed" by a computer.
The cout object, together with the << operator, is used to output values and print text.
You can also use cout() to print numbers.
To insert a new line in your output, you can use the \n character:
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
Variables are containers for storing data values.
To declare more than one variable of the same type, use a comma-separated list:
All C++ variables must be identified with unique names.
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
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
You have already learned that cout is used to output (print) values. Now we will use cin to get user input.
As explained in the Variables chapter, a variable in C++ must be a specified data type:
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)
A boolean data type is declared with the bool keyword and can only take the values true or false .
The char data type is used to store a single character. The character must be surrounded by single quotes, like 'A' or 'c':
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 automatically detects the type of a variable based on the value you assign to it.
Here's a real-life example of using different data types, to calculate and output the total cost of a number of items:
Operators are used to perform operations on variables and values.
Arithmetic operators are used to perform common mathematical operations.
Assignment operators are used to assign values to variables.
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.
As with comparison operators, you can also test for true ( 1 ) or false ( 0 ) values with logical operators.
When a calculation contains more than one operator, C++ follows order of operations rules to decide which part to calculate first.
Strings are used for storing text/characters.
The + operator can be used between strings to add them together to make a new string. This is called concatenation:
Warning: WARNING! C++ uses the + operator for both addition and concatenation. Numbers are added. Strings are concatenated.
To get the length of a string, use the length() function:
You can access the characters in a string by referring to its index number inside square brackets .
Because strings must be written within quotes, C++ will misunderstand this string, and generate an error:
It is possible to use the extraction operator on cin to store a string entered by a user:
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 are created with the char type instead of string .
C++ has many functions that allows you to perform mathematical tasks on numbers.
Very often, in programming, you will need a data type that can only have one of two values, like:
A Boolean expression is a piece of code that compares values or variables and returns a boolean value: 1 (true) or 0 (false).
Let's use booleans in a real-life example where we want to find out if a person is old enough to vote.
You already know that C++ supports familiar comparison conditions from mathematics, such as:
Use the else statement to specify a block of code to be executed if the condition is false .
Use the else if statement to specify a new condition to test if the first condition is false .
There is also a short-hand if...else, known as the ternary operator because it uses three operands.
You can also place an if statement inside another if . This is called a nested if statement.
You can combine or reverse conditions using logical operators. These work together with if , else , and else if to build more complex decisions.
This example shows how you can use if..else to "open a door" if the user enters the correct code:
Use the switch statement to select one of many code blocks to be executed.
Loops can execute a block of code as long as a specified condition is reached.
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
To demonstrate a practical example of the while loop, we have created a simple "countdown" program:
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:
It is also possible to place a loop inside another loop. This is called a nested 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):
To demonstrate a practical example of the for loop, let's create a program that counts to 100 by tens:
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.
Arrays are used to store multiple values in a single variable, instead of declaring separate variables for each value.
You can loop through the array elements with the for loop.
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:
To get the size of an array, you can use the sizeof() operator:
To demonstrate a practical example of using arrays, let's create a program that calculates the average of different ages:
A multi-dimensional array is an array of arrays.
Structures (also called structs) are a way to group several related variables into one place.
An enum is a special type that represents a group of constants (unchangeable values).
A reference variable is an alias for an existing variable. It is created using the & operator:
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
You learned from the previous chapter, that we can get the memory address of a variable by using the & operator:
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
You can also change the pointer's value. But note that this will also change the value of the original variable:
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
The new keyword lets you manage memory yourself.
A function is a block of code which only runs when it is called.
Information can be passed to functions as a parameter. Parameters act as variables inside the function.
You can also use a default parameter value, by using the equals sign ( = ).
Inside the function, you can add as many parameters as you want:
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
In the examples from the previous page, we used normal variables when we passed parameters to a function.
Note: Example Explained The function ( myFunction ) takes an array as its parameter ( int myNumbers 5 ), and loops through the array elements with the f
You can also pass a structure to a function.
To demonstrate a practical example of using functions, let's create a program that converts a value from fahrenheit to celsius:
Function overloading allows multiple functions to have the same name, as long as their parameters are different in type or number:
Now that you understand how functions work, it is important to learn how variables act inside and outside of functions.
Recursion is the technique of making a function call itself.
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
OOP stands for Object-Oriented Programming.
C++ is an object-oriented programming language.
Methods are functions that belongs to the class.
A constructor is a special method that is automatically called when an object of a class is created.
In C++, you can have more than one constructor in the same class. This is called constructor overloading.
Access specifiers control how the members (attributes and methods) of a class can be accessed.
The meaning of Encapsulation, is to make sure that "sensitive" data is hidden from users.
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
Inheritance allows one class to reuse attributes and methods from another class. It helps you write cleaner, more efficient code by avoiding duplication.
A class can also be derived from one class, which is already derived from another class.
A class can also be derived from more than one base class, using a comma-separated list:
You learned from the Access Specifiers chapter that there are three specifiers available in C++.
Polymorphism means "many forms", and it occurs when we have many classes that are related to each other by inheritance.
A virtual function is a member function in the base class that can be overridden in derived classes.
Templates let you write a function or class that works with different data types.
The fstream library allows us to work with files.
The <ctime library allows us to work with dates and times.
Even experienced C++ developers make mistakes. The key is learning how to spot and fix them!
Debugging is the process of finding and fixing errors (bugs) in your program.
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
When users enter data into a program, they might type something unexpected. Input validation makes sure the input is correct before your program continues.
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
A vector in C++ is like a resizable array.
A list is similar to a vector in that it can store multiple elements of the same type and dynamically grow in size.
A stack stores multiple elements in a specific order, called LIFO.
A queue stores multiple elements in a specific order, called FIFO.
In the previous page, your learned that elements in a queue are added at the end and removed from the front.
- Are sorted automatically in ascending order.
A map stores elements in "key/value" pairs.
Iterators are used to access and iterate through elements of data structures (vectors, sets, etc.), by "pointing" to them.
In the previous chapters, you learned that data structures (like vectors, lists, etc) are used to store and organize data.
Join Aivasu Tech Institute's C++ course — live mentorship, hands-on projects, and 100% placement support in Delhi NCR.
Enroll Now — Free Demo Available✳ Common questions
What students search before reading C++ notes.