2. History
• C is a programming Language which is developed during 1970 by Dennis
Ritchie . In the case of this language the data was not secured.
• C Language is not an object oriented language it is a procedural oriented
language. Now what is Object Oriented Language it will be discuss later.
• In C programming, a program can be divided into smaller programs called
functions. And it doesn't support Function overloading.
• C++ is also called as C with classes or we can say C++ is a C language with more
features.
3. History
• C++ is a middle-level programming language developed by BJARNE
STROUSTRUP starting in 1979 at Bell Labs. originally named C with Classes but
later it was renamed C++ in 1983.
C++ runs on a variety of platforms, such as Windows, Mac OS, and the various
versions of UNIX
Creator of C++ Language
4. Use of C++
C++ is being highly used to create computer programs like device drivers Anything
from art applications, music players and even video games. And etc.
Design Operating system
Design Language Compiler
Design Database
Utilities
Application Software
5. Difference Between C and C++
C is the function or procedure programming
language.
C++ is the object oriented programming language.
Top down approach is used in C. Bottom up approach is used in C++.
C is less secure than C++
C++ is secure, because oop's concept is available.
Here Data is hidden and can't be accessed by
external functions.
Features like function overloading and operator
overloading is not present.
C++ supports function overloading and operator
overloading.
C uses scanf() and printf() function for standard input
and output.
C++ uses cin>> and cout<< for standard input and
output.
6. CHARACTER SET
Letter A-Z both upper and lower case.digits form 0to 9
Ad the special charler like.
! * + @ $%^
7. Compiler and IDE for C++
What is compiler?
Computers understand only one language and that language consists of sets of
instructions made of ones and zeros. This computer language is appropriately
called machine language.
A single instruction to a computer could look like this:
00011101010101
If we want to give instruction to the computer for example 2+2=4 so
compiler convert our instruction to something shown bellow.
0010 + 0010 = 0100
8. Compiler in C++
A compiler is system software which converts programming language code into
binary format in single steps. In other words Compiler is a system software which
can take input from other any programming language and convert it into lower level
machine dependent language.
9. Interpreter & Assembler
It is system software which is used to convert programming language code into
binary format in step by step process
An assembler is system software which is used to convert the assembly language
instruction into binary format in step by step process. An assembler is system
software which is used to convert the assembly language instruction into binary
format.
10. What is IDE ?
IDEs are some tools which are use to write code for C++ for example
DEV C++, Turbo C++, Visual Studio, Eclipse and etc.
The Dev C++ is much famous for learning C++ that is why we use the Dev-C++
IDE (Integrated Development Environment)
Dev-C++ has its own compiler which is responsible for the conversion of your code
into machine language.
11. First Program In C++
This is a preprocessor directive. It tells
the preprocessor to include the contents
of iostream header file in the program
before compilation. This file is required
for input output statements.
This will be discuss later. For now it is
required to write it
The main function is responsible to begin
the program, it mean the program start
from here, it include body of your program.
“HELLOW “
12. Comments in C++
Each and every language will provide this great feature which is used to document
source code. We can create more readable and eye catching program structure
using comments. We should use as many as comments in C++ program.
Comment is non executable Statement in the C++.
We can have two types of comment in Programming
Single Line Comment
Multiple Line Comment
14. Data Types & Variables
While doing programming, you need to use variables to store information.
Variables are nothing but reserved memory locations to store values. This means
that when you create a variable you reserve some space in memory.
You may like to store information of various data types like character, wide
character, integer, floating point, double floating point, Boolean etc. Based on
the data type of a variable, the operating system allocates memory and decides
what can be stored in the reserved memory.
15. Data Types & Variables
Data means characteristic of a data. Bellow are some of the data types in C++,
Type Keyword Size reservation
Boolean bool 1 byte
Character char 1 byte
Short integer short int 2 bytes
Integer int 4 bytes
Floating point float 4 byte s
Double floating point double 8 byte s
17. Variable and Assignment
Assignment is a process through which we store some values to a variables. Assigning value
to a variable need two process
1. Variable Declaration
2. Variable initialization
What is variable Declaration?
Variable Declaration tell the system you have allocate a space by a specific name.
To declare a variable you need two things. Data Type and variable name (identifier).
For example int var1; char var2; double x; string y;
18. Variable and Assignment
What is variable initialization?
Variable initialization means assigning values to variable, it is the way how you can
insert values to the variable. For example
int x; //variable declaration
x=90; // variable initialization
Or
int x=80; //variable declaration + variable initialization
19. Examples
We can declare and initialize a variable through different ways
One Variable
More then one Variable
You can declare in
same line multiple
variables
You can declare
and initialize
multiple variable in
same line.
20. Keywords
Keywords are some reserve words in C++ programming which are already taken
by the certain language and you can not assign the reserved word to any variable,
function, class and etc.
Exapmle:
Int, cin, cout, iostream, double, string, include and etc.
But you can assign the name of variable as
Abc, khan, sname, password, username and etc
21. Constant
Constant are like variable, but once you assign a value to a content variable it is not
changeable. Where the variables are changing their values during the execution of a
program.
Example
const int x=90;
x=80; // this is an error
cout<< x;
22. Characters
Character is a single alphabet which takes only one byte in computer memory and it is
always define as bellow.
Syntax char variable_name;
Example char x;
x=‘Z’;
cout<<x;
23. Strings
String is the collection of characters, it is also called non-primitive data type and it
assume storage size depends on the character of string.
Syntax string variable_name;
Example string student_name;
student_name=“qarar khan”;
cout<<student_name;
24. cin/cout
cin and cout are not keywords, as you are beginner now in
this class just consider them as a keyword. cout means
console out. It is use to print something on the screen of your
system. We use the extraction operator(<<) with cout.
Reverse cin is use for input when you want to take something
from the user through keyboard. We use the insertion
operator (>>) with cin .
Example
25. Operator
Operator is a special symbol that tells the compiler to perform specific mathematical or
logical Operation.
These are the overall
C++ Operators, we are
going to discuss all of
them one by one with the
practical examples.
For now in this class we
covers the Arithmetic,
Unary and Relational
Operators. The others
will be debussed later
26. Arithmetic operator program
To understand the
arithmetical operators look
at this program.
For more example see the
video of this slide..
27. Increment & Decrement
Operators
The increment operator (++) adds 1 to its operand, and the decrement operator (--)subtracts
1 from its operand.
Both the increment and decrement operators can either Prefix or Postfix. There is an
important difference in prefix and postfix forms which will be discuss later
x = x+1; is the same as x++
x = x-1; is the same as x--
++x // Prefix increment form x++ // Postfix increment
form
29. if, if...else & nested if
The if, if...else and nested if...else statement are used to make one-time decisions in C++
Programming, use to execute some codes and ignore some codes depending upon the test
condition.
30. if Statement
The if statement checks whether
the test condition is true or not. If the
test condition is true, it executes the
code/s inside the body of if
statement. But it the test condition is
false, it skips the code/s inside the
body of if statement. The if keyword is followed by test condition inside
parenthesis ( ). If the test condition is true, the
codes inside curly bracket is executed but if test
condition is false, the codes inside curly bracket { }
is skipped and control of program goes just below
the body of if as shown in figure above
32. Else if Statement
This statement is executed when if statements
become fail. So the compiler checks for the else if
statement. Same if the first else if statement become
fail again it will check for the third else if statement,
same action will be perform. If none of the if and else
if statement become true then the last bock (else
block) will be execute.
34. Nested if Statement
When ever an if statement comes inside
of an if statement, it is called Nested if
statement. It means this has two test
condition for execution of a program.
See the example
36. Loops
When you need to execute a block of code several number
of times we use some statements which are called loops
statement. In fact loop statement allows you to execute a
statement or a group of statement multiple time.
following is the general form of a loop statement in most of
the programming language. Conditio
n
Body of loop
True
False
Exit from
loop
37. Types of Loop
C++ provide the following types of Loop statement.
1. While loop
2. For loop
3. Do while loop
4. Nested loop
38. While loop
A while loop statement repeatedly
executes a target statement as long as
a given condition is true
Syntax:
While (condition ){
Statements
}
Flow
Chart
40. For Loop
A for loop is a repetition control structure that
allows you to efficiently write a loop that needs to
execute a specific number of time.
Syntax:
for(initialization ; condition ; increment){
Statements;
}
Flow
Chart
42. Do… While loop
As we studied while and for loops, they were checking
for condition first and then execute the statement. But
do…while loop execute the statement and then it check
the condition for further execution.
Syntax:
do{
Statements;
}while (condition)
Flow
Chart
46. C++ Program Structure
C++ Programming language is most popular
language after C Programming language.
C++ is first Object oriented programming
language. structure of C++ Program is
discussed as bellow.
47. Section 1 : Header File Declaration
Section
1. Header files used in the program are listed here.
2. Header File provides Prototype declaration for different
library functions.
3. We can also include user define header file.
4. Basically all preprocessor directives are written in this
section.
48. Section 2 : Global Declaration Section
Global Variables are declared here.
Global Declaration may include
1. Declaring Structure
2. Declaring Class
3. Declaring Variable
49. Section 3 : Class Declaration Section
1. Actually this section can be
considered as sub section for the
global declaration section.
2. Class declaration and all methods
of that class are defined here.
50. Section 4 : Main Function
1. Each and every C++ program always
starts with main function.
2. This is entry point for all the function.
Each and every method is called
indirectly through main.
3. We can create class objects in the main.
4. Operating system call this function
automatically.
51. Section 5 : Method Definition Section
1. Normally this is optional in C++, use to write the
definition of a function, or we can say the body of a
function can be written here.
52. Switch
A switch statement allows a variable to be tested for equality against a list of values. Each
value is called a case, and the variable being switched on is checked for each case.
53. Switch Rules
The expression used in a switch statement must have an integral or enumerated
type.
You can have any number of case statements within a switch. Each case is
followed by the value to be compared to and a colon.
The constant-expression for a case must be the same data type as the variable in
the switch, and it must be a constant.
When the variable being switched on is equal to a case, the statements following
that case will execute until a break statement is reached.
54. Switch Rules
When a break statement is reached, the switch terminates, and the flow of control
jumps to the next line following the switch statement.
Not every case needs to contain a break. If no break appears, the flow of control
will fall through to subsequent cases until a break is reached.
A switch statement can have an optional default case, which must appear at the
end of the switch. The default case can be used for performing a task when none of
the cases is true. No break is needed in the default case.
56. Arrays
An array is a list of elements of the same data type, identified by a pair of square
brackets [ ]. To use an array, you need to declare the array with 3 things:
1. Array Type
2. Array name
3. Array Size
Instead of declaring individual variables, such as num0, num1, ..., and num99, you
declare one array variable such as numbers and use num[0], num[1], and ... num[99]
to represent individual variables. A specific element in an array is accessed by an
index
57. Array Declaration
To declare an array in C++, the programmer specifies the type of the elements and the
number of elements required by an array as follows:
Type arrayName[arraylength];
int num[10];
This is called a single-
dimension array.
The arraySize must be an
integer constant greater than
zero and type can be any
valid C++ data type. For
example, to declare a 10-
element array we use the this
statement.
58. Initializing Arrays:
Array elements can be initialize one by one or using a single
statement as follows:
Direct Initialization
int num[5]={20,30,40,50,70};
Initialization by Index
num[0]=20;
num[1]=30;
num[2]=40;
num[3]=50;
num[4]=60;
num[5]=70
Printing array
Cout<<num[0]<<endl;
Cout<<num[1]<<endl;
Cout<<num[2]<<endl;
Cout<<num[3]<<endl;
Cout<<num[4]<<endl;
59. Initializing Arrays
An element is accessed by index number of an array. This is done by placing the index of the
element within square brackets after the name of the array.
For example: int num[9];
The above statement will take 9 elements. So here is an example that cover the declaration,
and initialization of the array by index number.
num
10 20 30 40 50 60 70 18 90
Num[0
]
Num[1
]
Num[2
]
Num[3
]
Num[4
]
Num[5
]
Num[6
]
Num[7
]
Num[8
]
60. Initialization with loops
int num[9];
for(int i=0;i<10;i++){
cin>>num[i]=i;
}
for(int i=0;i<10;i++){
cout<<num[i]<<endl;
}
Output
123456789
Or
1
2
3
4
5
6
7
8
9
62. Initialization of 2D arrays
Direct Initialization
int num[3][3]={{2,3,4},{8,9,7},{22,33,44}};
Or
Through index number
Num[0][0]=90
Num[]0[1]=90
Num[0][2]=90
Num[]1[0]=90
Num[1][1]=90
Num[1][2]=90
Printing 2D array
Cout<<num[0][0]<<endl;
Cout<<num[0] [1]<<endl;
Cout<<num[0] [2]<<endl;
Cout<<num[1] [0]<<endl;
Cout<<num[1] [1]<<endl;
64. Methods/Functions
Sometime a portion of code has to use many times. Instead of re-writing the codes
many times, it is better to put them into a "subroutine", and "call" them many times.
The benefits of using functions are:
Avoid repeating codes: It is easy to copy and paste.
Software Re-use: you can reuse the functions in other programs, by packaging them
into library codes.
65. Methods/Functions
A function is a group of statements that together perform a task. Every C++ program
has at least one function, which is main(), and all the most programs can define
additional functions, so each function performs a specific task.
There are two types of functions
1. User define Function
2. Built-in function or Standard library function
66. User Define Functions
Every User define function has three parts
1. Declaration of a function
2. Definition of a function
3. Calling of a function
A function declaration tells the
compiler about a function's
name, return type, and
parameters
A function definition provides
the actual body of the function.
A function calling is use to call
the function or we can say a
function calling is use to tell the
function to preform its task.
A function which is declare by the user according to hisher requirements is
called user define functions
67. Example
#....
Using namespace std;
void show(); // function declaration
main(){
Show(); // function calling
}
Void show(){ // function definition
int number=90;
Cout<<“welcome to this program..!!<< number;
}
This is a user define function with
no return type and no parameter.
68. Syntax of a user define function
return_type function_name( parameter_list){
Body of a function
Return value;
}
Return Type: A function may return a value.
The return_type is the data type of the value the function
returns. Some function may not return if you use the void
keyword
Function Name: This is the actual name of the function.
Parameters: function take argument values. And further
perform a task on values.
Function Body: The function body contains a collection of
statements that define what the function does.
69. User define function types
Bellow the User define functions
1. Functions with no arguments and no return type.
2. Functions with arguments and with no return type.
3. Functions with no arguments and a return type.
4. Functions with arguments and with return type.
70. User define function types
1. Functions with no arguments and no return type.
These are the functions, which do not take any data item from calling function, in the form of
parameters and do not return anything to calling function, after execution. Example
void display(); // declaring of function
main(){
display(); //calling of function
}
void display(){ //definition of function
int a=10, b=20, c=a+b;
Cout<<“welcome “<<c;
}
71. User define function types
2. Functions with arguments and with no return type.
This type of function can take some values from the calling function means main
function.
void add(int a, int b); // declaring of function
main(){
add(60,50); //calling of function
}
void add(int a, int b){ //definition of function
Int add= a+b;
Cout<<“a + b = “ <<add;
}
72. User define function types
3. Functions with no arguments and a return type.
This type of function can only return a value and it is not going to take any parameter, must
be noted that a function only return one value.
int sub(); // declaring of function
main(){
sub(); //calling of function
}
int sub(){ //definition of function
int salary=900, tax=100, pay=salary-tax;
Return pay;
}
73. User define function types
4. Functions with arguments and with return type.
This type of functions takes values as parameter from the calling function. After execution,
they return some value to the main function.
int total(int x, int y); // declaring of function
main(){
total(500,200); //calling of function
}
int total(int x, int y){ //definition of function
int z=x+y;
Return z;
}
74. Inline Functions
Inline Function is powerful concept in C++ programming language. If a function is
inline, the compiler places a copy of the code of that function at each point where the
function is called at compile time.
To make any function inline function just use inline keyword before the function return
type.
Why we use Inline functions?
Whenever we call any function many time then, it take a lot of extra time in execution
of series of instructions such as saving the register, pushing arguments, returning to
calling function. To solve problem we use inline functions. The main advantage of
inline function is it make the program faster.
76. Some basic program
Write a program to print HELLO WORLD on screen.
Source Code
#include<iostream> using namespace std; int main() { cout<<"Hello world"; return
0; }
77. Write a program to display output using a single cout statement.
Source Code
#include<iostream> using namespace std; int main() { cout<<"subject "
<<"tmarks"<<"nmathematict" <<90<<"ncomputert"<<77<<"nchemistryt"<<69;
return 0; }
78. Write a program which accept two numbers and print their sum.
Source Code
#include<iostream> using namespace std; int main() { int a,b,c; cout<< "nEnter
first number : "; cin>>a; cout<<"nEnter second number : "; cin>>b; c=a+b;
cout<<"nThe Sum is : "<<c; return 0; }
79. C++ Program to Check if a Number is
Positive or Negative
#include<iostream>
using namespace std;
int main ()
{
int num;
cout << "Enter the number to be checked : ";
cin >> num;
if (num >= 0)
cout << num << " is a positive number.";
else
cout << num << " is a negative number.";
return 0;
}
80. Find Number is Even or Odd using if else
#include <iostream>
using namespace std;
int main()
{
int n;
cout << "Enter an integer: ";
cin >> n;
if ( n % 2 == 0)
cout << n << " is even.";
else
cout << n << " is odd.";
return 0; }
84. Pointers
Pointers are the powerful feature of C++ programming which differs it from other
popular programming languages like: Java, Visual Basic etc.
To understand pointers, you should have the knowledge of address in computer
memory.
Computer memory is broken down into bytes and each byte has its own address.
For example: In 1KB memory, there are 1024 bytes and each byte is given an
address (0 - 1023).
85. Example
The & operator can find address
occupied by a variable. If a, b are
variables then, &a and &b gives the
address of those variables.
86. Pointer Variable
Consider a normal variable as int a=40, these variables holds data. But pointer
variables are the special types of variable that holds memory address instead of
data. To declare pointer variable use : int *p; OR int* p;
// example
int a=100;
int *ptr;
ptr=&a;
cout<<ptr;
87. Pointer to an array
Pointers are the variables that hold
address. Pointers can point at cells
of an array not only single variable
int a[5];
int* ptr;
ptr = &a[2];
To access the address of the all
elements in array we use the for loop
int arr[5];
int *ptr;
For(int i=0;i<=5;i++){
ptr= &arr[i];
cout<<ptr<<endl;
}
88. Pointer to 2D arrays
int table[5][5];
int *ptr;
for(int r=0;r<5;r++){
for(int c=0; c<5; c++){
ptr=&table[r][c]<<endl;
}
}
This example describe you
how you can access the
address in two denominational
arrays.
89. Pointers to a function
In pervious, you learned about passing
arguments to a function. Which pass an actual
value to the function. There is another way of
passing argument in which actual value is not
passed, only the reference to that value is
passed. Consider this example
Here we passed the reference of
the variable to the function
90. Passing Pointers to a function
Now in this example we pass a pointer
variable to a function instead of the
actual value and reference.
92. Structures in C++
Structure is the collection of variables of different data types under a single name
for better visualization of problem. Arrays is also collection of data but arrays can
hold data of only one type whereas structure can hold data of one or more types.
The struct keyword defines a structure type followed by an identifier(name of the
structure). Then inside the curly braces, you can declare one or more members
(declare variables inside curly braces) of that structure.
94. Pointer to Structures
structures can be accessed along with pointers. A pointer variable of
structure can be created as below
Here, the pointer variable of type struct person is created.
Structure's member can be access through pointer can be used in two
ways:
1. Referencing pointer to another address to access memory
2. Using dynamic memory allocation
95. Referencing pointer to
another address to access
memory
Referencing pointer to another address
means we are accessing the data member of
a structure through pointers, we have to
declare a structure pointer and after all we
can access the data member of a structure by
(*ptr). Operator and -> operator.
See the example to understand more.
Or
96. Recursion
Recursion is a programming technique that allows the programmer to express operation
in terms of themselves. In C++ this takes the form of a function that calls it salves. A
useful way to think of recursive function is to consider it as a process being performed
where one instruction is repeated, it look like a loop because it repeat some code.
void recurse() { recurse(); //Function calls itself } int main { recurse(); //Sets off the recursion }
void recurse(){
recurse(); //Function calls itself
}
int main(){
recurse(); //Sets off the recursion
}
98. Type casting
Typecasting is the concept of converting the value of one type into another type. For
example, you might have a float that you need to use in a function that requires an integer.
Implicit conversion
It an automatically conversion, done by compiler, It can cause lose of data.
Explicit conversion
This process is also called type casting and it is user-defined. Here the user can typecast
the result to make it of a particular data type. In C++, it can be done by two ways
Converting by assignment
Conversion using Cast operator:
99. Converting by assignment
This is done by explicitly defining the required type in front of the expression in
parenthesis. This can be also considered as forceful casting.
Syntax: (type) expression
100. Conversion using Cast operator
A Cast operator is an unary operator which forces one data type to be converted into
another data type.
C++ supports four types of casting
Static Cast
Dynamic Cast
Const Cast
Reinterpret Cast OOP
101. Description for Casting
const_cast<type> (expr): The const_cast operator is used to explicitly override const and/or
volatile in a cast. The target type must be the same as the source type except for the alteration
of its const or volatile attributes. This type of casting manipulates the const attribute of the
passed object, either to be set or removed.
dynamic_cast<type> (expr): The dynamic_cast performs a runtime cast that verifies the
validity of the cast. If the cast cannot be made, the cast fails and the expression evaluates to
null. A dynamic_cast performs casts on polymorphic types and can cast a A* pointer into a B*
pointer only if the object being pointed to actually is a B object.
reinterpret_cast<type> (expr): The reinterpret_cast operator changes a pointer to any other
type of pointer. It also allows casting from pointer to an integer type and vice versa.
static_cast<type> (expr): The static_cast operator performs a nonpolymorphic cast. For
example, it can be used to cast a base class pointer into a derived class pointer.
102. Description for Casting
All of the above-mentioned casting operators will be used while working with
classes and objects. For now, try the following example to understand a simple
cast operators available in C++.
Float x=90.40;
Int y=x;
Cout<<y;
103. Header files
Header files contain definitions of Functions and Variables, which is imported or
used into any C++ program by using the pre-processor #include statement.
Header file have an extension ".h" which contains C++ function declaration and
macro definition.
Types of Header Files in C++
System header files: It is comes with compiler.
User header files: It is written by programmer.
104. Preprocessor Directives
The preprocessors are the directives, which give instruction to the compiler to
preprocess the information before actual compilation starts.
All preprocessor directives begin with #, and only white-space characters may
appear before a preprocessor directive on a line. Preprocessor directives are not
C++ statements, so they do not end in a semicolon (;).
You already have seen a #include directive in all the examples. This macro is
used to include a header file into the source file.
There are number of preprocessor directives supported by C++ like #include,
#define, #if, #else, #line, etc. Let us see important directives:
105. The #define Preprocessor
The #define preprocessor directive creates symbolic
constants. The symbolic constant is called a macro and
the general form of the directive is:
#define macro-name replacement-text
When this line appears in a file, all subsequent
occurrences of macro in that file will be replaced by
replacement-text before the program is compiled. For
example:
107. Using namespace std;
A namespace is a declarative region that provides a
scope to the identifiers (the names of types,
functions, variables, etc) inside it. It prevent name
collisions that can occur especially when your code
base includes multiple libraries.
Lets watch the video to understand
it practically