Module 26
Module 26
L
Partha Pratim
Das
Programming in Modern C++
E
Weekly Recap
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
Module M26
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
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
Module M26
L
Partha Pratim
Das 1 Weekly Recap
E
Weekly Recap
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
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
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
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
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 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
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
Module M26
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]’
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
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’
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
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 *’
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
pa = &b;
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
Module M26
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