[go: up one dir, main page]

0% found this document useful (0 votes)
18 views95 pages

C++ 1st and 2nd Semester

The document provides an overview of programming in C and C++, including their history, features, and uses. It covers fundamental concepts such as data types, variables, operators, control statements, and loops, along with examples and explanations. Additionally, it introduces the structure of a C++ program and the role of compilers and IDEs in programming.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views95 pages

C++ 1st and 2nd Semester

The document provides an overview of programming in C and C++, including their history, features, and uses. It covers fundamental concepts such as data types, variables, operators, control statements, and loops, along with examples and explanations. Additionally, it introduces the structure of a C++ program and the role of compilers and IDEs in programming.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 95

Watch videos on YouTube.

Channel Name: Zarif Bahaduri

Programming in
C/C++
By Zarif Bahaduri
Watch videos on YouTube.
Chapt
Channel Name: Zarif Bahaduri
er s

Select Chapter

Chapter 1 Chapter 2 Chapter 3 Chapter 4

Chapter 5 Chapter 6 Chapter 7 Chapter 8


Watch videos on YouTube.
Chapt
Channel Name: Zarif Bahaduri
er

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

Creator of C++ Language


Watch videos on YouTube.
Chapt
Channel Name: Zarif Bahaduri
er

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

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
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

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.
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

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.
Watch videos on YouTube.
Chapt
Channel Name: Zarif Bahaduri
er

Data Types & Variables


• Data means characteristic of a data. Bellow are some of
the dataType
types in C++,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


Watch videos on YouTube.
Chapt
Channel Name: Zarif Bahaduri
er

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;
Watch videos on YouTube.
Chapt
Channel Name: Zarif Bahaduri
er

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
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.

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
Watch videos on YouTube.
Arithmetic operator program Chapt
Channel Name: Zarif Bahaduri
er

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--

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 // Prefix increment x++ // Postfix


form increment form
Watch videos on YouTube.
Chapt
Channel Name: Zarif Bahaduri
er

Chapter 2
Data Flow Statements
Watch videos on YouTube.
Chapt
Channel Name: Zarif Bahaduri
er

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.
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

inside the body of if


statement.
Watch videos on YouTube.
Chapt
Channel Name: Zarif Bahaduri
er
Flow Chart of if Statement
Watch videos on YouTube.
Chapt
Channel Name: Zarif Bahaduri
er

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

Else if flow Chart


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

Nested if Flow Chart


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

While loop Flow


Chart

• A while loop statement


repeatedly executes a
target statement as long
as a given condition is
true
• Syntax: While (condition
){
Statements
}
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

• 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.
• for(initialization
Syntax: ; condition ;
increment){
Statements;
}
Watch videos on YouTube.
Chapt
Channel Name: Zarif Bahaduri
er

Program
Watch videos on YouTube.
Chapt
Channel Name: Zarif Bahaduri
er

Do… While loop Flow


Chart

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.
do{
Syntax:
Statements;
}while
(condition)
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

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.
Watch videos on YouTube.
Chapt
Channel Name: Zarif Bahaduri
er
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.
Watch videos on YouTube.
Chapt
Channel Name: Zarif Bahaduri
er
Section 2 : Global Declaration
Section
• Global Variables are declared
here.
• Global Declaration may include
1. Declaring Structure
2. Declaring Class
3. Declaring Variable
Watch videos on YouTube.
Chapt
Channel Name: Zarif Bahaduri
er
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.
Watch videos on YouTube.
Chapt
Channel Name: Zarif Bahaduri
er

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.
Watch videos on YouTube.
Chapt
Channel Name: Zarif Bahaduri
er
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.
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

More example will be discussed


Watch videos on YouTube.
Chapt
Channel Name: Zarif Bahaduri
er

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

Initialization with loops


int num[9];
Output
for(int i=0;i<10;i++){ 123456789
Or
1
cin>>num[i]=i;
2
}
3
for(int i=0;i<10;i++){ 4
5
6
cout<<num[i]<<endl; 7
8
}
9
Watch videos on YouTube.
Chapt
Channel Name: Zarif Bahaduri
er

Multi-Dimensional Array
int num[2][4];

It is in the form of table which


is the collection of row and columns
It is also called 2D array
Watch videos on YouTube.
Chapt
Channel Name: Zarif Bahaduri
er

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

Initializing 2D array with loops


Initializing values Printing values
for(r=0;r<3;r++){ for(r=0;r<3;r++){
for(c=0;c<3;c++){ for(c=0;c<3;c++){

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

User Define Functions


A function which is declare by the user according to his\her
requirements is called user define functions

Every User define function has three parts


A
1. Declaration of a function function declaration tells
the compiler about a
2. Definition of a function function's name, return
type, and parameters
3. Calling of a function
A
function definition provide
A function calling is use to s the actual body of the
call the function or we can function.
say a function calling is use
to tell the function to
preform its task.
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

Syntax of a user define function


return_type
function_name( parameter_list){
Return Type: A function may return a value.
Body of a function
The return_type is the data type of the value the function
returns. Some function may not return if you use the void
Return value; 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.
Watch videos on YouTube.
Chapt
Channel Name: Zarif Bahaduri
er

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.
Watch videos on YouTube.
Chapt
Channel Name: Zarif Bahaduri
er

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;
}
Watch videos on YouTube.
Chapt
Channel Name: Zarif Bahaduri
er

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;
}
Watch videos on YouTube.
Chapt
Channel Name: Zarif Bahaduri
er

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;
}
Watch videos on YouTube.
Chapt
Channel Name: Zarif Bahaduri
er

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;
}
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

Calling inline function


many times

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;

int a[5]; For(int i=0;i<=5;i++){


ptr= &arr[i];
int* ptr;
cout<<ptr<<endl;
ptr = &a[2];
}
Watch videos on YouTube.
Chapt
Channel Name: Zarif Bahaduri
er

Pointer to 2D arrays

int table[5][5];
int *ptr;

This example describe you for(int r=0;r<5;r++){


how you can access the for(int c=0; c<5; c++){
address in two
denominational arrays. ptr=&table[r]
[c]<<endl;
}
}
Watch videos on YouTube.
Chapt
Channel Name: Zarif Bahaduri
er

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

Passing Pointers to a function

Now in this example we


pass a pointer variable to
a function instead of the
actual value and
reference.
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

Structures are declare out of the


main function and then called inside
the main function,
Watch videos on YouTube.
Chapt
Channel Name: Zarif Bahaduri
er

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
Watch videos on YouTube.
Chapt
Referencing pointer to Channel Name: Zarif Bahaduri
er
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.

O
r
recurse() { recurse(); //Function calls itself } int main { recurse(); //Sets off the recursion }

Watch videos on YouTube.


Chapt
Channel Name: Zarif Bahaduri
er

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

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 OOP
• Reinterpret Cast
Watch videos on YouTube.
Chapt
Channel Name: Zarif Bahaduri
er

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.
Watch videos on YouTube.
Chapt
Channel Name: Zarif Bahaduri
er

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;
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

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:
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

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

You might also like