Programming with ANSI C ++

Slides:



Advertisements
Similar presentations
More on Classes Inheritance and Polymorphism
Advertisements

Polymorphism and Virtual Functions. Topics Polymorphism Virtual Functions Pure Virtual Functions Abstract Base Classes Virtual Destructors V-Tables Run.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 15: Polymorphism,
Chapter 5: Elementary Data Types Properties of types and objects –Data objects, variables and constants –Data types –Declarations –Type checking –Assignment.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Rossella Lau Lecture 8, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 8: Polymorphism & C++ pointer  Inheritance.
C++ Training Datascope Lawrence D’Antonio Lecture 1 Quiz 1.
. Plab – Tirgul 11 RTTI, Binary files. RTTI – why? Problem: u Up-casting works fine.  Treating sub-class as base class Shape * s = new Circle(); u What.
Run-time type information (RTTI) and casts Consider classes for components and windows: class Component {... virtual void draw() {} }; class Window: public.
Run Time Type Information, Binary files. RTTI – why? Problem: Up-casting works fine. –Treating sub-class as base class Shape * s = new Circle(); What.
Computer Science and Software Engineering University of Wisconsin - Platteville 7. Inheritance and Polymorphism Yan Shi CS/SE 2630 Lecture Notes.
Chapter 12: Adding Functionality to Your Classes.
Cpt S 122 – Data Structures Polymorphism
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Taken from slides of Starting Out with C++ Early Objects Seventh Edition.
Chapter 15 – Inheritance, Virtual Functions, and Polymorphism
Run-Time Type Identification Jim Fawcett CSE687 – Object Oriented Design Spring 2007.
Polymorphism &Virtual Functions
Department of Computer Science and Engineering, HKUST 1 HKUST Summer Programming Course 2008 Templates ~ their instantiation and specialization.
Copyright © Curt Hill Generic Classes Template Classes or Container Classes.
Polymorphism and Virtual Functions. Topics Polymorphism Virtual Functions Pure Virtual Functions Abstract Base Classes Virtual Destructors V-Tables Run.
CS212: Object Oriented Analysis and Design Lecture 17: Virtual Functions.
CS212: Object Oriented Analysis and Design Lecture 19: Exception Handling.
Inheritance and Composition Reusing the code and functionality Unit - 04.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Assignment 3 is due Sunday, the 8 th at 7pm. Problems with assn 3? Discuss at your team meeting tonight.
 Static  Example for Static Field  Example for Static Method  Math class methods  Casting  Scope of Declaration  Method Overloading  Constructor.
Recap Introduction to Inheritance Inheritance in C++ IS-A Relationship Polymorphism in Inheritance Classes in Inheritance Visibility Rules Constructor.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 20 - Virtual Functions Outline 20.1Introduction 20.2Type Fields and switch Statements 20.3Virtual.
CS 342: C++ Overloading Copyright © 2004 Dept. of Computer Science and Engineering, Washington University Overview of C++ Overloading Overloading occurs.
Polymorphism & Virtual Functions 1. Objectives 2  Polymorphism in C++  Pointers to derived classes  Important point on inheritance  Introduction to.
 Virtual Function Concepts: Abstract Classes & Pure Virtual Functions, Virtual Base classes, Friend functions, Static Functions, Assignment & copy initialization,
C++ How to Program, 7/e. © by Pearson Education, Inc. All Rights Reserved.2.
CSCI-383 Object-Oriented Programming & Design Lecture 17.
CSE 332: C++ Overloading Overview of C++ Overloading Overloading occurs when the same operator or function name is used with different signatures Both.
DEVRY COMP 220 iLab 7 Polymorphism Lab Report and Source Code Check this A+ tutorial guideline at
Programming with ANSI C ++
Prof. Bhushan Trivedi Director GLS Institute of Computer Technology
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Programming with ANSI C ++
Programming with ANSI C ++
Programming with ANSI C ++
Polymorphism, Virtual Methods and Abstract Classes
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
7. Inheritance and Polymorphism
Programming with ANSI C ++
Inheritance and Polymorphism
FUNCTIONS In C++.
Chapter 14 Templates C++ How to Program, 8/e
Polymorphism &Virtual Functions
Polymorphism.
Review: Two Programming Paradigms
Chapter 5 Classes.
Polymorphism & Virtual Functions
Templates.
Inheritance Often, software encapsulates multiple concepts for which some attributes/behaviors overlap E.g. A computer (role-playing) game has: Monsters:
Lecture 22 Inheritance Richard Gesick.
Virtual Functions Department of CSE, BUET Chapter 10.
Polymorphism Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition, by Kernighan.
Prof. Bhushan Trivedi Director GLS Institute of Computer Technology
Programming with ANSI C ++
Today’s Objectives 10-Jul-2006 Announcements Quiz #3
VIRTUAL FUNCTIONS RITIKA SHARMA.
Polymorphism 2019/4/29.
Object Slicing & Dynamic Casts
Binding 10: Binding Programming C# © 2003 DevelopMentor, Inc.
四時讀書樂 (春) ~ 翁森 山光照檻水繞廊,舞雩歸詠春風香。 好鳥枝頭亦朋友,落花水面皆文章。 蹉跎莫遣韶光老,人生唯有讀書好。
Lecture 6: Polymorphism
Static Binding Static binding chooses the function in the class of the base class pointer, ignoring any versions in the class of the object actually.
Computer Science II for Majors
Presentation transcript:

Programming with ANSI C ++ A Step-by-Step Approach Prof. Bhushan Trivedi Director GLS Institute of Computer Technology

Chapter 11 Run Time Type Information and casting operators

Introduction RTTI is a very powerful tool in C++ for finding out the type of an object at run time. RTTI also impacts the performance of the system to a notable extent. Compilers disable RTTI by default.

The addition in the standard ANSI C++ added two keywords to work with RTTI typeid returns the type of object in a specific form dynamic_cast is a new type of operator

Polymorphism typeinfo objects polymorphic objects possible to be pointed to by a base class pointer and can be manipulated at run time. Virtual function has limitations

The Hierarchy News SendNews(); StoreNews() //PrintNews() NewsWithPlainText NewsWithImages

The Solution RTTI requires polymorphic objects to operate. The <typeinfo> library contains the definition of type_info object. Automatically created and associated with every data type used For non-polymorphic data types, it is available even when RTTI is not enabled One type_info object for one type

The Typeinfo Object attributes Name() function Operator == Operator !=

The Hierarchy for next example Shape Rectangle Circle Dot

Applying typeid to Class Names and Objects Difference between typeid(NewsItem) and typeid (PlainTextNews),

Need for RTTI: case 1 and 2 Prior knowledge about usage of the class is not available to designer in some cases; and we may need to use RTTI the users of these classes are not involved in the design user needs operations other than designed Designer has no idea of all possible applications While using templates, we may need to know about the actual data type and provide validations

Need for RTTI : Case 3 and 4 RTTI enables the user to decide about the type of the object at run time and take decision at that point of time. We may need to do specific job for a specific subset of class hierarchy safely, then RTTI is handy

The problem with the solution typeid sovlves the problem but not completely We still need to modify code when a new class is added An idea solution does not need code modification

Solution The operator dynamic_cast, is a solution for the problem in the previous section This casting operator is used only for polymorphic object casting. That means it can cast from one polymorphic object to another polymorphic object only

Features of dynamic_cast It succeeds only when it is properly casted dynamic_cast <ToObjectPtrOrRef> (FromObjectPtrOrRef) It casts from FromObject to ToObject pointer or reference. The casting from Base pointer to Derived pointer can succeed only if base pointer is actually pointing to an object of Derived type

Dynamic Cast Summary Pointer to Content Casted to Successful? Base Yes Derived No (Not Possible)

Using dynamic_cast to replace typeid In some cases the code using dynamic_cast can be replaced by a code using typeid Typeid has the same performance overhead as the virtual function Performance of the result is dependent on the length of the inheritance chain

Const Cast It is special; no other cast, i.e. dynamic, static, or reinterpret cast can do the same The cast from const to non const can be done either with pointers or reference.

Two different types of const Real const, which is defined like  const int RealConstInt;  Contextual const <function header> (const int *NotActuallyConst) The variable assumed constness upon entry to the function.

Two different types of const Only contextual consts can be converted to non-const Real const variables like RealConstInt defined above cannot be converted to non-const

Static_cast Static_cast is a normal non-polymorphic cast Converting int to double, etc. should be done using this cast If applied to a polymorphic object, static_cast results in compile time error If conversion from a data type is not relevant, static_cast again results in compile time error Static cast is performed at compile time

reinterpret_cast If a programmer purposefully wants to convert to an irrelevant type; (like converting an integer value to a pointer) then reinterpret_cast provides a way to do it

Importance of RTTI for template objects In case of templates the type is determined at compile time and a specific function is instantiated that works for that type We can use typeid here to check the type of the item and then execute specific validation routine for that particular data type

Backward Compatibility with C and Casting It is important to note that the compatibility with C is a major concern to C++ designers C style casting is still being used in a large number of programs It is not recommended to use that in newer C++ programs though.

Efficiency Using RTTI is not efficient Even if we do not use typeid or dynamic_cast, when RTTI is enabled, the program runs slower

Cross Casting Cross casting refers to casting from derived to proper base class when there are multiple base classes in case of multiple-inheritance. Suppose we have the following class Derived : public Base1, public Base2; Base1 *Base1Ptr = new Derived; Base2 *Base2Ptr; Base2Ptr = Base1; //cross casting

Cross Casting   Base2 Sub Object Base1 Sub Object Remaining Derived

Cross Casting Base1Ptr pointer’s value is incremented to make it point to Base2 Sub Object of the derived class and then assigned to the Base2Ptr pointer Base2 *Base2Ptr= dynamic_cast<Base2 *> (Base1Ptr);  

Downcasting The cast from a base class pointer to a derived class pointer is downcasting It was considered unsafe and wrong programming before RTTI was introduced because we had to use C style casting