C++ Notes English
C++ Notes English
What is C++
C++ language is a general purpose, object-oriented programming language created by Bjarne
Stroustrup in 1979.
C++ language is similar to C language. We can make all the programs made in C language very
easily in C ++ language without any changes or with minor changes.
We can say that C ++ language is a superset of C language, which has the features of C language
as well as some other additional features as well.
C++ is an efficient and powerful programming language which is mainly used in GUI platforms
and 3D graphics.
C++ language has much bigger function libraries than C language which makes it much more
simple and convenient than C language.
History of C++
Bjarne Stroustrup started working on the C++ language in 1978 and released its first version in
1979. Initially, according to them, this was not a new programming language, they said that it
was an extended version of the C language.
Statically typed
Compiler Based
General-Purpose Language
Case Sensitive
Object-Oriented Programming
1. Statically typed
C++ language is a statically typed programming language in which the data type of the variable
is checked at compile-time instead of at run time.
Saurabh sir c++ notes english
2. Compiler Based
C ++ language is a compiler-based programming language in which programs cannot be
executed or run without compilation.
The program made in C ++ language is first converted into machine language with the help of
compiler, then we can execute that program.
4. Case Sensitive
C++ is a case sensitive programming language in which words written in lower case letters and
upper case letters have different meanings.
5. Object-oriented Programming
C++ is an object-oriented programming language in which programs are made by treating things
as an object.
Let us look at a simple code that would print the words Hello World.
Live Demo
#include <iostream.h.h>
}
Let us look at the various parts of the above program −
The C++ language defines several headers, which contain information that is either
necessary or useful to your program. For this program, the header <iostream.h> is needed.
The next line '// main() is where program execution begins.' is a single-line comment
available in C++. Single-line comments begin with // and stop at the end of the line.
The next line cout << "Hello World"; causes the message "Hello World" to be displayed
on the screen.
Let's look at how to save the file, compile and run the program. Please follow the steps given
below −
Open a text editor and add the code as above.
Save the file as: hello.cpp
Open a command prompt and go to the directory where you saved the file.
Type 'g++ hello.cpp' and press enter to compile your code. If there are no errors in your
code the command prompt will take you to the next line and would generate a.out
executable file.
Now, type ctrl+f9 to run your program.
C++ Tokens
C++ Tokens are the tiniest individual units of any program. C++ is the superset of „C‟ and so most
constructs of „C‟ are permissible in the „C++‟ with their sense and usage unaffected. So tokens,
expressions, and the data types are equal to that of „C‟.C++ Tokens are the tiniest individual units of
any program. C++ is the superset of „C‟ and so most constructs of „C‟ are permissible in the „C++‟
with their sense and usage unaffected. So tokens, expressions, and the data types are equal to that of
„C‟.
Following, given below are the „C++‟ tokens: (most of the „c++‟ tokens are basically equal to the
„C‟ tokens, respectively)
Keywords
Identifiers
Saurabh sir c++ notes english
Constants
Variables
Operators
Keywords
These are the reserved words that have a fixed and definite meaning, and their sense cannot be
changed or modified. Moreover, the meaning and the working process of these keywords are
already known to the compiler. „C++‟ has more numbers of keywords as compared to „C‟, and those
extra ones are having special working abilities.
There are additional 30 reserved words that were not in ‘C’, these are therefore new to the
‘C++’, and these are as follows:
Identifiers
Identifiers are the names that are given to the different entries just like variables, structures, as well
as functions. Also, the names of the identifiers should be unique because these entities are
applicable to the implementation of the program.
Keywords that are reserved can‟t be used as the name of the identifier.
Constants
Constants are similar to a variable, except for one thing, that is their value which never changes
during the execution process once defined.
There are two more different ways of defining the constants in ‘C++’. These are as follows:
Declaration of a constant:
const [data_type] [constant_name]=[value];
Operator
„C++‟ operator is a symbol that we use for performing mathematical or logical manipulations.
Arithmetic Operators
Relational Operators
Logical Operators
Bitwise Operators
Assignment Operators
Variable
A variable is a name that is meaningful. In addition, this name is basically of the data storage
location in a computer system‟s memory. Similarly, when we use a variable we refer to
the memory address of the computer system
.
Saurabh sir c++ notes english
Example
COPY CODE
#include
int main() {
cin>>a;
cin>>b;
int sum;
sum=a+b;
Integer: The keyword used for integer data types is int. Integers typically require 4 bytes of
memory space and range from -2147483648 to 2147483647.
Character: Character data type is used for storing characters. The keyword used for the
character data type is char. Characters typically require 1 byte of memory space and range
from -128 to 127 or 0 to 255.
Boolean: Boolean data type is used for storing Boolean or logical values. A Boolean
variable can store either true or false. The keyword used for the Boolean data type is bool.
Floating Point: Floating Point data type is used for storing single-precision floating-point
values or decimal values. The keyword used for the floating-point data type is float. Float
variables typically require 4 bytes of memory space.
Double Floating Point: Double Floating Point data type is used for storing double-
precision floating-point values or decimal values. The keyword used for the double
floating-point data type is double. Double variables typically require 8 bytes of memory
space.
void: Void means without any value. void data type represents a valueless entity. A void
data type is used for those function which does not return a value.
Wide Character: Wide character data type is also a character data type but this data type
has a size greater than the normal 8-bit datatype. Represented by wchar_t. It is generally 2
or 4 bytes long.
2. Derived Data Types: The data types that are derived from the primitive or built-in
datatypes are referred to as Derived Data Types. These can be of four types namely:
Function
Array
Pointer
Reference
3. Abstract or User-Defined Data Types: These data types are defined by the user itself.
Like, as defining a class in C++ or a structure. C++ provides the following user-defined
datatypes:
Class
Structure
Union
Saurabh sir c++ notes english
Enumeration
Typedef defined Datatype
In , control statement allows to smooth flow of the program. By using the control flow
statements, a program can be altered, redirected, or repeated based on the application logic.
o Decision-making statements
o Looping statements
o Jump statements
Saurabh sir c++ notes english
Decision-Making Statements
The Decision-making statements allow us to determine which statement to execute based on the
test expression at runtime. Decision-making statements are also known as the Selection
statements. In program, single or multiple test expression (or condition) can be existed, which
evaluates Boolean TRUE and FALSE. These results of the expression/condition helps to decide
which block of statement (s) will execute if the given condition is TRUE or FALSE.
o If Statement
o If-else Statements
o If else if Statement
o Switch Case Statement
1. if statement
This consists of a single expression followed by one or more statements. If the expression
evaluates to true, then the statement body is executed. Otherwise, the statement body is skipped.
The statement body is enclosed in curly braces.
#include <iostream.h>
main () {
// local variable :
int x = 20;
iostream.h
}
Output
2. if..else statement
This is an if statement followed by an else part. The else part is executed if the conditional
expression is false.
main () {
int age;
cout << "How old are you? ";
cin >> age;
3. nested if statements
In this case, we use on if..else statement inside another on. It will be best understood with an
example. The previous example is modified to include a nested if statement
main () {
int age;
cout << "How old are you? ";
cin >> age;
4. switch statement
The switch statement enables you to test a particular variable against a list of values. When the
variable matches any of the items in the list, a particular statement or statements are executed.
switch(expression) {
case expression_1 :
statements;
break;
case expression_2 :
statements;
break;
...
...
case expression_n :
statements;
break;
default : //Optional
statements;
}
Looping Statements
looping statements are used to execute the block of code multiple-times for the given number of
time until it matches the given condition. These statements are also called Iteration statement.
o for loop
o for….in loop
o while loop
o do while loop
For Loop
Loop is an entry controlled loop, meaning that the condition specified by us is verified before
entering the loop block. It is a repetition control structure. The loop written by us is run a
To control the loop, we use a loop variable in For loop. This variable is first initialized to some
value, then we perform the check on this variable comparing it to the counter variable, and
Syntax:
Initialization Expression:
Here we initialize the loop variable to a particular value. For example, int i=1;
#include <iostream.h>
int main()
}
}
Saurabh sir c++ notes english
While Loop
While loop is also an entry controlled loop, where we verify the condition specified by us, before
running the loop. The difference is being that we use For loops when we know the number of
times the body of the loop needs to run, whereas we use while loops in circumstances
when beforehand we do not know the precise number of times the body of the loop needs to run.
Syntax:
initialization expression;
while (test_expression)
update_expression;
}
Jump Statements
Jump statements are used to jump from another statement, or we can say that it transfers the
execution to another statement from the current statement.
o Break Statement
o Continue Statement
Continue
It is used to execute other parts of the loop while skipping some parts declared inside the
condition, rather than terminating the loop, it continues to execute the next iteration of the same
loop.
Break
It is used to terminate the whole loop if the specified condition is met. Unlike the continue
statement once the condition is met, break breaks the loop and the remaining part of the loop is
not executed.