Sabyasachi Moitra
Sabyasachi Moitra
Sabyasachi Moitra
moitrasabyasachi@hotmail.com
Introduction
2
OOP vs POP
OOP POP
Object Oriented Programming Procedure Oriented Programming
Programs are divided into what are known as Large programs are divided into smaller
objects. programs known as functions.
Objects may communicate with each other Data move openly around the system from
through functions. function to function.
Data is hidden & cannot be accessed by Does not have any proper way for hiding data.
external functions. Thus, more secure. Thus, less secure.
Follows bottom-up approach. Follows top-down approach.
3
OOP Terminologies
Terminology Description
Objects are the basic run-time entities in an
Object
object-oriented system.
A class is a collection of similar type of objects.
Example
Class Employee emp1;
emp1 OBJECT
Employee CLASS
4
Terminology Description
Inheritance is the process by which objects of one
Inheritance class acquire the properties of objects of another
class.
• Ability to take more than one form.
• Using a single function name to perform
different types of tasks is known as Function
Polymorphism Overloading.
• The process of making an operator to exhibit
different behaviors in different instances is
termed as Operator Overloading.
Connecting a method call to the method body is
known as Binding.
When type of the object is determined at compile-
Dynamic Binding
time, it is known as Static Binding.
When type of the object is determined at run-time,
it is known as Dynamic Binding.
Involves specifying the name of the object, name of
Message Passing
the function (message) & the information to be sent.
5
First C++ Program
Source Code
Output
(first.cpp)
#include<iostream.h> HELLO WORLD!!!
#include<conio.h>
void main()
{
clrscr();
cout<<"HELLO WORLD!!!";
getch();
}
6
Parameter Description
#include<iostream.h> • Includes the standard input output library
functions.
• The cout function is defined in iostream.h
file.
#include<conio.h> • Includes the console input output library
functions.
• The getch() function is defined in conio.h
file.
void main() • The main() function is the entry point of
every program in C++ language.
• The void keyword specifies that it returns
no value.
clrscr() Used to clear the output screen.
cout Used to print data on the console.
getch() Asks for a single character, blocks the screen
until any key is pressed.
7
Flow of C++ Program
8
Constants, Variables & Data
Types
9
Tokens
Keywords
Identifiers
TOKENS
Constants
Strings
Special Symbols
Operators
10
Keywords
▪ Reserved identifiers.
▪ Cannot be used as names for the program variables or other user-
defined program elements.
auto else long switch
break enum register typedef
case extern return union
char float short unsigned
const for signed void
continue goto sizeof volatile
default if static while
do int struct ….. 11
Identifiers
12
Constants
Constants refer to fixed values that do not change during the execution
of a program.
Integer
Constants 5
Numeric
Constants
Real Constants 5.5
CONSTANTS
Single Character
Constants 'A'
Character
String Constants "ABC"
Constants
Backslash
Character '\n'
Constants
13
Variables
▪ Identifier.
▪ Data name.
▪ Used to store a data value which can be changed during program
execution.
▪ E.g. sum, avg, etc.
14
Data Types
DATA TYPES
Primary or
Derived User-defined
Fundamental
15
User-defined Data Type
Data Type Description Example
Allow users to define an typedef int units;
identifier that would units batch1,batch2;
represent an existing data
typedef type.
Syntax
typedef type identifier;
enum identifier enum day
{value1,…,valuen}; {Mon,Tue,...,Sun};
The identifier is a user-defined enum day
enumerated data type which
week_st,week_end;
can be used to declare
variables that can have one of
enum
the values enclosed within the
braces.
After this definition, we can
declare variables of this new
type,
enum identifier v1,…,vn; 16
Storage Class
The keywords which are used to modify the properties of a variable are
called type qualifiers.
Qualifier Description Example
Tells the size of basic data long int a;
Size Qualifier
type.
Tells the sign of the unsigned int a;
Sign Qualifier
variable.
A variable declared as const int a;
constant cannot be
Constant Qualifier
modified during program
execution.
A variable declared as volatile int a;
volatile can be changed at
Volatile Qualifier any time by some external
sources during program
execution. 18
Access Modifiers
Modifier Description
private Visible to the class only.
protected Visible to the derived classes.
public Visible to the world.
19
Operators & Expressions
Operators
21
Types of Operator
Type Operators
Arithmetic Operators +, -, *, /, %
Relational Operators ==, !=, >, <, >=, <=
Logical Operators &&, ||, !
Increment & Decrement Operators ++, --
Conditional Operator ?:
Bitwise Operators &, |, ~, ^, <<, >>
=, +=, -=, *=, /=, %=, &=, |=, ^=, <<=,
Assignment Operators
>>=
Special Operators &, *, ,, sizeof()
22
Example
23
Output
24
Input Operator(>>) VS Output
Operator(<<)
25
Type Cast Operator
26
Scope Resolution Operator (::)
27
Example
28
Output
29
Decision Making & Branching
Simple if Statement
31
Example
32
if…else Statement
33
Example
34
Nested if or if…else Statements
35
Example
36
if…else if Ladder Statement
37
Example
38
switch Statement
39
Example
40
goto Statement
41
Example
42
Decision Making & Looping
What is loop?
44
Types of Loops
LOOPS
46
Example
getch();
}
47
do while Loop
48
Example
getch();
}
49
for Loop
50
Example
getch();
}
51
break VS continue
break continue
▪ Can appear in both switch and ▪ Can appear only in loop
loop statements. statements.
▪ When encountered, terminates ▪ When encountered, gets the
the block and gets the control control to the next iteration of
out of the switch or loop. the loop.
52
Example
break continue
#include <iostream.h> #include <iostream.h>
#include <conio.h> #include <conio.h>
clrscr(); clrscr();
for(i=1;i<=10;i++) for(i=1;i<=10;i++)
{ {
if(i==5) if(i==5)
break; continue;
getch(); getch();
} }
1234 1 2 3 4 6 7 8 9 10
53
Arrays
What is an Array?
55
Example (1D Array)
56
Example (2D Array)
57
Manipulating Strings
C++ Strings
59
C-Style Character String
60
Example
Source Code Output
#include<iostream.h> Char Array Value is: Hello
#include<conio.h>
String Literal Value is: Hello
void main ()
{
char ch[6]={'H', 'e', 'l', 'l', 'o', '\0'};
char ch2[6]=“Hello";
clrscr();
getch();
}
61
The getline() Function
62
Some String Handling Functions
Function Description
strlen() Finds the length of a string.
strcpy() Copies one string over another.
strcat() Concatenates two strings.
strcmp() Compares two strings.
…..
63
Example
64
String Class in C++
Output
66
User-defined functions
What is Function?
68
Types of Functions
Function
created by
programmer
getch()
Library User-defined
Function Function
69
Example
70
Passing Array to Function
71
What is Recursion?
72
Example
73
Function Overloading
74
Example
75
Inline Function
76
Example
77
Some Terminologies
Term Description
Parameters that appear in function calls.
Actual Parameters
s=add(10,20);
Parameters that appear in function
Formal Parameters declarations.
int add(int a,int b){…}
The point at which the function is being
invoked or called is known as the calling
Calling Function function.
s=add(10,20);
79
What is a Structure?
80
Example
81
Array of Structures
82
Example
83
What is a Union?
84
Example
85
Structure VS Union
Structure Union
A structure is defined with struct keyword. A union is defined with union keyword.
The size of a structure variable is equal to the sum of The size of a union variable is equal to the size of its
the individual sizes of its members. largest member.
Structure members are allocated distinct memory. Union members share common memory space.
86
Pointers
What are Pointers?
▪ Derived data type, i.e., built from one of the fundamental data types
available in C++.
▪ Contains memory address of another variable as its value.
88
Example
89
Pointer to Pointer
90
Example
91
Pointers & Arrays
92
Array of Pointers
93
Therefore, instead of making each row a fixed number of characters, we can make it a pointer
to a string of varying length,
char *name[3]={
"New Zealand",
"Australia",
"India“
};
Here, name is an array of three pointers to characters, each pointer pointing to a particular
name,
name[0] New Zealand
name[1] Australia
name[2] India
94
Thus, allocating only 28 bytes of memory,
N e w Z e a l a n d \0
A u t r a l i a \0
I n d i a \0
95
Example
96
Call by Value VS Call by Reference
Call by Value Call by Reference
A copy of the value is passed to the function. An address of the value is passed to the function.
Changes made inside the function is not reflected on Changes made inside the function is reflected outside
other functions. the function also.
Actual and formal arguments will be created in Actual and formal arguments will be created in same
different memory locations. memory location.
Original value is not modified. Original value is modified.
97
Example (Call by Value)
98
Example (Call by Reference)
99
Static Memory Allocation VS Dynamic
Memory Allocation
Static Memory Allocation Dynamic Memory Allocation
Memory allocated at compile time. Memory allocated at run time.
Memory can’t be increased during execution . Memory can be increased during execution .
100
Dynamic Memory Allocation Functions
▪ malloc()
- Allocates single block of requested memory.
- Doesn't initialize memory at execution time, so it has garbage value
initially.
ptr=(cast-type*)malloc(byte-size)
▪ calloc()
- Allocates multiple blocks of requested memory, each of same size.
- Initially initialize all bytes to zero.
ptr=(cast-type*)calloc(number,byte-size)
101
Dynamic Memory Allocation Functions (2)
▪ realloc()
- Reallocates the memory occupied by malloc() or calloc() functions.
ptr=realloc(ptr, new-size)
▪ free()
- Frees the dynamically allocated memory.
free()
102
Example (malloc())
103
Example (calloc())
104
Example (realloc())
105
Classes & Objects
What is a Class?
▪ A class is a way to bind the data & its associated methods together.
▪ General form of a class definition:
107
Access Modifiers in C++
108
What is an Object?
class-name object-name;
109
Example
110
Array within Class
111
Array of Objects
112
Static Member Variable
113
Example
114
Static Member Function
class-name :: function-name;
115
Example
116
Friend Function
117
Example
118
Constructor & Destructor
What is Constructor?
120
Types of Constructor
CONSTRUCTOR
constructor
constructor with
without
parameters
parameters
Default Parameterized
121
Default Constructor
124
Example
125
Copy Constructor
126
Example
127
Constructor VS Function
Constructor Function
Constructor is used to initialize the state Function is used to expose behaviour of
of an object. an object.
Constructor must not have return type. Function must have return type.
Constructor is invoked implicitly. Function is invoked explicitly.
Constructor name must be same as the Function name may or may not be same
class name. as class name.
128
What is Destructor?
129
Example
130
this Pointer
131
Example
132
Operator Overloading
133
What is Operator Overloading?
134
Example
135
Inheritance
136
What is Inheritance?
137
Types of Inheritance
138
Single Inheritance
139
Visibility of Inherited Members
140
Multiple Inheritance
141
Hierarchical Inheritance
142
143
Multilevel Inheritance
144
Hybrid Inheritance
(combination of more than one inheritance)
145
Virtual Base Class
▪ When a class is made virtual base class, only one copy of that class is
inherited, regardless of how many inheritance paths exist between
the virtual base class & a derived class.
▪ Thus duplication of inherited members due to multipath inheritance
can be avoided.
146
Example
147
148
Abstract Class
149
Example
150
Exception Handling
151
What is an Exception?
152
Exception Handling Mechanism
153
Example
154
Template
What is a Template?
156
▪ The general format of a class template is:
template<class T>
class classname
{
//body
};
▪ The syntax for defining an object of a template class is:
classname<type> objectname(arglist);
157
Example (template class)
158
Example (function template)
159
Working with Files
What is File?
161
▪ To overcome the discussed problems the concept of files is employed
in C++ programming.
▪ Reading from and writing to a file requires the standard C++ library
called fstream.
▪ The data types defined in fstream library are:
Data Type Description
Used to create files, write information to
fstream
files, and read information from files.
ifstream Used to read information from files.
Used to create files and write
ofstream
information to the files.
162
Writing to a File
163
Reading from a File
164
Some File Opening Modes
Mode Description
ios::app Append mode
ios::ate Go to the end-of-file on opening
ios::in Open a file for reading
ios::out Open a file for writing
ios::trunc Deletes the contents of the file if it exists
…..
165
References
166