C++ 1st and 2nd Semester
C++ 1st and 2nd Semester
Programming in
C/C++
By Zarif Bahaduri
Watch videos on YouTube.
Chapt
Channel Name: Zarif Bahaduri
er s
Select Chapter
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.
Watch videos on YouTube.
Chapt
Channel Name: Zarif Bahaduri
er
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
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.
• Keep in mind….just like you wouldn't be able to write a
book in Spanish after taking a few classes, same you
won't be making any serious video games.
• Programming, like many skills, takes time. This is
something many people sadly learn every day.
Watch videos on YouTube.
Chapt
Channel Name: Zarif Bahaduri
er
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
Watch videos on YouTube.
Chapt
Channel Name: Zarif Bahaduri
er
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.
Watch videos on YouTube.
Chapt
Channel Name: Zarif Bahaduri
er
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
Watch videos on YouTube.
Chapt
Channel Name: Zarif Bahaduri
er
Comments
Example
Watch videos on YouTube.
Chapt
Channel Name: Zarif Bahaduri
er
Examples
We can declare and initialize a variable through different
ways
One Variable
You can declare
and initialize
multiple variable
You can declare
in same line.
in same line
More then one Variable multiple variables
Watch videos on YouTube.
Chapt
Channel Name: Zarif Bahaduri
er
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
Watch videos on YouTube.
Chapt
Channel Name: Zarif Bahaduri
er
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;
Watch videos on YouTube.
Chapt
Channel Name: Zarif Bahaduri
er
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;
Watch videos on YouTube.
Chapt
Channel Name: Zarif Bahaduri
er
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;
Watch videos on YouTube.
Chapt
Channel Name: Zarif Bahaduri
er
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
Watch videos on YouTube.
Operator Chapt
Channel Name: Zarif Bahaduri
er
Operator is a special symbol that tells the compiler to
perform specific mathematical or logical Operation.
To understand the
arithmetical operators
look at this program.
For more example see
the video of this slide..
Watch videos on YouTube.
the Chapt
ee e Channel Name: Zarif Bahaduri
er
S
x
m
l
p s
a thi
Increment & Decrement
es Slid
o r e o of Operators
F de
Vi
The increment operator (++) adds 1 to its operand, and the
decrement operator (--)subtracts 1 from its operand.
x = x+1; is the same as x++
x = x-1; is the same as x--
Chapter 2
Data Flow Statements
Watch videos on YouTube.
Chapt
Channel Name: Zarif Bahaduri
er
if Statement
The if statement
checks whether the test
condition is true or not.
If the test condition is
true, it executes the
The if keyword is followed by test condition inside
code/s inside the body parenthesis ( ). If the test condition is true, the
of if statement. But it codes inside curly bracket is executed but if test
condition is false, the codes inside curly bracket { }
the test condition is is skipped and control of program goes just below
false, it skips the code/s the body of if as shown in figure above
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.
Watch videos on YouTube.
Chapt
Channel Name: Zarif Bahaduri
er
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
Watch videos on YouTube.
Chapt
Channel Name: Zarif Bahaduri
er
Loops
• When you need to execute a block of Body of loop
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 Conditi Tru
on e
group of statement multiple time.
• following is the general form of a loop False
statement in most of the programming
language. Exit from
loop
Watch videos on YouTube.
Chapt
Channel Name: Zarif Bahaduri
er
Types of Loop
C++ provide the following types of Loop statement.
1. While loop
2. For loop
3. Do while loop
4. Nested loop
Watch videos on YouTube.
Chapt
Channel Name: Zarif Bahaduri
er
Program
Watch videos on YouTube.
Chapt
Channel Name: Zarif Bahaduri
er
Flow
For Loop Chart
Program
Watch videos on YouTube.
Chapt
Channel Name: Zarif Bahaduri
er
Program
Watch videos on YouTube.
Chapt
Channel Name: Zarif Bahaduri
er
Switch Statement
Watch videos on YouTube.
Chapt
Channel Name: Zarif Bahaduri
er
Programming 2
Watch videos on YouTube.
Chapt
Channel Name: Zarif Bahaduri
er
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.
Watch videos on YouTube.
Chapt
Channel Name: Zarif Bahaduri
er
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.
Watch videos on YouTube.
Chapt
Channel Name: Zarif Bahaduri
er
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.
Watch videos on YouTube.
Chapt
Channel Name: Zarif Bahaduri
er
Example
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
Watch videos on YouTube.
Chapt
Channel Name: Zarif Bahaduri
er
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: This is called a single-
dimension array.
Type arrayName[arraylength]; The arraySize must be an
integer constant greater
than zero and type can be
any valid C++ data type.
For example, to declare a
int num[10]; 10-element array we use
the this statement.
Watch videos on YouTube.
Chapt
Channel Name: Zarif Bahaduri
er
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 Printing array
Cout<<num[0]<<endl;
num[0]=20;
Cout<<num[1]<<endl;
num[1]=30; Cout<<num[2]<<endl;
Cout<<num[3]<<endl;
num[2]=40;
Cout<<num[4]<<endl;
num[3]=50;
num[4]=60;
num[5]=70
Watch videos on YouTube.
Chapt
Channel Name: Zarif Bahaduri
er
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[ Num[ Num[ Num[ Num[ Num[ Num[ Num[ Num[
0] 10 1] 2] 3] 4] 5] 6] 7] 8]
20 30 40 50 60 70 18 90
num
Watch videos on YouTube.
Chapt
Channel Name: Zarif Bahaduri
er
Multi-Dimensional Array
int num[2][4];
Initialization of 2D arrays
Direct Initialization
int num[3][3]={{2,3,4},{8,9,7},{22,33,44}};
Or
Through index number Printing 2D array
Num[0][0]=90 Cout<<num[0][0]<<endl;
Cout<<num[0] [1]<<endl;
Num[]0[1]=90 Cout<<num[0] [2]<<endl;
Num[0][2]=90 Cout<<num[1] [0]<<endl;
Cout<<num[1] [1]<<endl;
Num[]1[0]=90
Num[1][1]=90
Num[1][2]=90
Watch videos on YouTube.
Chapt
Channel Name: Zarif Bahaduri
er
cin>>sal[r][c]; cout>>sal[r][c];
} }
} }
Watch videos on YouTube.
Chapt
Channel Name: Zarif Bahaduri
er
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.
Watch videos on YouTube.
Chapt
Channel Name: Zarif Bahaduri
er
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
Watch videos on YouTube.
Chapt
Channel Name: Zarif Bahaduri
er
Example
#....
Using namespace std;
void show(); // function declaration
main(){
This is a user define function
Show(); // function calling
with no return type and no
}
parameter.
Void show(){ // function definition
int number=90;
Cout<<“welcome to this program..!!<< number;
}
Watch videos on YouTube.
Chapt
Channel Name: Zarif Bahaduri
er
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.
Watch videos on YouTube.
Chapt
Channel Name: Zarif Bahaduri
er
Example
Declaring inline
function
Definition of Inline
function
Watch videos on YouTube.
Chapt
Channel Name: Zarif Bahaduri
er
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).
Watch videos on YouTube.
Chapt
Channel Name: Zarif Bahaduri
er
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.
Watch videos on YouTube.
Chapt
Channel Name: Zarif Bahaduri
er
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;
Watch videos on YouTube.
Chapt
Channel Name: Zarif Bahaduri
er
Pointer to an array
Pointers are the To access the address of the all
variables that hold elements in array we use the for
loop
address. Pointers can
point at cells of an array int arr[5];
not only single variable int *ptr;
Pointer to 2D arrays
int table[5][5];
int *ptr;
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 we
Here thepassed
reference to that
the reference
value
of is passed.
the variable to Consider this
the function
example
Watch videos on YouTube.
Chapt
Channel Name: Zarif Bahaduri
er
Chapter 3
Structures
Watch videos on YouTube.
Chapt
Channel Name: Zarif Bahaduri
er
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.
Watch videos on YouTube.
Chapt
Channel Name: Zarif Bahaduri
er
Example
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.
O
r
recurse() { recurse(); //Function calls itself } int main { recurse(); //Sets off the recursion }
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 void
performed
recurse(){ where one
instruction is repeated, it look like a loop
recurse(); because
//Function it
calls itself
repeat some code. }
int main(){
recurse(); //Sets off the recursion
}
Watch videos on YouTube.
Chapt
Channel Name: Zarif Bahaduri
er
Example
Watch videos on YouTube.
Chapt
Channel Name: Zarif Bahaduri
er
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:
Watch videos on YouTube.
Chapt
Channel Name: Zarif Bahaduri
er
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
Watch videos on YouTube.
Chapt
Channel Name: Zarif Bahaduri
er
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.
Watch videos on YouTube.
Chapt
Channel Name: Zarif Bahaduri
er
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:
Watch videos on YouTube.
Chapt
Channel Name: Zarif Bahaduri
er
Function-Like Macros:
The #define
directives Can take
some arguments
Watch videos on YouTube.
Chapt
Channel Name: Zarif Bahaduri
er