0% found this document useful (0 votes)
6 views

Module 26

Uploaded by

ferozalircr
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Module 26

Uploaded by

ferozalircr
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

Module M26

L
Partha Pratim
Das
Programming in Modern C++

E
Weekly Recap

Objectives & Module M26: Polymorphism: Part 1: Type Casting

T
Outline

Type Casting

P
Comparison
Built-in Type
Promotion & Partha Pratim Das

N
Demotion
Unrelated Classes
Inheritance Hierarchy
Department of Computer Science and Engineering
Upcast
Downcast
Indian Institute of Technology, Kharagpur
Module Summary
[email protected]

All url’s in this module have been accessed in September, 2021 and found to be functional

Programming in Modern C++ Partha Pratim Das M26.1


Weekly Recap

Module M26

• Understood Hierarchy or ISA Relationship in OOAD and introduced the Semantics of

L
Partha Pratim
Das
Inheritance in C++

E
Weekly Recap
• Discussed the effect of inheritance on Data Members and Object Layout; and on
Objectives &
Member Functions with special reference to Overriding and Overloading

T
Outline

Type Casting
• Understood the need and use of protected Access specifier

P
Comparison
Built-in Type
Promotion &
• Discussed the Construction and Destruction process of class hierarchy and related

N
Demotion
Unrelated Classes
Object Lifetime
Inheritance Hierarchy
Upcast
• Using the Phone Hierarchy as an example analyzed the design process with inheritance
Downcast
• Introduced restricted forms of inheritance and protected specifier
Module Summary
• Discussed how private inheritance is used for Implemented-in-terms-of Semantics

Programming in Modern C++ Partha Pratim Das M26.2


Module Objectives

Module M26

• Understand type casting and the difference between implicit and explicit casting

L
Partha Pratim
Das
• Understand type casting in a class hierarchy

E
Weekly Recap

Objectives &
• Understand the notions of upcast and downcast

T
Outline

Type Casting

P
Comparison
Built-in Type
Promotion &

N
Demotion
Unrelated Classes
Inheritance Hierarchy
Upcast
Downcast

Module Summary

Programming in Modern C++ Partha Pratim Das M26.3


Module Outline

Module M26

L
Partha Pratim
Das 1 Weekly Recap

E
Weekly Recap

Objectives & 2 Type Casting

T
Outline

Type Casting
Basic Notions

P
Comparison Comparison of Implicit and Explicit Casting
Built-in Type
Promotion & Built-in Type

N
Demotion
Unrelated Classes
Promotion & Demotion
Inheritance Hierarchy Unrelated Classes
Upcast
Downcast Inheritance Hierarchy
Module Summary
Upcast
Downcast

3 Module Summary

Programming in Modern C++ Partha Pratim Das M26.4


Type Casting

Module M26

L
Partha Pratim
Das

E
Weekly Recap

Objectives &

T
Outline

Type Casting

P
Comparison
Built-in Type
Promotion &

N
Demotion
Unrelated Classes
Inheritance Hierarchy
Upcast
Downcast

Module Summary
Type Casting

Programming in Modern C++ Partha Pratim Das M26.5


Type Casting: Basic Notions

Module M26 • Casting is performed when a value (variable) of one type is used in place of some other type.
Converting an expression of a given type into another type is known as type-casting

L
Partha Pratim
Das

int i = 3;

E
Weekly Recap
double d = 2.5;
Objectives &

T
Outline
double result = d / i; // i is cast to double and used
Type Casting

P
Comparison
Built-in Type • Casting can be implicit or explicit
Promotion &

N
Demotion
d = i; // implicit: int to double
Unrelated Classes
Inheritance Hierarchy
i = d; // implicit: warning: ’=’ : conversion from ’double’ to ’int’: possible loss of data
Upcast
d = (double)i; // explicit: int to double
Downcast i = (int)d; // explicit: double to int
Module Summary
• Casting Rules can be grossly classified for:
◦ Built-in types
◦ Unrelated types
◦ Inheritance hierarchy (static)
◦ Inheritance hierarchy (dynamic)
Programming in Modern C++ Partha Pratim Das M26.6
Comparison of Implicit and Explicit Casting

Module M26 Implicit Casting Explicit Casting

L
Partha Pratim
Das • Done automatically • Done programatically
• No data loss, for promotion • Data loss may or may not take place

E
Weekly Recap
Compiler will be silent Compiler will be silent
Objectives & • Possible data loss, for demotion

T
Outline
Compiler will issue warning
Type Casting
• No throwing of exception – is type safe • May throw for wrong type casting

P
Comparison
Built-in Type • Requires no special syntax • Requires cast operator for conversion
Promotion & C style operator: (< type >)

N
Demotion
Unrelated Classes C++ style operators:
Inheritance Hierarchy const cast,
Upcast
static cast,
Downcast
dynamic cast, and
Module Summary
reinterpret cast
• Avoid, if possible • Avoid C style cast
Use C++ style cast
• Possible only in static time • Possible in static as well as dynamic time
• May be disallowed for User-Defined Types, but • May be defined for User-Defined Types
cannot be disallowed for built-in types

Programming in Modern C++ Partha Pratim Das M26.7


Type Casting Rules: Built-in Type

Module M26 • Various type castings are possible between built-in types

L
Partha Pratim
Das int i = 3;
double d = 2.5;

E
Weekly Recap

Objectives &
double result = d / i; // i is cast to double and used

T
Outline

Type Casting • Casting rules are defined between numerical types, between numercial types and pointers, and

P
Comparison
Built-in Type
between pointers to different numerical types and void
Promotion &
• Casting can be implicit or explicit

N
Demotion
Unrelated Classes
Inheritance Hierarchy
int i = 3;
Upcast
double d = 2.5, *p = &d;
Downcast

Module Summary d = i; // implicit: int to double


i = d; // implicit: warning: ’=’ : conversion from ’double’ to ’int’: possible loss of data

d = (double)i; // explicit: int to double


i = (int)d; // explicit: double to int

i = p; // error: ’=’ : cannot convert from ’double *’ to ’int’


i = (int)p; // explicit: double * to int
Programming in Modern C++ Partha Pratim Das M26.8
Type Casting Rules: Built-in Type: Numerical Types

Module M26

• Casting is safe for promotion (All the data types of the variables are upgraded to the

L
Partha Pratim
Das
data type of the variable with larger data type)

E
Weekly Recap

Objectives &
bool → char → short int → int → unsigned int → long → unsigned →

T
Outline

Type Casting long long → float → double → long double

P
Comparison
Built-in Type
Promotion &
• Casting in built-in types does not invoke any conversion function. It only re-interprets

N
Demotion
Unrelated Classes
Inheritance Hierarchy
the binary representation
Upcast
Downcast
• Casting is unsafe for demotion – may lead to loss of data
Module Summary

Programming in Modern C++ Partha Pratim Das M26.9


Type Casting Rules: Built-in Type: Pointer Types

Module M26

• Implicit casting between different pointer types is not allowed

L
Partha Pratim
Das
• Any pointer can be implicitly cast to void* (with loss of type); but void* cannot be

E
Weekly Recap implicitly cast to any pointer type
Objectives &
• Conversion between array and corresponding pointer is not type casting – these are two

T
Outline

Type Casting different syntactic forms for accessing the same data

P
Comparison
Built-in Type
int i = 1, *p = &i, a[10]; double d = 1.1, *q = &d; void *r;
Promotion &

N
Demotion
Unrelated Classes q = p; // error: cannot convert ‘int*’ to ‘double*’
Inheritance Hierarchy p = q; // error: cannot convert ‘double*’ to ‘int*’
Upcast q = (double*)p; // Okay
Downcast p = (int*)q; // Okay
Module Summary
r = p; // Okay to convert from ‘int*’ to ‘void*’
p = r; // error: invalid conversion from ‘void*’ to ‘int*’
p = (int*)r; // Okay

p = a; // Okay by array pointer duality. p[i], a[i], *(p+i), *(a+i) are equivalent
a = p; // error: incompatible types in assignment of ‘int*’ to ‘int[10]’

Programming in Modern C++ Partha Pratim Das M26.10


Type Casting Rules: Built-in Type: Pointer Types

Module M26 • Implicit casting between pointer type and numerical type is not allowed
• However, explicit casting between pointer and integral type (int or long etc.) is a common

L
Partha Pratim
Das
practice to support various tasks like serialization (save a file) and de-serialization (open a file)
• Care should be taken with these explicit cast to ensure that the integral type is of the same

E
Weekly Recap

Objectives & size as of the pointer. That is: sizeof(void*) = sizeof(< integraltype >)

T
Outline

Type Casting int i, *p = 0; long j;

P
Comparison
Built-in Type
// sizeof(i) = sizeof(int) = 4
Promotion &
// sizeof(j) = sizeof(long) = 8

N
Demotion
Unrelated Classes // sizeof(p) = sizeof(int*) = sizeof(void*) = 8
Inheritance Hierarchy
Upcast i = p; // error: invalid conversion from ‘int*’ to ‘int’
Downcast p = i; // error: invalid conversion from ‘int’ to ‘int*’
Module Summary
i = (int)p; // error: cast from ‘int*’ to ‘int’ loses precision
p = (int*)i; // warning: cast to pointer from integer of different size

j = (long)p; // Okay
p = (int*)j; // Okay

• Here, the conversion should be done between int* and long and not between int* and int
Programming in Modern C++ Partha Pratim Das M26.11
Type Casting Rules: Unrelated Classes

Module M26
• (Implicit) Casting between unrelated classes is not permitted

L
Partha Pratim
Das
class A { int i; };

E
Weekly Recap class B { double d; };
Objectives &

T
Outline A a;
B b;
Type Casting

P
Comparison
Built-in Type
A *p = &a;
Promotion &
B *q = &b;

N
Demotion
Unrelated Classes a = b; // error: binary ’=’ : no operator which takes a right-hand operand of type ’B’
Inheritance Hierarchy a = (A)b; // error: ’type cast’ : cannot convert from ’B’ to ’A’
Upcast
Downcast
b = a; // error: binary ’=’ : no operator which takes a right-hand operand of type ’A’
Module Summary b = (B)a; // error: ’type cast’ : cannot convert from ’A’ to ’B’

p = q; // error: ’=’ : cannot convert from ’B *’ to ’A *’


q = p; // error: ’=’ : cannot convert from ’A *’ to ’B *’

p = (A*)&b; // explicit on pointer: type cast is okay for the compiler


q = (B*)&a; // explicit on pointer: type cast is okay for the compiler

Programming in Modern C++ Partha Pratim Das M26.12


Type Casting Rules: Unrelated Classes

Module M26
• Forced Casting between unrelated classes is dangerous

L
Partha Pratim
Das
class A { public: int i; };

E
Weekly Recap class B { public: double d; };
Objectives &

T
Outline A a;
B b;
Type Casting

P
Comparison
Built-in Type
a.i = 5;
Promotion &
b.d = 7.2;

N
Demotion
Unrelated Classes A *p = &a;
Inheritance Hierarchy B *q = &b;
Upcast
Downcast
cout << p->i << endl; // prints 5
Module Summary cout << q->d << endl; // prints 7.2

p = (A*)&b; // Forced casting on pointer: Dangerous


q = (B*)&a; // Forced casting on pointer: Dangerous

cout << p->i << endl; // prints -858993459: GARBAGE


cout << q->d << endl; // prints -9.25596e+061: GARBAGE

Programming in Modern C++ Partha Pratim Das M26.13


Type Casting Rules: Inheritance Hierarchy

Module M26
• Casting on a hierarchy is permitted in a limited sense

L
Partha Pratim
Das
class A { };

E
Weekly Recap class B : public A { };
Objectives &

T
Outline A *pa = 0;
B *pb = 0;
Type Casting
void *pv = 0;

P
Comparison
Built-in Type
Promotion &
pa = pb; // UPCAST: Okay

N
Demotion
Unrelated Classes pb = pa; // DOWNCAST: error: ’=’ : cannot convert from ’A *’ to ’B *’
Inheritance Hierarchy
Upcast
pv = pa; // Okay, but lose the type for A * to void *
Downcast
pv = pb; // Okay, but lose the type for B * to void *
Module Summary
pa = pv; // error: ’=’ : cannot convert from ’void *’ to ’A *’
pb = pv; // error: ’=’ : cannot convert from ’void *’ to ’B *’

Programming in Modern C++ Partha Pratim Das M26.14


Type Casting Rules: Inheritance Hierarchy

Module M26
• Up-Casting is safe

L
Partha Pratim
Das
class A { public: int dataA_; };

E
Weekly Recap class B : public A { public: int dataB_; };
Objectives &

T
Outline A a;
B b;
Type Casting

P
Comparison
Built-in Type
a.dataA_ = 2;
Promotion &
b.dataA_ = 3;

N
Demotion b.dataB_ = 5;
Unrelated Classes
Inheritance Hierarchy A *pa = &a;
Upcast
B *pb = &b;
Downcast

Module Summary cout << pa->dataA_ << endl; // prints 2


cout << pb->dataA_ << " " << pb->dataB_ << endl; // prints 3 5

pa = &b;

cout << pa->dataA_ << endl; // prints 3


cout << pa->dataB_ << endl; // error: ’dataB_’ : is not a member of ’A’

Programming in Modern C++ Partha Pratim Das M26.15


Type Casting Rules: Inheritance Hierarchy

Module M26
• Down-Casting is risky

L
Partha Pratim
Das
class A { public: int dataA_; };

E
Weekly Recap class B : public A { public: int dataB_; };
Objectives &

T
Outline A a;
B b;
Type Casting

P
Comparison
Built-in Type
a.dataA_ = 2;
Promotion &
b.dataA_ = 3;

N
Demotion b.dataB_ = 5;
Unrelated Classes
Inheritance Hierarchy B *pb = (B*)&a; // Forced downcast
Upcast
Downcast
cout << pb->dataA_ << endl; // prints 2
Module Summary cout << pb->dataB_ << endl; // Compilation okay. Prints garbage for ’dataB_’ -- no ’dataB_’ in ’A’ object

Programming in Modern C++ Partha Pratim Das M26.16


Module Summary

Module M26

• Introduced type casting

L
Partha Pratim
Das
• Understood the difference between implicit and explicit type casting

E
Weekly Recap

Objectives &
• Introduced the notions of Casting in a class hierarchy – upcast and downcast

T
Outline

Type Casting

P
Comparison
Built-in Type
Promotion &

N
Demotion
Unrelated Classes
Inheritance Hierarchy
Upcast
Downcast

Module Summary

Programming in Modern C++ Partha Pratim Das M26.17

You might also like