OBJECT ORIENTED DESIGN
AND PROGRAMMING (OODP)
(CODE: 21CSC101T)
2nd Semester, B. Tech
2024-2025
UNIT-1(i)
Dr. Bapuji Rao
Assistant Professor
Department of CSE(CORE)
UNIT – 1(i)
2
Introduction to C++
Data Types
SRM IST, DELHI-NCR CAMPUS
Constants
Variables – Value Variable, Reference Variable, Pointer Variable
Input / Output Operations
Type Conversions – Implicit & Explicit
Conditional Statements
Manipulators – endl, setw( ), setiosflags( ) & setprecision( ), setfill( ), setbase( )
Looping Statements
Arrays
DR. BAPUJI RAO, Department of CSE(CORE)
TEXT BOOKS
3
Robert Lafore, Object-Oriented Programming in C++, 4th Edition,
SRM IST, DELHI-NCR CAMPUS
SAMS Publishing, 2008.
Reema Thareja, Object Oriented Programming with C++, 1st Edition,
Oxford University Press, 2015.
Sourav Sahay, Object Oriented Programming with C++, 2nd Edition,
Oxford University Press, 2017.
E Balagurusamy, Object Oriented Programming with C++, Tata
McGraw-Hill Publishing Company Ltd.
DR. BAPUJI RAO, Department of CSE(CORE)
PROGRAMMING LANGUAGES
4
MONOLITHIC
PROGRAMMING
SRM IST, DELHI-NCR CAMPUS
PROCEDURAL
PROGRAMMING
STRUCTURED
PROGRAMMING
OBJECT ORIENTED
PROGRAMMING
DR. BAPUJI RAO, Department of CSE(CORE)
PROGRAMMING LANGUAGES
5
MONOLITHIC PROGRAMMING
• The program size is lengthy.
• It consists of global data and the code is sequential.
• The code is duplicated each time.
SRM IST, DELHI-NCR CAMPUS
• The flow of control achieves through jump.
• Examples: Assembly Language and Basic.
PROCEDURAL PROGRAMMING
• The program consists of sub-routines.
• Data items are global.
• Program controls through jump.
• Repetition of code can be avoided by using sub-routines.
• Suitable for medium sized software applications.
• Difficult to maintain and reuse of the program code.
• Examples: FORTRAN and COBOL.
DR. BAPUJI RAO, Department of CSE(CORE)
PROGRAMMING LANGUAGES
6
STRUCTURED PROGRAMMING OBJECT ORIENTED PROGRAMMING
• Program can be divided into individual • Data abstraction (new data type creation) is
SRM IST, DELHI-NCR CAMPUS
procedures that perform individual task. introduced.
• Procedures are independent and have own • Data and its operations are united together
declaration and processing logic. into a single unit.
• Parameter passing is possible. • Programs are designed around data being
• Control of scope of data. operated.
• Declaration of user-defined data type. • Relationship can be created between
• Projects can be broken into small modules. similar data type.
• Maintenance of large software system is • Examples: C++, Java, Smalltalk, Eiffel,
tedious and costly. Sather.(Pure Object Oriented Language)
• Examples: C and Pascal.
DR. BAPUJI RAO, Department of CSE(CORE)
INTRODUCTION TO C++
7
Developed by Bjarne Stroustrup in 1979 at Bell
Laboratories in Murray Hill, New Jersy.
SRM IST, DELHI-NCR CAMPUS
Initially it was referred as C-Classes.
In 1984, the name was changed to C++.
The idea of C++ comes from the C-Language’s Increment
Operator (++).
Therefore, C++ is the incremented version of C-Language.
In 1985, first commercial release of C++.
DR. BAPUJI RAO, Department of CSE(CORE)
C++ AND C RELATIONSHIP
8
C++ is derived from the C language.
C++ is a superset of C.
SRM IST, DELHI-NCR CAMPUS
Every correct statement in C is also a correct statement in C++, but
reverse is not true.
The most important elements are added to C to create C++ classes,
objects, and object-oriented programming.
C++ has improved approach to input/output (I/O) operation.
C++ has a new way to write comments.
DR. BAPUJI RAO, Department of CSE(CORE)
C++ CHARACTER SET
9
The 256 characters are classified into four groups.
SRM IST, DELHI-NCR CAMPUS
LOWERCASE UPPERCASE DIGITS SPECIAL
ALPHABETS ALPHABETS (0 to 9) CHARACTERS
(a to z) (A to Z) (+ - * / % , ‘ “
; : > < = # $
Space & | etc. )
DR. BAPUJI RAO, Department of CSE(CORE)
C++ TOKENS
10
The smallest individual element in C++ Program is known as TOKENS.
SRM IST, DELHI-NCR CAMPUS
The FIVE types of Tokens are:
• KEYWORDS (RESERVED WORDS)
• LITERALS (CONSTANTS)
• SEPARATORS
• IDENTIFIERS
• OPERATORS
DR. BAPUJI RAO, Department of CSE(CORE)
KEYWORDS
11
C++ has 63 Keywords (Reserved Words).
A keyword is a predefined word with a special meaning.
The word which is already defined in the C++ Compiler.
SRM IST, DELHI-NCR CAMPUS
It is used for its own purpose.
asm const else friend namespace return template union
auto const_cast enum goto new short this unsigned
bool continue explicit if operator signed throw using
break default export inline private sizeof true virtual
case delete extern int protected static try void
catch do false long public static_cast typedef volatile
char double float mutable register struct typeid wchar_t
class dynamic_cast for reinterpret_cast switch typename while
NOTE: C-Keywords are shown in BOLD FACE.
DR. BAPUJI RAO, Department of CSE(CORE)
LITERALS
12
It is a sequence of characters that represent a constant value to be in the
memory location.
SRM IST, DELHI-NCR CAMPUS
There are FOUR types of literals (constants).
• INTEGER LITERALS
• FLOATING POINT LITERALS
• CHARACTER LITERALS
• STRING LITERALS
DR. BAPUJI RAO, Department of CSE(CORE)
INTEGER LITERALS
13
It only consists of digits ranging from 0 to 9.
Examples:
75
4
SRM IST, DELHI-NCR CAMPUS
2143 are valid.
A special character + or – may be prefixed to mark as signed integer constant.
Examples:
-236 2-36
+5 are valid. 5+ are invalid.
No other special characters are allowed.
Examples:
3%
2,500
5.5 are invalid.
Here [% , .] are special characters.
DR. BAPUJI RAO, Department of CSE(CORE)
FLOATING POINT LITERALS
14
It only consists of digits ranging from 0 to 9 and a special character
dot (.) is used.
Examples:
2.5 2.5.
SRM IST, DELHI-NCR CAMPUS
5.0 5. are invalid.
0.2
.2 are valid.
A special character + or – may be prefixed to mark as signed
floating constant.
Examples:
-65.6 0-65.6
+4.0 are valid. 4+0.0 are invalid.
No other special characters are used.
Examples: 2.5%
5500.50/- are invalid.
DR. BAPUJI RAO, Department of CSE(CORE)
CHARACTER LITERALS
15
Any one character of the character set is enclosed in between single quotes (' ').
SRM IST, DELHI-NCR CAMPUS
Examples:
'S' S
's' 'HI'
'+' ' '
'6' ‘123’
' ' are valid. 'A+' are invalid.
DR. BAPUJI RAO, Department of CSE(CORE)
STRING LITERALS
16
More than one character of the character set is enclosed in between double
quotes (" ").
SRM IST, DELHI-NCR CAMPUS
Examples:
"SRM" SRM
" " ""
"C++" 'C++'
"243" 243
"B/67" are valid. B/67 are invalid.
DR. BAPUJI RAO, Department of CSE(CORE)
SEPARATORS
17
It is a symbol used to separate and arrange a group of code.
The EIGHT types of separators are:
• Parentheses ( )
SRM IST, DELHI-NCR CAMPUS
• Square Brackets [ ]
• Curly Braces { }
• Semicolon ( ; )
• Comma ( , )
• Dot OR Period ( . )
• Right Arrow (→)
• Scope Resolution (::)
DR. BAPUJI RAO, Department of CSE(CORE)
IDENTIFIERS
18
To identify the elements (identifiers) of a program with a unique name.
The various elements (identifiers) of the program are:
• Variable Name
SRM IST, DELHI-NCR CAMPUS
• Label Name
• Function Name
• Macro Name
• Type Definition Name
• Structure Name
• Union Name
• Enumerated Name
• Constant Name
• Class Name
DR. BAPUJI RAO, Department of CSE(CORE)
RULE FOR IDENTIFIERS
19
It may only consists of alphabets.
Examples:
NUM
sum
SRM IST, DELHI-NCR CAMPUS
Add
Product are valid.
It may consists of both alphabets and numbers. But it should not start with a
number.
Examples:
N1 1N
Pro1 5a6b are invalid.
Sum12
a5b6 are valid.
DR. BAPUJI RAO, Department of CSE(CORE)
RULE FOR IDENTIFIERS
20
The special characters are not allowed except underscore ( _ ).
Examples:
add_two_nos Rate%
_diff add two nos are invalid.
SRM IST, DELHI-NCR CAMPUS
DIFF_ are valid.
Keywords are not used as an identifier.
Examples:
int
char
goto are invalid.
C++ is a case-sensitive language.
Examples:
SUM and sum are two different identifiers.
An identifier can be given up to 31 significant characters.
Examples: Rate5 - It has 5 numbers of characters.
NUMBER1 - It has 7 numbers of characters.
DR. BAPUJI RAO, Department of CSE(CORE)
OPERATORS
21
It is a symbol that takes one or more arguments and operates on them to
produce a result.
The various operators are:
SRM IST, DELHI-NCR CAMPUS
• Arithmetic (+, -, *, /, %) • sizeof( )
• Assignment (=) • Right Arrow (→)
• Unary Operator (++, --) • Scope Resolution (::)
• Relational (==, >, <, >=, <=, !=) • Insertion / Put To (<<)
• Logical (||, &&, !) • Extraction / Get From (>>)
• Ternary (?:) • Shorthand Operators (+=, -=, *=,
• Address (&) /=, %=, >>=, <<=)
• Indirection (*) • new
• Bitwise (|, &, ~, ^, >>, <<) • delete
DR. BAPUJI RAO, Department of CSE(CORE)
CLASSIFICATION OF OPERATORS
22
UNARY OPERATORS BINARY OPERATORS TERNARY OPERATOR
SRM IST, DELHI-NCR CAMPUS
The expression has ONE The expression has TWO The expression has THREE
operand and one operator. operands and one operator. operands and one operator.
Unary Increment (++) Assignment Operator ?:
Unary Decrement (--) Arithmetic Operators
Unary Minus ( - ) Shorthand Operators
Address Operator (&) Relational Operators
Indirection Operator(*) Logical AND, OR Operators
Logical Not Operator (!) Bitwise Operators
Bitwise 1’s Complement(~) Dot Operator
Scope Resolution Operator Right Arrow Operator
Scope Resolution Operator (::)
DR. BAPUJI RAO, Department of CSE(CORE)
UNARY OPERATOR
23
UNARY INCREMENT (++) UNARY DECREMENT (--)
It increments the present value of the It decrements the present value of the
SRM IST, DELHI-NCR CAMPUS
variable by 1. variable by 1.
Pre-fix Increment Post-fix Increment Pre-fix Decrement Post-fix Decrement
( ++a ) ( a++ ) ( --a ) ( a-- )
NOTE
Pre-fix: 1st evaluates, then assigns.
Post-fix: 1st assigns, then evaluates.
DR. BAPUJI RAO, Department of CSE(CORE)
ASSIGNMENT (=) OPERATOR
24
It assigns or stores a value in a variable.
Syntax:
SRM IST, DELHI-NCR CAMPUS
L-Value = R-Value;
VARIABLE CONSTANT / VARIABLE / EXPRESSION
The R-Value may be a/an constant (Literal) or Variable or Expression.
The L-Value must be a variable.
DR. BAPUJI RAO, Department of CSE(CORE)
EXAMPLES
25
VALID STATEMENTS INVALID STATEMENTS
SRM IST, DELHI-NCR CAMPUS
a = 10; a 10 10 = a;
b = 5; b 5 a + b = sum;
sum = a + b; sum 15 sum/2.0 = avg;
avg = sum/2.0; avg 7 'A' = Section;
Section = 'A'; Section A
DR. BAPUJI RAO, Department of CSE(CORE)
ARITHMETIC OPERATORS PRIORITY
26
PRIORITY OPERATORS PURPOSE
Multiplication, Division (Quotient),
1st * / % Division (Remainder)
SRM IST, DELHI-NCR CAMPUS
2nd + - Addition, Subtraction
3rd = Assignment
The evaluation of Arithmetic Expression takes place from the left-hand
side.
The 1st Priority Operators that appears first is to be evaluated first. This
way all the 1st Priority operators are evaluated.
Then the 2nd and 3rd Priority Operators are evaluated.
DR. BAPUJI RAO, Department of CSE(CORE)
EVALUATION OF ARITHMETIC EXPRESSION
27
Result = 5 * 4 / 10 + 6 * 2 + 8 / 2 – 5 * 3;
EVALUATION PROCESS
SRM IST, DELHI-NCR CAMPUS
Left Right
Result = 5 * 4 / 10 + 6 * 2 + 8 / 2 – 5 * 3 1st Operation 5 * 4 = 20
Result = 20 / 10 + 6 * 2 + 8 / 2 – 5 * 3 2nd Operation 20 / 10 = 2
Result = 2 + 6 * 2 + 8 / 2 – 5 * 3 3rd Operation 6 * 2 = 12
Result = 2 + 12 + 8 / 2 – 5 * 3 4th Operation 8 / 2 = 4
Result = 2 + 12 + 4 – 5 * 3 5th Operation 5 * 3 = 15
Result = 2 + 12 + 4 – 15 6th Operation 2 + 12 = 14
Result = 14 + 4 – 15 7th Operation 14 + 4 = 18
Result = 18 – 15 8th Operation 18 – 15 = 3
Result = 3
DR. BAPUJI RAO, Department of CSE(CORE)
BITWISE OPERATORS
28
It does BIT (Binary digIT) manipulation.
It operates only on char and int data.
SRM IST, DELHI-NCR CAMPUS
The int and char data may be signed or unsigned.
There are SIX types of bitwise operators.
OPERATOR PURPOSE
~ 1’s Complement
| OR
& AND
>> Right Shift Operator
<< Left Shift Operator
^ XOR (Exclusive OR)
DR. BAPUJI RAO, Department of CSE(CORE)
~ (1’s complement)
29
It changes BIT 1 to BIT 0, and vice-versa.
SRM IST, DELHI-NCR CAMPUS
SYNTAX: ~ Operand
EXAMPLES:
~111 = 000
~10011 = 01100
DR. BAPUJI RAO, Department of CSE(CORE)
>> (Right Shift Operator)
30
It shifts all bits right side depending on the place value as the Operand-2.
Syntax: Operand-1 >> Operand-2
SRM IST, DELHI-NCR CAMPUS
General Formula: Operand-1/2Operand-2
Example-1: Example-2:
Let a = 11010111 Let a = 9
Let b = a>>3 Let b = 3
b = 00011010 Let c = a >> b
c = 1 [a/2b = 9/23 = 9/8 = 1]
DR. BAPUJI RAO, Department of CSE(CORE)
<< (Left Shift Operator)
31
It shifts all bits left side depending on the place value as the Operand-2.
Syntax: Operand-1 << Operand-2
SRM IST, DELHI-NCR CAMPUS
General Formula: Operand-1X2Operand-2
Example-1: Example-2:
Let a = 11010111 Let a = 9
Let b = a<<3 Let b = 2
b = 11010000 Let c = a<<b
c = 36[aX2b = 9X22 = 9X4 = 36]
DR. BAPUJI RAO, Department of CSE(CORE)
| (BITWISE OR OPERATOR)
32
When both the BITs are 0 then the result is 0; otherwise the result is 1..
Syntax: Operand-1 | Operand-2
SRM IST, DELHI-NCR CAMPUS
Operand-1 OR Operand-2 Result Examples:
0 | 0 0 • 11011 | 10011 = 11011
0 | 1 1
• 110011 | 100 = 110111
• Let a = 9, b = 3
1 | 0 1
Let c = a | b
1 | 1 1 = 9 | 3 = 1001 | 11 = 1011.
DR. BAPUJI RAO, Department of CSE(CORE)
& (BITWISE AND OPERATOR)
33
When both the BITs are 1 then the result is 1; otherwise the result is 0..
SRM IST, DELHI-NCR CAMPUS
Syntax: Operand-1 & Operand-2
Operand-1 AND Operand-2 Result Examples:
0 & 0 0 • 11011 & 10011 = 10011
0 & 1 0
• 110011 & 100 = 110000
• Let a = 9, b = 3
1 & 0 0
Let c = a & b
1 & 1 1 = 9 & 3 = 1001 & 11 = 1001.
DR. BAPUJI RAO, Department of CSE(CORE)
^ (BITWISE XOR / EXCLUSIVE OR)
34
When both the bits are same then the result is 0; otherwise the result is 1..
Syntax: Operand-1 ^ Operand-2
SRM IST, DELHI-NCR CAMPUS
Operand-1 XOR Operand-2 Result Examples:
0 ^ 0 0 • 11011 ^ 10011 = 01000
• 110011 ^ 100 = 110111
0 ^ 1 1
• Let a=9, b=3
1 ^ 0 1 Let c = a ^ b
1 ^ 1 0 = 9 ^ 3 = 1001 ^ 11 = 1010
DR. BAPUJI RAO, Department of CSE(CORE)
SHORTHAND OPERATORS
35
It is used to write shorthand statements or shorthand expressions.
SRM IST, DELHI-NCR CAMPUS
The TWO types of shorthand operators are:
• Arithmetic Shorthand Operators (+=, -=, *=, /=, %=)
• Bitwise Shorthand Operators (>>=, <<=)
DR. BAPUJI RAO, Department of CSE(CORE)
EXAMPLES
36
Let a = 10, b =6
GENERAL EXPRESSION SHORTHAND EXPRESSION
SRM IST, DELHI-NCR CAMPUS
a=a+6 a += 6
a=a-b a -= b
a=a*6 a *= 6
a=a/6 a /= 6
a=a%6 a %= 6
a = a >> b a >>= b
a = a << 6 a <<= 6
DR. BAPUJI RAO, Department of CSE(CORE)
DATA TYPE CLASSIFICATIONS
37
PRIMITIVE (BUILT-IN) NON-PRIMITIVE (DERIVED)
SRM IST, DELHI-NCR CAMPUS
ARRAY
NUMERIC CHARACTER
ENUMERATED
INTEGER SINGLE STRUCTURE
UNION
FLOATING STRING
TYPE DEFINITION
CLASS
DR. BAPUJI RAO, Department of CSE(CORE)
C++ DATA TYPES
38
To store a type of data or value in a memory cell or block for manipulation.
Basically C++ has three types of data.
SRM IST, DELHI-NCR CAMPUS
• Integer
• Floating (Real)
• Character
DR. BAPUJI RAO, Department of CSE(CORE)
PRIMITIVE DATA TYPES
39
DATA TYPE KEYWORD SIZE IN BYTES RANGE
int / signed int -32,768 (-215 ) to +32,767 (+215 - 1)
2
unsigned int 0 to 65,535 (216 )
SRM IST, DELHI-NCR CAMPUS
INTEGER
long int / long / -2,14,74,83,648 (-231) to +2,14,74,83,647
signed long int 4 (+231 - 1)
unsigned long int 0 to 4,29,49,67,295 (232)
3.4 X 10-38 to 3.4 X 10+38
float 4
( 3.4e-38 to 3.4e+38 )
FLOATING 1.7 X 10-308 to 1.7 X 10+308
double 8
( 1.7e-308 to 1.7e+308 )
3.4 X 10-4932 to 3.4 X 10+4932
long double 10
( 3.4e-4932 to 3.4e+4932 )
SINGLE -128 (-27) to +127 (+27-1)
1
CHARACTER 0 to 255 ( 28-1)
char
Number of
STRING
Characters
DR. BAPUJI RAO, Department of CSE(CORE)
C++ STATEMENTS
40
DECLARATION STATEMENTS
SRM IST, DELHI-NCR CAMPUS
ASSIGNMENT STATEMENTS
OUTPUT STATEMENTS
INPUT STATEMENTS
DR. BAPUJI RAO, Department of CSE(CORE)
DECLARATION STATEMENTS
41
It is used to declare the type of variables and objects to be used in the
program.
SRM IST, DELHI-NCR CAMPUS
The variable and object must be declared before using in any statement of the
program.
The declaration statement (instruction) is written anywhere in the program.
DR. BAPUJI RAO, Department of CSE(CORE)
DECLARATION STATEMENTS
42
Syntax:
DataType Variable-1, Variable-2, …………;
SRM IST, DELHI-NCR CAMPUS
Examples:
int num, sum; num sum
float avg, price; avg price
char Gender; Gender
DR. BAPUJI RAO, Department of CSE(CORE)
ASSIGNMENT STATEMENTS
43
Declaration int a, b, sum; a b sum
Statements float avg; avg
SRM IST, DELHI-NCR CAMPUS
a = 10; a 10
Assignment b = 7; b 7
Statements sum = a + b; sum 17
avg = sum/2.0; avg 8.5
Declaration int a = 10; a 10
and int b = 7; b 7
Assignment
Statements int sum = a + b; sum 17
float avg = sum/2.0; avg 8.5
DR. BAPUJI RAO, Department of CSE(CORE)
TYPE CONVERSION (TYPE CASTING)
44
It changes the right-hand side expression value while storing in the left-hand
side variable in Arithmetic Instruction.
SRM IST, DELHI-NCR CAMPUS
It is of TWO types.
• IMPLICIT TYPE CONVERSION
It is an automatic process.
• Syntax:
VarName = Constant / Variable / Expression;
• EXPLICIT TYPE CONVERSION
It is done with the help of Typecast Operator .
• Syntax:
VarName = DataType (Constant / Variable / Expression);
DR. BAPUJI RAO, Department of CSE(CORE)
IMPLICIT EXAMPLES
45
float a, b; a 7 b 11.5 int f; f 97
int c; char d, e; d 'B' e 'A'
c 6
SRM IST, DELHI-NCR CAMPUS
a = 7; PROMOTED f = 'a';
b = 5 + 6.5; d = 'B';
c = 6.2; DEMOTED e = 65;
Promoted:
The smaller data type of right-hand side expression changes to the data type of the
left-hand side variable.
Demoted:
The higher data type of right-hand side expression changes to the data type of the
left-hand side variable.
DR. BAPUJI RAO, Department of CSE(CORE)
EXPLICIT EXAMPLES
46
int a, b; a b
float c, d; c d
SRM IST, DELHI-NCR CAMPUS
a = 7; a 7 b 2
b = 2;
c = a/b; PROMOTED
d = float(a)/b;
c 3 d 3.5
(OR)
d = a/float(b);
DR. BAPUJI RAO, Department of CSE(CORE)
OUTPUT STATEMENTS
47
The output statement is written with output stream (cout).
SRM IST, DELHI-NCR CAMPUS
It outputs: Console OUTput
a) Prompt Message(s).
b) Constant(s) Value.
c) Variable(s) Value.
d) All (Prompt Message(s), Constant(s) Value and
Variable(s) Value).
The cout stream is present in the header file <iostream.h>.
DR. BAPUJI RAO, Department of CSE(CORE)
SYNTAX
48
PRINTING A PROMPT MESSAGE
SRM IST, DELHI-NCR CAMPUS
cout<< "Prompt Message";
INSERTION (PUT TO) OPERATOR
PRINTING PROMPT MESSAGES
cout<<"Prompt-Message-1"<<"Prompt-Message-2"<< ......<<"Prompt-Message-N";
NOTE: More than one << (INSERTION OPERATOR) with one cout stream is called
as CASCADED OUTPUT OPERATION.
DR. BAPUJI RAO, Department of CSE(CORE)
EXAMPLES
49
PRINTING A PROMPT MESSAGE
cout<<"It is a C++ Programming";
SRM IST, DELHI-NCR CAMPUS
OUTPUT:
It is a C++ Programming
PRINTING PROMPT MESSAGES
cout<<"It is a "<<" C++ "<<" Programming";
OUTPUT:
It is a C++ Programming
DR. BAPUJI RAO, Department of CSE(CORE)
SYNTAX
50
PRINTING A CONSTANT VALUE
SRM IST, DELHI-NCR CAMPUS
cout<<Constant;
PRINTING CONSTANTS VALUE
cout<<Constant-1<<Constant-2<< ……..<<Constant-N;
DR. BAPUJI RAO, Department of CSE(CORE)
EXAMPLES
51
PRINTING A CONSTANT VALUE
cout<<786;
cout<< 'A';
SRM IST, DELHI-NCR CAMPUS
cout<<5+10;
Arithmetic Addition Expression
OUTPUT
786A15
PRINTING CONSTANT VALUES
cout<<786<< 'A' << 5+10;
OUTPUT
786A15
DR. BAPUJI RAO, Department of CSE(CORE)
SYNTAX
52
PRINTING A VARIABLE’S VALUE
SRM IST, DELHI-NCR CAMPUS
cout<< VariableName;
PRINTING VARIABLES VALUE
cout<<VariableName-1<<………………..<<VariableName-N;
DR. BAPUJI RAO, Department of CSE(CORE)
EXAMPLES
53
PRINTING VARIABLE’S VALUE
int a = 10, b = 15;
cout<<a;
SRM IST, DELHI-NCR CAMPUS
cout<<b;
cout<<a+b;
Arithmetic Addition Expression
OUTPUT
101525
PRINTING VARIABLES VALUE
int a = 10, b = 15;
cout<<a<<b<<a+b;
OUTPUT
101525
DR. BAPUJI RAO, Department of CSE(CORE)
SYNTAX
54
SRM IST, DELHI-NCR CAMPUS
PRINTING PROMPT MESSAGE, VARIABLE’S VALUE AND CONSTANT
cout << "Prompt-Message-1" << VariableName-1<< ..….. << "Prompt-Message-N"
<< VariableName-N;
DR. BAPUJI RAO, Department of CSE(CORE)
EXAMPLES
55
PRINTING PROMPT MESSAGE, VARIABLE’S VALUE AND CONSTANT
cout<<"First Number = "<<10<<"\n";
SRM IST, DELHI-NCR CAMPUS
cout<<"Second Number = "<<15<<"\n";
a = 10 + 15;
cout<<"Sum = "<<a;
OUTPUT
First Number = 10
Second Number = 15
Sum = 25
DR. BAPUJI RAO, Department of CSE(CORE)
EXAMPLES
56
PRINTING PROMPT MESSAGE, VARIABLE’S VALUE AND CONSTANT
int a = 10, b = 15;
SRM IST, DELHI-NCR CAMPUS
cout<<"First Number = "<<a<<"\n";
cout<<"Second Number = "<<b<<"\n";
cout<<"Sum = "<<a+b;
OUTPUT Arithmetic Addition Expression
First Number = 10
Second Number = 15
Sum = 25
DR. BAPUJI RAO, Department of CSE(CORE)
COMMENTS
57
// and /*… */ are used for commenting a statement (s) in C++.
SRM IST, DELHI-NCR CAMPUS
Syntax:
• // Any Comment Statement
Examples:
• // This is a C++ programming Comment.
• /* This is a C programming Comment. */
DR. BAPUJI RAO, Department of CSE(CORE)
INPUT STATEMENTS
58
The input statement is written with input stream (cin).
SRM IST, DELHI-NCR CAMPUS
Console INput
It allows you to input unknown values through the keyboard during
run-time of the program.
The cin stream is present in the header file <iostream.h>.
DR. BAPUJI RAO, Department of CSE(CORE)
SYNTAX
59
INPUT A VALUE TO A VARIABLE
SRM IST, DELHI-NCR CAMPUS
cin>>VariableName;
EXTRACTION (GET FROM) OPERATOR
INPUT VALUES TO THE VARIABLES
cin>>VariableName-1 >> VariableName-2 >> …….>> VariableName-N;
DR. BAPUJI RAO, Department of CSE(CORE)
EXAMPLES
60
int roll;
float avg;
char name [15];
SRM IST, DELHI-NCR CAMPUS
cin>>roll;
cin>>avg;
cin >>name;
OR
cin>>roll>>avg>>name;
More than one >> (EXTRACTION OPERATOR) with one cin stream is called as
CASCADED INPUT OPERATION.
DR. BAPUJI RAO, Department of CSE(CORE)
PROGRAM-1
61
// Find the sum and product of any two input numbers.
#include<iostream.h>
void main( )
SRM IST, DELHI-NCR CAMPUS
{ OUTPUT
float a, b, sum, pro;
cout<<"\n Enter First Number : ";
cin>>a;
cout<<" Enter Second Number : ";
cin>>b;
sum = a + b;
pro = a * b;
cout<<" First Number = "<<a<<"\n";
cout<<" Second Number = "<<b<<"\n";
cout<<" Sum = "<<sum;
cout<<"\n Product = "<<pro;
} DR. BAPUJI RAO, Department of CSE(CORE)
PROGRAM-2
62
// Swapping of two numbers. (Without temporary variable)
#include<iostream.h>
void main( )
{
SRM IST, DELHI-NCR CAMPUS
float num1, num2;
cout<<"\n Enter Two Numbers : ";
cin>>num1>>num2;
cout<<" The Numbers = "<<num1<<" "<<num2;
// Swapping Process
num1 = num1 + num2;
num2 = num1 – num2;
num1 = num1 – num2;
cout<<"\n The Swapped Numbers = "<<num1<<" "<<num2;
}
DR. BAPUJI RAO, Department of CSE(CORE)
OUTPUT
63
OR
SRM IST, DELHI-NCR CAMPUS
num1 num2
num1 7.000000 num2 12.000000
num1 19.000000 num2 12.000000
num1 19.000000 num2 7.000000
num1 12.000000 num2 7.000000
DR. BAPUJI RAO, Department of CSE(CORE)
CONSTANTS
64
A constant or literal can be defined in THREE different ways:
SRM IST, DELHI-NCR CAMPUS
• Using const Keyword
• Using #define
• Using enum (Enumerated Data Type)
DR. BAPUJI RAO, Department of CSE(CORE)
USING const KEYWORD
65
SYNTAX:
const DataType VariableName = CONSTANT / LITERAL;
EXAMPLES: Symbolic Name to the CONSTANT
• const int a = 66;
SRM IST, DELHI-NCR CAMPUS
• const ROLL = 3147;
Considers by default int data type
• const float Pi = 3.142;
• const char *ptr = "SRM IST C++";
• const char str[ ] = "SRM IST DELHI-NCR CAMPUS";
NOTE:
The symbolic name for the constant 66 is a, 3147 is ROLL , 3.142 is Pi , "SRM IST C++" is *ptr ,
"SRM IST DELHI-NCR CAMPUS" is str.
DR. BAPUJI RAO, Department of CSE(CORE)
USING #define
66
SYNTAX: Symbolic Name to the CONSTANT / LITERAL
#define MacroName CONSTANT / LITERAL
SRM IST, DELHI-NCR CAMPUS
Pre-Processor Directive
EXAMPLES:
• #define ROLL 3147
• #define SevenStars "* * * * * * *\n"
• #define PI 3.142
• #define SRM cout<<"SRM IST DELHI-NCR CAMPUS";
• #define AND &&
• #define OR ||
• #define DIGITS (a>=48 && a<=57)
DR. BAPUJI RAO, Department of CSE(CORE)
USING enum
67
Enumerated is a data type consisting of a set of named values called
elements or members or enumerators of the type.
It is called enumerated type or enumeration or enum.
SRM IST, DELHI-NCR CAMPUS
By default the elements or members of enumerator are int kind and the
initial default value is 0.
Syntax-1:
• enum enum_name { element-1, element-2, ……..};
• enum_name variable-1, variable-2, .........................;
Syntax-2:
• enum enum_name {element-1, element-2,……..} variable-1, ..........;
Example-1: Example-2:
enum color { PINK, RED, GREEN, BLUE, WHITE }; enum objects { Pearl, Doll, Diamond, Ruby } Obj;
enum color Col1, Col2; Obj is an enumerated variable of type objects.
Col1 and Col2 are two enumerated variables of type color. DR. BAPUJI RAO, Department of CSE(CORE)
enum EXAMPLES
68
Example-1:
enum color { PINK, RED, GREEN, BLUE, WHITE };
SRM IST, DELHI-NCR CAMPUS
enum color Col1, Col2;
Col1 and Col2 are two enumerated variables of type color.
Example-2:
enum objects { Pearl, Doll, Diamond, Ruby } Obj;
Obj is an enumerated variable of type objects.
DR. BAPUJI RAO, Department of CSE(CORE)
TYPES OF VARIABLES
69
VALUE VARIABLE
It stores the actual value/data.
Syntax:
SRM IST, DELHI-NCR CAMPUS
DataType Variable-1, Variable-2…....;
Examples: REFERENCE VARIABLE / VARIABLE ALIASES
int a=9; It refers to a value variable with another name.
float b =67.76; Syntax:
char c = '#';
DataType &Variable = valueVariable;
Examples:
char ch, &chi = ch; // chi is an alias of char ch
int b, &a =b; // a is an alias of int b
float y, &x =y; // x is an alias of float y float y, &x;
x = y; // invalid
DR. BAPUJI RAO, Department of CSE(CORE)
TYPES OF VARIABLES
70
POINTER VARIABLE
It holds the address of value variable or pointer variable of same data type.
Syntax: DataType *pointerVariable;
SRM IST, DELHI-NCR CAMPUS
Examples: Referencing / Indirection Operator
char *cptr;
int *iptr;
float *fptr;
Here pointer variables *cptr, *iptr, and *fptr can point to char, int, and float memory
blocks address respectively.
The size of the above three pointer variables are 2 bytes each irrespective of its data type
since the address of memory block is unsigned int data type.
DR. BAPUJI RAO, Department of CSE(CORE)
PROGRAM-3
71
#include<iostream.h>
void main( )
{
SRM IST, DELHI-NCR CAMPUS
float value, &ref = value, *ptr = &value;
cout<<"\n Enter a Number = ";
cin>>value;
// Displaying Value
cout<<"\n Direct Accessing Number = "<<value<<'\n';
cout<<"\n Indirect Accessing Number Through Reference = "<<ref<<'\n';
cout<<"\n Indirect Accessing Number Through Pointer = "<<*ptr<<'\n';
DR. BAPUJI RAO, Department of CSE(CORE)
Continue…
72
// Displaying Size in terms of BYTES
cout<<"\n Size of Value Variable 'value' = "<<sizeof(value)<<"\n";
SRM IST, DELHI-NCR CAMPUS
cout<<"\n Size of Reference Variable 'ref' = "<<sizeof(ref)<<"\n";
cout<<"\n Size of Pointer Variable 'ptr' = "<<sizeof(ptr)<<"\n";
// Displaying Address
cout<<"\n Address of 'value' Variable = "<<&value<<"\n";
cout<<"\n Address of 'value' Variable Through 'ref' Variable = "<<&ref<<"\n";
cout<<"\n Address of 'value' Variable Through 'ptr' Variable = "<<ptr;
}
DR. BAPUJI RAO, Department of CSE(CORE)
OUTPUT
73
SRM IST, DELHI-NCR CAMPUS
value 6.700000
0x8f1fff2
&ref *ptr 0x8f1fff2
0x8f1fff0
DR. BAPUJI RAO, Department of CSE(CORE)
new OPERATOR
74
It allocates memory during run time of a program.
The allocated memory's address assigned to a pointer variable.
Syntax for One Memory Block:
SRM IST, DELHI-NCR CAMPUS
dataType *pointerVariale = new dataType;
Examples:
• int *ptr = new int;
• float *a = new float;
• char *cptr = new char;
Syntax for Array of Memory Blocks:
dataType *pointerVariale = new dataType [size];
Examples:
• int *ptr = new int[7];
• float *qtr = new float[10];
DR. BAPUJI RAO, Department of CSE(CORE)
delete OPERATOR
75
It releases the dynamically allocated memory block.
Syntax:
delete pointerVariable;
SRM IST, DELHI-NCR CAMPUS
Examples:
int *p = new int; //Allocates one memory block of int kind.
int *q = new int[7]; //Allocates seven memory blocks of int kind.
*p = 66;
q[0] = 1; // *(q+0) = 1;
q[1] = 5; // *(q+1) = 5;
delete p, q; // It releases one and seven memory blocks of int kind
// held by pointers p and q.
DR. BAPUJI RAO, Department of CSE(CORE)
CONDITIONAL / SELECTION
STATEMENTS 76
The CONDITIONAL STATEMENTS are used to write condition(s) in a program.
The statement(s) is/are performed based on the result of the condition.
SRM IST, DELHI-NCR CAMPUS
The CONDITIONAL STATEMENTS are:
• if
• if...else
• Nested if
• Nested if…else
• Nested else…if
DR. BAPUJI RAO, Department of CSE(CORE)
CONDITIONAL / TERNARY
OPERATOR 77
The CONDITIONAL / TERNARY OPERATORS are also used to write
SRM IST, DELHI-NCR CAMPUS
conditions in the program.
The TWO Conditional Operators are:
• Ternary / Conditional Operator (?:)
• Nested Ternary / Conditional Operator (?:?:…….:)
DR. BAPUJI RAO, Department of CSE(CORE)
if Statement
78
It is used for ONLY ONE POSSIBILITY.
It bothers only TRUE case.
SRM IST, DELHI-NCR CAMPUS
Syntax (Single Statement) Syntax (Compound Statement)
if (Condition(s)) if (Condition(s))
Statement; {
Statement-1;
Statement-2;
--------------;
Statement-N;
}
DR. BAPUJI RAO, Department of CSE(CORE)
PROGRAM-4
79
Check a number is Even or Odd. (Using if Statement)
#include<iostream.h> #include<iostream.h>
void main( ) void main( )
SRM IST, DELHI-NCR CAMPUS
{ {
int num, rem; int num;
cout<<"Enter a Number : "; cout<<"Enter a Number : ";
cin>>num; OR cin>>num;
rem = num%2;
if (rem==0) if (num%2==0)
cout<<"Even Number"; cout<<"Even Number";
if (rem==1) if (num%2==1)
cout<<"Odd Number"; cout<<"Odd Number";
} }
DR. BAPUJI RAO, Department of CSE(CORE)
if...else Statement
80
It is used for ONLY TWO POSSIBILITIES.
It bothers either TRUE or FALSE case.
Syntax (Single Statement) Syntax (Compound Statement)
SRM IST, DELHI-NCR CAMPUS
if (Condition(s)) if (Condition(s))
{
Statement-1;
Statement-1;
else
----------------;
Statement-2;
Statement-N;
}
else
{
Statement-1;
----------------;
Statement-N;
}
DR. BAPUJI RAO, Department of CSE(CORE)
PROGRAM-5
81
Read two Numbers. Print the positive difference. (Using if…else statement)
#include<iostream.h>
void main( )
SRM IST, DELHI-NCR CAMPUS
{
float num1, num2, diff;
cout<<"Enter Two Numbers : ";
cin>>num1>>num2;
if (num1>num2)
diff = num1 – num2;
else
diff = num2 – num1;
cout<<"Positive Difference ="<<diff;
}
DR. BAPUJI RAO, Department of CSE(CORE)
Nested if Statement
82
It is used for MORE THAN ONE possibilities.
It bothers only TRUE case.
It performs statement(s), when all the conditions of a possibility are TRUE.
SRM IST, DELHI-NCR CAMPUS
Syntax: Example:
if (a>b)
if (Condition-1)
if (a>c)
if (Condition-2) cout<<"Largest="<<a;
……………….
if (b>a)
if (Condition-N)
if (b>c)
{
cout<<"Largest="<<b;
Statement(s);
} if (c>a)
if (c>b)
cout<<"Largest="<<c;
DR. BAPUJI RAO, Department of CSE(CORE)
Nested if…else Statement
83
Syntax: It is used for MORE THAN TWO possibilities.
if (Condition-1) It performs the statement(s), when all the conditions of a
………………. possibility are TRUE.
if (Condition-N)
SRM IST, DELHI-NCR CAMPUS
{ Example:
Statement(s); if(num>10)
}
else if(num>20)
{ if(num>30)
Statement(s); cout<<"More Than 30";
} else
……………… cout<<"More Than 20";
else else
{ cout<<"More Than 10";
Statement(s); else
} cout<<"Less Than or Equal to 10";
DR. BAPUJI RAO, Department of CSE(CORE)
PROGRAM-6
84
#include<iostream.h>
void main( )
{
int num;
SRM IST, DELHI-NCR CAMPUS
Check a number is a "Single Digit" or "Double cout<<"Enter a Number: ";
cin>>num;
Digits" or "Triple Digits" or "More than three if(num>=10)
if(num>=100)
Digits". (Using Nested...if...else Statement) if(num>=1000)
cout<<"More Than Three Digits Number";
else
cout<<"Triple Digits Number";
else
cout<<"Double Digits Number";
else
cout<<"Single Digit Number";
}
DR. BAPUJI RAO, Department of CSE(CORE)
Nested else…if Statement
85
It is used for MORE THAN TWO possibilities. Syntax:
if (Condition-1)
It performs the statement(s) of a particular possibility, {
SRM IST, DELHI-NCR CAMPUS
when the condition is TRUE. Statement-1(s);
}
Example-1: Example-2:
else if (Condition-2)
if(num>0) if(num==0) {
cout<<"Positive"; cout<<"Zero"; Statement-2(s);
}
else if(num<0) else if(num>0) ……………………
cout<<"Negative"; cout<<"Positive"; else
{
else else Statement-N(s);
cout<<"Zero"; cout<<"Negative"; }
DR. BAPUJI RAO, Department of CSE(CORE)
PROGRAM-7
86
#include<iostream.h>
void main( )
{
SRM IST, DELHI-NCR CAMPUS
int num;
Check a number is "Single Digit" or cout<<"Enter a Number: ";
cin>>num;
"Double Digits" or "Triple Digits" or "More if(num>=1000)
than three Digits". (Using Nested...else...if cout<<"More Than Three Digits Number";
else if(num>=100)
Statement) cout<<"Triple Digits Number";
else if(num>=10)
cout<<"Double Digits Number";
else
cout<<"Single Digit Number";
}
DR. BAPUJI RAO, Department of CSE(CORE)
switch…case Statement
87
It is a multi-selection or multi-conditional statement.
SRM IST, DELHI-NCR CAMPUS
It is used for MORE THAN ONE possibility.
It only checks for equal without using Relational Equal To (==) operator.
It is generally used for menu driven programs.
DR. BAPUJI RAO, Department of CSE(CORE)
PROGRAM-8
88
Check a number is Even or Odd (Using switch…case).
void main( ) void main( ) void main( ) void main( )
{ { { {
SRM IST, DELHI-NCR CAMPUS
int num , rem; int num; int num; int num , rem;
cout<<"Enter a Number:"; cout<<"Enter a Number:"; cout<<"Enter a Number:"; cout<<"Enter a Number:";
cin>>num; cin>>num; cin>>num; cin>>num;
rem = num%2; rem = num%2;
switch(rem) switch(num%2) switch(num%2) switch(rem)
{ { { {
case 0 : cout<<"Even"; case 0 : cout<<"Even"; case 0 : printf("Even"); case 1 : cout<<"Odd";
break; break; break; break;
case 1 : cout<<"Odd"; case 1 : cout<<"Odd"; default : printf("Odd"); default : cout<<"Even";
} } } }
} } } }
DR. BAPUJI RAO, Department of CSE(CORE)
LOGICAL OR in switch…case
89
Syntax: Example:
switch (Operand) switch (gender)
{
{
SRM IST, DELHI-NCR CAMPUS
case 'M':
case Op-1:
case 'm': cout<<"Male";
case Op-2: break;
…………………………… case 'F':
case 'f': cout<<"Female";
…………………………… break;
case Op-N: statement(s); case 'T':
case 't': cout<<"Third Gender";
break;
break;
default: statement(s); default: cout<<"Wrong Gender Character";
} }
DR. BAPUJI RAO, Department of CSE(CORE)
TERNARY / CONDITIONAL OPERATOR (? :)
90
It is equivalent to if...else statement.
It is a single lined statement.
SRM IST, DELHI-NCR CAMPUS
It performs at a time one statement.
It can also perform compound statement; such statements are kept in a
parentheses.
It may or may not return a value.
DR. BAPUJI RAO, Department of CSE(CORE)
EXAMPLES
91
(age>=18) ? cout<<"Eligible for Voting" : cout<<"Not Eligible for Voting";
SRM IST, DELHI-NCR CAMPUS
(a>b) ? cout<<"Largest="<<a : cout<<"Largest="<<b;
c = (a>b) ? a : b;
cout<<"Largest="<<c;
c = (a>b) ? a-b : b-a;
cout<<"Difference="<<c;
(a!=b) ? (s = a + b, cout<<"Sum="<<s)) : cout<<"Equal" ;
DR. BAPUJI RAO, Department of CSE(CORE)
NESTED TERNARY OPERATOR (? : ? : ….. :)
92
It is equivalent to Nested else…if statement.
Syntax:
SRM IST, DELHI-NCR CAMPUS
(Condition-1) ? Statement-1 : (Condition-2) ? Statement-2 : … : Statement-N;
Example-1:
(num>0) ? cout<<"Positive" : (num<0) ? cout<<"Negative" : cout<<"Zero";
Example-2:
res = (a>b && a>c) ? a : (b>c) ? b : c;
cout<<"Largest = "<<res;
DR. BAPUJI RAO, Department of CSE(CORE)
MANIPULATORS
93
The manipulator manipulates the data during printing time or inputting time.
There are two types of manipulators.
Manipulators with insertion Operator (<<) : It manipulates or formats the data while displaying
SRM IST, DELHI-NCR CAMPUS
or printing on the screen.
Examples:
• endl iostream.h
• setw(int width)
• setiosflags(ios::showpoint) and setprecision(int decimalPlaces)
iomanip.h
• setbase(int baseValue)
• setfill('character'/ char Variable)
DR. BAPUJI RAO, Department of CSE(CORE)
PROGRAM-1
94
// endl Manipulator Example
#include<iostream.h>
void main()
SRM IST, DELHI-NCR CAMPUS
{
int s1=45, s2=60, s3=70;
int total = s1+s2+s3;
float avg = total/3.0;
cout<<endl<<" Subject-1 = "<<s1<<endl;
cout<<" Subject-2 = "<<s2<<endl;
cout<<" Subject-3 = "<<s3<<endl;
cout<<" Total = "<<total<<endl;
cout<<" Average = "<<avg;
}
DR. BAPUJI RAO, Department of CSE(CORE)
PROGRAM-2
95
// Example of setiosflags(ios::showpoint) and setprecision()
#include<iostream>
#include<iomanip>
SRM IST, DELHI-NCR CAMPUS
using namespace std;
int main( )
{
float num = 13.4;
cout<<num<<endl;
// 2 width for number before decimal point and 2 width for
// number of digits after decimal point using
// setiosflags() manipulator
cout<<setiosflags(ios::showpoint)<<setprecision(4)<<num<<endl;
return 0;
}
DR. BAPUJI RAO, Department of CSE(CORE)
PROGRAM-3
96
// setw() Manipulator Example
#include<iostream.h>
#include<iomanip.h>
SRM IST, DELHI-NCR CAMPUS
void main( )
{
char n1[]="CINU", n2[]="SMILEY";
int age1 = 23, age2 = 29;
cout<<setw(10)<<"NAME"<<setw(5)<<"AGE"<<endl;
cout<<setw(10)<<"----"<<setw(5)<<"---"<<endl;
cout<<setw(10)<<n1<<setw(5)<<age1<<endl;
cout<<setw(10)<<n2<<setw(5)<<age2<<endl;
}
DR. BAPUJI RAO, Department of CSE(CORE)
PROGRAM-4
97
// setw() and setfill() Manipulators Example
#include<iostream>
#include<iomanip>
SRM IST, DELHI-NCR CAMPUS
using namespace std;
int main()
{
char n1[]="CINU", n2[]="SMILEY";
int age1 = 23, age2 = 29;
char ch = '.';
cout<<setw(10)<<"NAME"<<setw(5)<<"AGE"<<endl;
cout<<setw(10)<<"----"<<setw(5)<<"---"<<endl;
cout<<setw(10)<<setfill(ch)<<n1<<setw(5)<<setfill(ch)<<age1<<endl;
cout<<setw(10)<<setfill(ch)<<n2<<setw(5)<<setfill(ch)<<age2<<endl;
return 0;
}
DR. BAPUJI RAO, Department of CSE(CORE)
PROGRAM-5
98
// Example of Octal, Hexadecimal, and Decimal Numbers Using
// setbase() Manipulator
#include<iostream>
SRM IST, DELHI-NCR CAMPUS
#include<iomanip>
using namespace std;
int main()
{
int num1 = 27;
int num2 = 017;
int num3 = 0xA5;
cout<<endl<<" Decimal Value of num1 = "<<num1<<endl;
cout<<" Octal Value of num1 = "<<setbase(8)<<num1<<endl;
cout<<" Hexadecimal Value of num1 = "<<setbase(16)<<num1<<endl;
DR. BAPUJI RAO, Department of CSE(CORE)
Continue…
99
cout<<endl<<" Octal Value of num2 = "<<setbase(8)<<num2<<endl;
cout<<" Decimal Value of num2 = "<<setbase(10)<<num2<<endl;
cout<<" Hexadecimal Value of num2 = "<<setbase(16)<<num2<<endl;
cout<<endl<<" Hexadecimal Value of num3 = "<<setbase(16)<<num3<<endl;
SRM IST, DELHI-NCR CAMPUS
cout<<" Decimal Value of num3 = "<<setbase(10)<<num3<<endl;
cout<<" Octal Value of num3 = "<<setbase(8)<<num3<<endl;
return 0;
}
DR. BAPUJI RAO, Department of CSE(CORE)
MANIPULATORS
100
Manipulators with Extraction (>>) Operator: It manipulates or formats the data while inputting
data during run-time of the program.
Examples:
SRM IST, DELHI-NCR CAMPUS
• dec
• oct iostream.h
• hex
DR. BAPUJI RAO, Department of CSE(CORE)
PROGRAM-6
101
// Example of Input of Octal and Hexadecimal Numbers using oct and hex manipulator.
#include<iostream>
#include<iomanip>
using namespace std;
SRM IST, DELHI-NCR CAMPUS
int main( )
{
int num1, num2;
cout<<endl<<" Enter a Octal Number = ";
cin>>oct>>num1;
cout<<endl<<" Octal Value of num1 = "<<oct<<num1<<endl;
cout<<" Decimal Value of num1 = "<<dec<<num1<<endl;
cout<<" Hexadecimal Value of num1 = "<<hex<<num1<<endl;
DR. BAPUJI RAO, Department of CSE(CORE)
Continue…
102
SRM IST, DELHI-NCR CAMPUS
cout<<endl<<" Enter a Hexadecimal Number = ";
cin>>hex>>num2;
cout<<endl<<" Hexadecimal Value of num2 = "<<hex<<num2<<endl;
cout<<" Decimal Value of num2 = "<<dec<<num2<<endl;
cout<<" Octal Value of num2 = "<<oct<<num2<<endl;
return 0;
}
DR. BAPUJI RAO, Department of CSE(CORE)
LOOP
103
Repetition of the same statement(s) more than once is called a loop.
The THREE loop statements are:
SRM IST, DELHI-NCR CAMPUS
• for
• while
• do…while
DR. BAPUJI RAO, Department of CSE(CORE)
for LOOP
104
It is an ENTRY CONTROL LOOP.
The control gets inside the body part of the loop, when the CONDITION is TRUE.
SRM IST, DELHI-NCR CAMPUS
Syntax (Single Statement)
for (Initialization; Condition; Update)
Statement;
Syntax (Compound Statement)
for (Initialization; Condition; Update)
{
Statement-1;
-----------------
Statement-N;
}
DR. BAPUJI RAO, Department of CSE(CORE)
EXAMPLES
105
// Display SRM 5 times. // Display SRM 5 times.
SRM IST, DELHI-NCR CAMPUS
for(int i=1; i<=5; i++) for(int i=5; i>=1; i--)
cout<<"SRM\n"; cout<<"SRM\n";
// Display the 1st TEN Natural Numbers.
for(int i=1; i<=10; i++)
cout<<i<<"\n";
DR. BAPUJI RAO, Department of CSE(CORE)
while LOOP
106
It is also an ENTRY CONTROL LOOP.
The control gets inside the body part of the loop, when the CONDITION is
SRM IST, DELHI-NCR CAMPUS
TRUE.
Syntax (Single Statement) Syntax (Compound Statement)
while (Condition) while (Condition)
Statement; {
Statement-1;
-----------------
Statement-N;
}
DR. BAPUJI RAO, Department of CSE(CORE)
EXAMPLES
107
// Display SRM 5 times.
int i = 1; int i = 5;
while (i<=5) while (i>=1)
{ {
SRM IST, DELHI-NCR CAMPUS
cout<<"SRM\n"; cout<<"SRM\n";
i++; i--;
} }
// Display the 1st TEN Natural Numbers.
int i = 1; int i = 1;
while (i<=10) while (i<=10)
{
cout<<i++<<"\n"; cout<<i<<"\n";
i++;
}
DR. BAPUJI RAO, Department of CSE(CORE)
do…while LOOP
108
It is an EXIT CONTROL LOOP.
The control exit from the loop, when the CONDITION is FALSE.
SRM IST, DELHI-NCR CAMPUS
First it executes the statement(s), Then it checks the condition.
It is equivalent to CONDITIONAL goto Statement.
Syntax (Single Statement) Syntax (Compound Statement)
do do
Statement; {
Statement-1;
while (Condition);
----------------;
Statement-N;
} while (Condition);
DR. BAPUJI RAO, Department of CSE(CORE)
EXAMPLES
109
// Display SRM 5 times.
int i = 1; int i = 5;
do do
{ {
SRM IST, DELHI-NCR CAMPUS
cout<<"SRM\n"; cout<<"SRM\n";
i++; i--;
} while (i<=5); } while (i>=1);
// Display the 1st TEN Natural Numbers.
int i = 1; int i = 1;
do
{ do
cout<<"\n"<<i; cout<<"\n"<<i++;
i++;
} while (i<=10); while (i<=10);
DR. BAPUJI RAO, Department of CSE(CORE)
LOOP CONTROL STATEMENTS
110
It controls the LOOP STATEMENTS.
SRM IST, DELHI-NCR CAMPUS
The two Loop Control Statements are:
• break
• continue
DR. BAPUJI RAO, Department of CSE(CORE)
break STATEMENT
111
It takes the control out of the loop statement / switch…case statement
with a proper condition.
It is also used in switch…case statement.
SRM IST, DELHI-NCR CAMPUS
Syntax of break (in Loop Statement): Syntax of break (in switch…case):
Loop-Statement switch(Operand)
if (Condition(s)) {
case Operand-1: Statement(s);
break; break;
---------------------------------------
default: Statement(s);
break;
}
DR. BAPUJI RAO, Department of CSE(CORE)
EXAMPLES
112
Example-1: Example-2: Example-3:
switch(num%2)
SRM IST, DELHI-NCR CAMPUS
for (int i=1; i<=10; i++) int i=1;
{
{ while(i<=10)
case 0 : cout<<"Even";
if(i==5) break; {
break;
cout<<"SRM"<<endl; cout<<"SRM"<<endl;
case 1 : cout<<"Odd";
} i++;
break;
if(i==6) break;
}
}
DR. BAPUJI RAO, Department of CSE(CORE)
continue STATEMENT
113
It skips / ignores statement(s) in a loop with a proper condition and sends
SRM IST, DELHI-NCR CAMPUS
the control back to the loop statement.
It is generally used in LOOP STATEMENTS.
Syntax:
Loop-Statement
if (Condition(s)) continue;
DR. BAPUJI RAO, Department of CSE(CORE)
EXAMPLES
114
Example-2:
int i=1;
SRM IST, DELHI-NCR CAMPUS
Example-1: while (i<=6)
for (int i=1; i<=6; i++) {
{ if (i==3 || i==5)
if(i==3 || i==5) continue;
continue;
cout<<i<<" ";
} else
{
cout<<i<<" ";
i++;
}
}
DR. BAPUJI RAO, Department of CSE(CORE)
ARRAYS
115
An array stores a set of similar data in a sequential memory location
SRM IST, DELHI-NCR CAMPUS
under a common or unique name.
It is a NON-PRIMITIVE DATA TYPE.
It is also known as HOMOGENEOUS DATA TYPE.
The 3 types of ARRAYS are:
• Single Dimension / 1-Dimension / 1-D Array
• Double Dimension / 2-Dimension / 2-D Array
• Multi Dimension Array
DR. BAPUJI RAO, Department of CSE(CORE)
1-D ARRAY
116
It has one row but more than one columns.
Syntax:
SRM IST, DELHI-NCR CAMPUS
Data_Type Array_Name [dimension / size];
Examples:
INDEX
0 1 2 3 4
num
num[0] num[1] num[2] num[3] num[4]
• int num[5];
0 1 2 3 4 5 6 7 8 9
IN ITIAL
a
a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7] a[8] a[9]
• float a[10];
0 1 2 3 4 5
b • char b[6];
b[0] b[1] b[2] b[3] b[4] b[5]
DR. BAPUJI RAO, Department of CSE(CORE)
INITIALIZATION SYNTAX
117
Data_Type Array_Name [dimension] = { Value1, Value2, ……};
SRM IST, DELHI-NCR CAMPUS
OR
Data_Type Array_Name [ ] = { Value1, Value2, ………………};
NOTE:
Value1, Value2,……….: Constants / Expression Using Constants.
DR. BAPUJI RAO, Department of CSE(CORE)
EXAMPLES
118
int a[5] = {1, 4, 3, 7, 66}; 0 1 2 3 4
a 1 4 3 7 66
OR
SRM IST, DELHI-NCR CAMPUS
a[0] a[1] a[2] a[3] a[4]
int a[ ] = {1, 4, 3, 7, 66};
int b[5] = {7, 6, 9, 22*3, 4+3}; 0 1 2 3 4
OR b 7 6 9 66 7
int b[ ] = {7, 6, 9, 22*3, 4+3}; b[0] b[1] b[2] b[3] b[4]
DR. BAPUJI RAO, Department of CSE(CORE)
ACCESSING 1-D ARRAY ELEMENTS
119
The elements of 1-D array are accessed with the help of INDEX.
SRM IST, DELHI-NCR CAMPUS
The accessing starts from 0th index onwards.
Syntax:
Array_Name[Index]
DR. BAPUJI RAO, Department of CSE(CORE)
EXAMPLE
120
int a[5] = {1, 4, 3, 7, 66}; 0 1 2 3 4
OR a 1 4 3 7 66
SRM IST, DELHI-NCR CAMPUS
int a[ ] = {1, 4, 3, 7, 66}; a[0] a[1] a[2] a[3] a[4]
INDIVIDUAL ADDRESS ACCESSING COMMON ADDRESS ACCESSING USING LOOP
cout<<a[0]<<"\n"; for ( int i=0; i<5; i++)
cout<<a[1]<<"\n"; cout<<a[i]<<"\n";
cout<<a[2]<<"\n";
cout<<a[3]<<"\n";
cout<<a[4]<<"\n";
DR. BAPUJI RAO, Department of CSE(CORE)
INPUT PROCESS IN 1-D ARRAY
121
INDIVIDUAL ADDRESS 0 1 2 3 4
INPUT PROCESS
num
int num[5];
SRM IST, DELHI-NCR CAMPUS
100 102 104 106 108
cout<<"Enter a Number :";
cin>>num[0];
COMMON ADDRESS INPUT
cout<<"Enter a Number :";
PROCESS USING LOOP
cin>>num[1];
int num[5];
cout<<"Enter a Number :";
cin>>num[2]; for ( int i=0; i<5; i++)
{
cout<<"Enter a Number :";
cout<<"Enter a Number :";
cin>>num[3];
cin>>num[i];
cout<<"Enter a Number :";
cin>>num[4]; }
DR. BAPUJI RAO, Department of CSE(CORE)
PROGRAM-7
122
Read 10 numbers in an array. Then display all the numbers, its sum
and the average.
0 1 2 3 4 5 6 7 8 9 Enter a Number : 4
num Enter a Number : 1
SRM IST, DELHI-NCR CAMPUS
100 104 108 112 116 120 124 128 132 136
Enter a Number : 3
#include<iostream.h> Enter a Number : 5
void main( )
Enter a Number : 8
{
float num[10], sum = 0; Enter a Number : 6
int i; Enter a Number : 12
// input process
for ( i=0; i<10; i++) Enter a Number : 10
{ Enter a Number : 2
cout<<"Enter a Number :"; Enter a Number : 9
cin>>num[i];
}
DR. BAPUJI RAO, Department of CSE(CORE)
Continue…
123
0 1 2 3 4 5 6 7 8 9
num 4 1 3 5 8 6 12 10 2 9
100 104 108 112 116 120 124 128 132 136
cout<<"All 10 Numbers:"<<endl; All 10 Numbers:
SRM IST, DELHI-NCR CAMPUS
4
// Accessing array numbers 1
for ( i = 0; i < 10; i++ ) 3
{ 5
cout<<num[i]<<endl; 8
// sum process 6
sum = sum + num[i]; 12
} 10
float avg = sum/10; 2
9
cout<<"\nSum = "<<sum; Sum = 60
cout<<"\nAverage = "<<avg;
} Average = 6
DR. BAPUJI RAO, Department of CSE(CORE)
2-D ARRAY
124
It has more than one rows and one columns.
It is also known as MATRIX in Mathematics and TABLE in Business
Applications.
SRM IST, DELHI-NCR CAMPUS
Syntax:
• Data_Type Matrix_Name [Rows][Columns];
Example:
• int mat[2][3];
DR. BAPUJI RAO, Department of CSE(CORE)
INITIALIZATION SYNTAX
125
Data_Type Matrix_Name [Rows][Columns] = {{Val-1, Val-2, …}, {Val1, Val2, …}, …};
OR
SRM IST, DELHI-NCR CAMPUS
Data_Type Matrix_Name [ ][Columns] = {{Val-1, Val-2, …}, {Val-1, Val-2, …}, …};
OR
Data_Type Matrix_Name [Rows][Columns] = {Val-1, Val-2, ……};
OR
Data_Type Matrix_Name [ ][Columns] = {Val-1, Val-2, ……};
DR. BAPUJI RAO, Department of CSE(CORE)
EXAMPLE
126
int mat[3][2] = {{1, 4}, {3, 7}, {5, 9}};
OR
int mat[ ][2] = {{1, 4}, {3, 7}, {5, 9}};
SRM IST, DELHI-NCR CAMPUS
OR
int mat[3][2] = {1, 4, 3, 7, 5, 9};
OR
int mat[ ][2] = {1, 4, 3, 7, 5, 9};
DR. BAPUJI RAO, Department of CSE(CORE)
ACCESSING 2-D ARRAY ELEMENTS
127
The elements of 2-D array are accessed with the help of row-index and
SRM IST, DELHI-NCR CAMPUS
column-index.
The accessing starts from 0th row-index and 0th column-index.
Syntax:
Matrix_Name[Row_Index][Column_Index]
DR. BAPUJI RAO, Department of CSE(CORE)
EXAMPLE
128
int mat[3][2] = {1, 4, 3, 7, 5, 9};
OR
int mat[3][2] = {{1, 4}, {3, 7}, {5, 9}};
SRM IST, DELHI-NCR CAMPUS
INDIVIDUAL ADDRESS ACCESSING COMMON ADDRESS ACCESSING USING NESTED LOOP
cout<<mat[0][0]<<" ";
cout<<mat[0][1]; for ( int i=0; i<3; i++)
cout<<"\n"; {
cout<<mat[1][0]<<" "; for ( int j=0; j<2; j++)
cout<<mat[1][1]; cout<<mat[ i ][ j ]<<" ";
cout<<"\n";
cout<<"\n";
cout<<mat[2][0]<<" ";
cout<<mat[2][1]; }
cout<<"\n";
DR. BAPUJI RAO, Department of CSE(CORE)
INPUT PROCESS IN 2-D ARRAY
129
INDIVIDUAL ADDRESS INPUT 0 1 2
PROCESS
0 1 0 1 0 1
int mat[3][2];
SRM IST, DELHI-NCR CAMPUS
cout<<"Enter a Number : "; mat
cin>> mat[0][0]; 100 102 104 106 108 110
cout<<"Enter a Number : ";
COMMON ADDRESS INPUT PROCESS
cin>> mat[0][1];
USING NESTED LOOP
cout<<"Enter a Number : ";
cin>> mat[1][0];
cout<<"Enter a Number : "; for (int i=0; i<3; i++)
cin>> mat[1][1]; for (int j=0; j<2; j++)
cout<<"Enter a Number : "; {
cin>> mat[2][0]; cout<<"Enter a Number : ";
cout<<"Enter a Number : "; cin>>mat[i][j];
cin>> mat[2][1]; }
DR. BAPUJI RAO, Department of CSE(CORE)
PROGRAM-8
130
Read 10 numbers in a 2X5 Matrix. Then display the matrix, the
sum of even numbers and the sum of odd numbers.
0 1
0 1 2 3 4 0 1 2 3 4
mat
SRM IST, DELHI-NCR CAMPUS
100 102 104 106 108 110 112 114 116 118
#include<iostream.h> Enter a Number : 4
void main( ) Enter a Number : 1
{ Enter a Number : 3
int mat[2][5] , i, j, even = 0, odd = 0;
Enter a Number : 5
// Input Process
Enter a Number : 8
for ( i=0; i<2; i++)
Enter a Number : 6
for ( j=0; j<5; j++)
Enter a Number : 12
{
cout<<"Enter a Number : "; Enter a Number : 10
cin>>mat[i][j]; Enter a Number : 2
} Enter a Number : 9
DR. BAPUJI RAO, Department of CSE(CORE)
Continue…
131
0 1
cout<<nThe Matrix"<<endl<<endl;
0 1 2 3 4 0 1 2 3 4
for (i=0; i<2; i++) mat 4 1 3 5 8 6 12 10 2 9
{ 100 102 104 106 108 110 112 114 116 118
SRM IST, DELHI-NCR CAMPUS
for ( j=0; j<5; j++)
{
cout<<mat[i][j]<<" "; The Matrix
if (mat[i][j] % 2 == 0)
even = even + mat[i][j]; 4 1 3 5 8
else 6 12 10 2 9
odd = odd + mat[i][j];
} Sum of Evens = 42
cout<<"\n"; Sum of Odds = 18
}
cout<<endl<<"Sum of Evens = "<<even;
cout<<endl<<"Sum of Odds = "<<odd;
}
DR. BAPUJI RAO, Department of CSE(CORE)
SRM IST, DELHI-NCR CAMPUS
132
THANK YOU