Object-Oriented Programming (OOP)
Object-Oriented Programming (OOP)
(OOP)
Week – 04
Feb 10-14, 2019
Instructor: Basit Ali
Email: [email protected]
Object-Oriented Programming (OOP)
Type Casting
• Converting an expression of a given type into another type is
known as type-casting
Types of Type-Casting
•Implicit conversion
•Implicit conversions do not require any operator
•Perform by the compiler automatically
•Explicit conversion
•Perform by the programmer
•Data loss might occur
Implicit Casting
Implicit Casting
•Implicit conversions also include constructor or operator conversions, which affect
classes that include specific constructors or operator functions to perform
conversions
•For example
•Here, an implicit conversion happened
between objects of class A and class B,
because B has a constructor that takes
an object of class A as parameter.
Implicit Casting (Example)
Explicit Casting
• User defined typecast for the result to make it to a particular data type
• In C++, it can be done by two ways:
• By Assignment
Also known as forceful casting where type indicates the data type to which
the final result is converted.
• By cast Operator
A Cast operator is an unary operator which forces one data type to
be converted into another data type.
Explicit Casting
• Syntax:
(type_name) expression/value
Or
Type_name (expression/value)
• Example:
Float f = 15.65;
int a;
a= (int) f;
Or
• a = int(f);
Explicit Casting
Note: the output of all cout statements is 2.
• Syntax:
const class_name object;
6) Defining Class's Member function
as const
• A const member function never modifies data members
in an object.
• Syntax:
return_type function_name() const;
Example for const Object and const
Member function
Example for const Object and const
Member function
mutable Keyword