1. Prepared By: Asst. Prof. Sejal Jadav
Unit-1
Principles of Object Oriented Programming Tokens,
expressions & Control Statements
(14 MARKS)
B.C.A & B.Sc.(IT) – 3
CS-13 C++ and Object Oriented
Programming
2. Prepared By: Asst. Prof. Sejal Jadav
Operators
• Input/output operators
• << output / insertion operator
• >> input / extraction operator
• Scope resolution operator
• ::
• Memory management operators
• New-memory allocation
operator
• Delete-memory release operator
• Member dereferencing operator
• ::* pointer-to-member declarator
• ->*pointer-to-member operator
• .* pointer-to-member operator
• Manipulators
• Endl-line feed operator
• Setw-field width operator
• Type cast operator
• Definition : An operator is a symbol that tells the compiler to
perform specific mathematical or logical manipulations.
3. Prepared By: Asst. Prof. Sejal Jadav
Scope Resolution Operator
• We know that the same variable name can be used in
different blocks in C++
• The scope of the variable extends from the point of its
declaration till the end of the block containing the
declaration.
4. Prepared By: Asst. Prof. Sejal Jadav
• The variable declared inside the block is called local
variable to that block.
• While blocks are nested the declaration in inner block
hides a declaration of the same variable in an outer
block.
5. Prepared By: Asst. Prof. Sejal Jadav
• Hence in C – language the global variable cannot be
accessed from the inner block.
• C++ resolves this problem by introducing a new
operator : : called scope resolution operator.
6. Prepared By: Asst. Prof. Sejal Jadav
Syntax :
: : variable_name
• This operator allows global version of the variable.
• Let’s see Example……. 12_scope_operator.cpp
7. Prepared By: Asst. Prof. Sejal Jadav
Type cast operator
•Type casting is a way to convert a variable
from one data type to another datatype.
•For example, if you want to store a long value
into a simple integer then you can typecast long
to int.
8. Prepared By: Asst. Prof. Sejal Jadav
•You can convert values from one type to
another explicitly using the cast operator.
•There are two types of type conversion in C++.
1. Implicit Conversion
2. Explicit Conversion (also known as Type
Casting)
9. Prepared By: Asst. Prof. Sejal Jadav
Implicit Type Conversion
• The type conversion that is automatically done by the
compiler is known as implicit type conversion.
• This type of conversion is also known as automatic
conversion.
• Let’s look at examples: 01_typecast, 02_typecast.cpp
11. Prepared By: Asst. Prof. Sejal Jadav
• In the program, we have assigned an int data to a
double variable.
num_double = num_int;
• Here, the int value is automatically converted to
double by the compiler before it is assigned to the
num_double variable. This is an example of implicit
type conversion.
12. Prepared By: Asst. Prof. Sejal Jadav
Data Loss During Conversion (Narrowing Conversion)
• As we have seen from the above example, conversion
from one data type to another is to data loss.
• This happens when data of a larger type is converted
to data of a smaller type.
14. Prepared By: Asst. Prof. Sejal Jadav
C++ Explicit Conversion
• When the user manually changes data from one type
to another, this is known as explicit conversion.
• This type of conversion is also known as type
casting.
15. Prepared By: Asst. Prof. Sejal Jadav
• There are three major ways in which we can use
explicit conversion in C++. They are:
1. C-style type casting (also known as cast notation)
2. Function notation (also known as old C++ style
type casting)
3. Type conversion operators
16. Prepared By: Asst. Prof. Sejal Jadav
C-style Type Casting
• As the name suggests, this type of casting is favored
by the C programming language. It is also known as
cast notation.
• The syntax for this style is:
(data_type)expression;
17. Prepared By: Asst. Prof. Sejal Jadav
For example,
// initializing int variable
int num_int = 26;
// declaring double variable
double num_double;
// converting from int to double
num_double = (double)num_int;
18. Prepared By: Asst. Prof. Sejal Jadav
Function-style Casting
• We can also use the function like notation to cast data
from one type to another.
• The syntax for this style is:
data_type(expression);
19. Prepared By: Asst. Prof. Sejal Jadav
For example,
// initializing int variable
int num_int = 26;
// declaring double variable
double num_double;
// converting from int to double
num_double = double(num_int);
20. Prepared By: Asst. Prof. Sejal Jadav
Manipulators
• Manipulators are operators used in C++ for
formatting output.
• The data is manipulated by the programmer's choice
of display.
• 1. endl Manipulator
• 2. setw Manipulator
21. Prepared By: Asst. Prof. Sejal Jadav
endl
• endl is the line feed operator in C++.
• It acts as a stream manipulator whose purpose is to
feed the whole line and then point the cursor to the
beginning of the next line.
• We can use n (n is an escape sequence) instead
of endl for the same purpose.
22. Prepared By: Asst. Prof. Sejal Jadav
setw
• This manipulator sets the minimum field width on
output.
Syntax:
setw(x)
• Example: 04_ manipulator.cpp
23. Prepared By: Asst. Prof. Sejal Jadav
•main() function is the entry point of any C++
program.
•It is the point at which execution of program is
started.
main function in c++
24. Prepared By: Asst. Prof. Sejal Jadav
•When a C++ program is executed, the execution
control goes directly to the main() function.
•Every C++ program have a main() function.
25. Prepared By: Asst. Prof. Sejal Jadav
Syntax
main() [OR int main()]
{
//body of program
return 0; //optional
}
• In the above syntax …..
26. Prepared By: Asst. Prof. Sejal Jadav
int :
• This is perfectly valid because the main() doesn’t
return any value to the operating system.
• In C++, by default, the main() returns int type
value to the operating system.
27. Prepared By: Asst. Prof. Sejal Jadav
•Here int keyword in the main() function
declaration is optional.
•If return statement at the end is missing,
most compilers will generate the warning
that the function should return a value.
28. Prepared By: Asst. Prof. Sejal Jadav
main:
•Main() is a name of function which is
predefined function in C++ library.
29. Prepared By: Asst. Prof. Sejal Jadav
#include<iostream.h>
int main()
{
cout<< “CCSIT College - Junagadh”;
return 0;
}
30. Prepared By: Asst. Prof. Sejal Jadav
Functions in c++
• What is Function?
• Definition, Declaration & Call
• Declaration
• Way to define a function
• Formal & Actual Arguments
• Types of formal arguments
• Call by value, call by address and call by reference
31. Prepared By: Asst. Prof. Sejal Jadav
What is Function?
• Function is block of code performing a unit task.
• Function definition is block of code
• Function is a way to achieve modularization. (no. of small
functions)
• Function are of two types : Library (predefined) function
and user –defined
• Predefined functions are
• declared in header files and
• defined in library files
32. Prepared By: Asst. Prof. Sejal Jadav
Function Definition, Declaration & Call
#include<iostream>
Using namespace std;
Main()
{
Void fun();
Cout<<“You are in main”;
Fun();
}
Void fun()
{
Cout<<“you are in fun”;
}
Function Definition
Function Call
Function Declaration
Declaration of cout & cin
33. Prepared By: Asst. Prof. Sejal Jadav
Function Declaration [Function PROTOTYPE]
• Function Declaration is also known as Function
Prototype
• Functions need to be declared before use (just like
variable)
• Function can be declared locally or globally
• Function has a name, return type and arguments.
Syntax:
• ReturnType functionName(argumentList);
34. Prepared By: Asst. Prof. Sejal Jadav
Way to define a function
• No argument no return value
• With argument no return value
• No argument with return value
• With argument with return value
35. Prepared By: Asst. Prof. Sejal Jadav
5
6
Formal & Actual Arguments
#include<iostream>
Int sum(int,int);
Main()
{
Int a=5,b=6;
Int s=sum(a,b);
Cout<<“Sum is :
“<<s;
}
Int sum(int x, int y)
{
Return (x+y);
}
a
b
s
a & b are actual argument
Sum() memory
block
main()
memory
block
x & y are formal argument
5
6
11
x
y
36. Prepared By: Asst. Prof. Sejal Jadav
• Let’s see example :callbyvalue.cpp
37. Prepared By: Asst. Prof. Sejal Jadav
Types of Formula Argument
• Formal arguments can be of three types
1. Ordinary variables of any type
2. Pointer variables
3. Reference variables
41. Prepared By: Asst. Prof. Sejal Jadav
Call by reference
• When formal arguments are reference variables, it is
function call by reference.
• The call by reference method the called function
does not create its own copy rather it refers to
original value only by different name i.e. reference.
42. Prepared By: Asst. Prof. Sejal Jadav
• When we pass arguments by reference, the formal
arguments int sum(int &x,int &y) in the called
function becomes aliases to the actual arguments in
the calling function.
• It means that when the called function is working
with its own arguments, it is actually working on the
original data.