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.
• You can convert values from one type to another
explicitly using the cast operator.
8. Prepared By: Asst. Prof. Sejal Jadav
•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 done 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);