C++ II Sem 2020-21 Part-1
C++ II Sem 2020-21 Part-1
Application of OOP:
• Real-time system
• Simulation and modeling
• Object-oriented data bases
• Hypertext, Hypermedia, and expertext
• AI(Artificial Intelligence) and expert systems
• Neural networks and parallel programming
• Decision support and office automation systems
• CIM/CAM/CAD systems.
C++ Data Types
All variables use data-type during declaration therefore, we can say that
“data types are used to tell variables the type of data it can store”.
The Data types in C++ are mainly divided into two types:
1. Primitive Data Types: These data types are built-in or predefined data types
and can be used directly by the user to declare variables. example: int, char ,
float, bool etc. Primitive data types available in C++ are:
o Integer
o Character
o Boolean
o Floating Point
o Double Floating Point
2. Abstract or user defined data type: These data types are defined by user itself.
Like, defining a class in C++ or a structure.
• Integer: Keyword used for integer data types is int. Integers typically requires 4
bytes of memory space and ranges from -2147483648 to 2147483647.
• Character: Character data type is used for storing characters. Keyword used for
character data type is char. Characters typically requires 1 byte of memory space
and ranges from -128 to 127 or 0 to 255.
• Boolean: Boolean data type is used for storing boolean or logical values. A
boolean variable can store either true or false. Keyword used for boolean data
type is bool.
• Floating Point: Floating Point data type is used for storing single precision
floating point values or decimal values. Keyword used for floating point data type
is float. Float variables typically requires 4 byte of memory space.
• Double Floating Point: Double Floating Point data type is used for storing
double precision floating point values or decimal values. Keyword used for
double floating point data type is double. Double variables typically requires 8
byte of memory space.
Data type Modifiers: As the name implies, data type modifiers are used with the built-
in data types to modify the length of data that a particular data type can hold. Data type
modifiers available in C++ are:
• Signed
• Unsigned
• Short
• Long
Based on the type of modifiers the size and range of the primitive/ primary data type is
may vary. The following table shows the data type and their respective size and range.
C++ Tokens:
“Each individual word and punctuation is referred to as a token in C++. Tokens
are the smallest building block or smallest unit of a C++ program”.
These following tokens are available in C++:
3. Constants: Constants are the fixed values that do not change their values thought
the c++ program execution. Constants may be like in the form of Numeric or
Character or Symbolic type.
4. Operators: An operator is a symbol that is used to perform a task. C++ has the
some operators like, arithmetic(+,-,*,/,%), logical(&&,||,!),
relational(<,>,<=,>=,==) operators etc.
5. Strings: A string is a sequence of character, which is also used as c++ token.
(or)
cout<<variable; Ex: cout<<area;
(Or)
cout<<” the string”<< variable; Ex:- cout<<” area of square is: “<<area;
cin>> :- This is a standard object of iostream class in C++, this is used as a input
operation in c++, here cin is pronounced (called) as c in and the symbol >> is called
extraction or get from operator.
Operators in C++:
Types of operators
1. Assignment Operator
2. Arithmetic or Mathematical Operators
3. Relational Operators
4. Logical Operators
5. Bitwise Operators
6. Shift Operators
7. Unary Operators
8. Ternary Operator
9. Comma Operator
Assignment Operator ( = )
Operates '=' is used for assignment, it takes the right-hand side (called rvalue) and copy
it into the left-hand side (called lvalue). Assignment operator is the only operator which
can be overloaded but cannot be inherited.
Mathematical Operators
There are operators used to perform basic mathematical operations. Addition (+) ,
subtraction (-) , diversion (/) multiplication (*) and modulus (%) are the basic
mathematical operators. Modulus operator cannot be used with floating-point numbers.
Relational Operators
You must notice that assignment operator is (=) and there is a relational operator, for
equivalent (==). These two are different from each other, the assignment operator
assigns the value to any variable, whereas equivalent operator is used to compare
values
Logical Operators
The logical operators are AND (&&) and OR (||). They are used to combine two
different expressions together.
These operators are mostly used in loops (especially while loop) and in Decision
making.
Bitwise Operators
There are used to change individual bits into a number. They work with only integral
data types like int and long and not with floating point values.
Shift Operators
Shift Operators are used to shift Bits of any variable. It is of three types,
Unary Operators
These are the operators which work on only one operand. There are many unary
operators, but increment ++and decrement -- operators are most used.
Other Unary Operators : address of &, dereference *, new and delete, bitwise not ~,
logical not !, unary minus - and unary plus +.
Ternary Operator
• if statement
• switch statement
• conditional operator statement
• goto statement
1. Simple if statement
2. If....else statement
3. Nested if....else statement
4. else if statement
Simple if statement
if...else statement
else-if ladder
The general form of else-if ladder is,
if(expression 1)
{
statement-block1;
}
else if(expression 2)
{
statement-block2;
}
else if(expression 3 )
{
statement-block3;
}
else
default-statement;
The expression is tested from the top(of the ladder) downwards. As soon as the true
condition is found, the statement associated with it is executed.
Example :
void main( )
{
int a;
cout << "enter a number";
cin >> a;
if( a%5==0 && a%8==0)
{
cout << "divisible by both 5 and 8";
}
else if( a%8==0 )
{
cout << "divisible by 8";
}
else if(a%5==0)
{
cout << "divisible by 5";
}
else
{
cout << "divisible by none";
}
getch();
}
Looping in C++
In any programming language, loops are used to execute a set of statements repeatedly
until a particular condition is satisfied.
How it works
1. while loop
2. for loop
3. do-while loop
while loop
Syntax :
variable initialization ;
while (condition)
{
statements ;
variable increment or decrement ;
}
for loop
for loop is used to execute a set of statement repeatedly until a particular condition is
satisfied. we can say it an open ended loop. General format is,
for(initialization; condition ; increment/decrement)
{
statement-block;
}
In for loop we have exactly two semicolons, one after initialization and second after
condition. In this loop we can have more than one initialization or
increment/decrement, separated using comma operator. for loop can have only
one condition.
Nested for loop
We can also have nested for loop, i.e one for loop inside another for loop. Basic syntax
is,
for(initialization; condition; increment/decrement)
{
for(initialization; condition; increment/decrement)
{
statement ;
}
}
do while loop
In some situations it is necessary to execute body of the loop before testing the
condition. Such situations can be handled with the help of do-while loop. do statement
evaluates the body of the loop first and at the end, the condition is checked
using while statement. General format of do-while loop is,
do
{
....Body of the loop
.....
} while(condition);
When break statement is encountered inside a loop, the loop is immediately exited and
the program continues with the statement immediately following the loop.
2) continue statement
It causes the control to go directly to the test-condition and then continue the loop
process. On encountering continue, cursor leave the current cycle of loop, and starts
with the next cycle.
C++ Function
C++ functions are basic building blocks in a program. All C++ programs are written
using functions to improve re-usability, understandability and to keep track on them.
“A C++ Function is a self contained block that is used to complete a task. A large C++
program is divided into basic building blocks called C++ function. C++ function contains
set of instructions enclosed by “{ }” which performs specific operation in a C++
program.
1. Function declaration: This informs compiler about the function name, function
parameters and return value’s data type.
2. Function calling: It is nothing accessing the functionality of a function.
There are two ways that a C function can be called from a program. They are,
1. Call by value
2. Call by reference
1. Call by value:
• In call by value method, the value of the variable is passed to the function as
parameter.
• The value of the actual parameter cannot be modified by formal parameter.
• Different Memory is allocated for both actual and formal parameters. Because,
value of actual parameter is copied to formal parameter.
Note:
#include<iostream.h>
#include<conio.h>
void swap(int a, int b);
void main()
{
int m = 22, n = 44;
cout<<" values before swap”<< m<<n;
swap(m, n);
getch();
}
void swap(int a, int b)
{
int tmp;
tmp = a;
a = b;
b = tmp;
cout<<”values after swap <<a<<b;
Output:
values before swap 22 44
values after swap 44 22
2. Call by reference:
• In call by reference method, the address of the variable is passed to the function
as parameter.
• The value of the actual parameter can be modified by formal parameter.
• Same memory is used for both actual and formal parameters since only address
is used by both parameters.
• In this program, the address of the variables “m” and “n” are passed to the
function “swap”.
• These values are not copied to formal parameters “a” and “b” in swap function.
• Because, they are just holding the address of those variables.
• This address is used to access and change the values of the variables.
#include<iostream.h>
#include<conio.h>
void swap(int *a, int *b);
void main()
{
int m = 22, n = 44;
cout<<" values before swap”<< m<<n;
swap(&m, &n);
getch();
}
void swap(int *a, int *b)
{
int tmp;
tmp = *a;
*a = *b;
*b = tmp;
cout<<”values after swap <<a<<b;
}
Output: