Chapter 8 Typecasting and Inheritance 1 Type Casting

  • Slides: 17
Download presentation
Chapter 8 Typecasting and Inheritance 1

Chapter 8 Typecasting and Inheritance 1

Type Casting z. Allows flexible structures to be built z. Idea: Treat a derived

Type Casting z. Allows flexible structures to be built z. Idea: Treat a derived class object as if it were a base class object z. Syntax: This syntax cannot be used with pointers. Base. Class. Name (derived. Class. Object) or (Base. Class. Name) derived. Class. Object 2

Type Casting Displayable. Number* Show. In Show Value Number* Next 3

Type Casting Displayable. Number* Show. In Show Value Number* Next 3

Type Casting Using Pointers to an Object Text. Box display(Location(100, 100), Shape(75, 50)); Number*

Type Casting Using Pointers to an Object Text. Box display(Location(100, 100), Shape(75, 50)); Number* number = new Number(100); Displayable. Number* dnp; Number->Next(); Type Cast dnp = (Displayable. Number*) number; dnp->Show. In(display); dnp->Show(); We may only call methods from Displayable. Number on dnp, even though it’s really an object of type Number! 4

Type Casting Using References Text. Box display(Location(100, 100), Shape(75, 50)); Number number(100); number. Next();

Type Casting Using References Text. Box display(Location(100, 100), Shape(75, 50)); Number number(100); number. Next(); Displayable. Number& displayable = (Displayable. Number&) number; displayable. Show. In(display); displayable. Show(); 5

Type Casting Errors Displayable. Number* num. Ptr; Number *count 1 = new Number(100); Number

Type Casting Errors Displayable. Number* num. Ptr; Number *count 1 = new Number(100); Number count 2(200); num. Ptr = (Displayable. Number*) count 1; Displayable. Number& num. Ref = (Displayable. Number)count 2 num. Ptr->Next(); num. Ref. Next(); Displayable. Number doesn’t have a Next() method! 6

Why Type Casting? z. Allows us to treat a collection of objects uniformly by

Why Type Casting? z. Allows us to treat a collection of objects uniformly by viewing them as their base class. z. Example 1 : Shapes in a Graphical Editor z. Example 2 : Number. Panel class example 7

Shapes z. Derive Circle and Rectangle from “Shape” yavoid code duplication y. Shape. Manager

Shapes z. Derive Circle and Rectangle from “Shape” yavoid code duplication y. Shape. Manager handles instances of Shape y. E. g. : Location loc Rectangle bounds Set. Location Get. Bounds. . . Game. Circle Game. Rectangle Set. Center Set. Radius Draw. . . Set. Size Draw. . . 8

Type Casting to Create a Polymorphic Structure class Number. Panel { private: Displayable. Number

Type Casting to Create a Polymorphic Structure class Number. Panel { private: Displayable. Number *number[3]; // array for simplicity int last; Panel *panel; public: Number. Panel(); void Add( Displayable. Number* num ); // add num void Show. In(Panel & panel); // put all objects here void Show(); // Show all objects in panel }; 9

Implementation of the Number. Panel Class Number. Panel: : Number. Panel() { last =

Implementation of the Number. Panel Class Number. Panel: : Number. Panel() { last = -1; panel = (Panel *)0; } void Number. Panel: : Add( Displayable. Number* num) { if ( last < 2 ) { number[++last] = num; Text. Box* tbox = new Text. Box(Location(last*60, 20) Shape(50, 20)); num->Show. In(*tbox); if (panel) panel->Add(*tbox); } 10 }

Implementation of Number. Panel Class (Cont. ) void Number. Panel: : Show. In( Panel&

Implementation of Number. Panel Class (Cont. ) void Number. Panel: : Show. In( Panel& panel) { this->panel = &panel; } void Number. Panel: : Show() { if (last > -1 ) for(int i = 0; i <= last; i++) number[i]->Show(); } Number. Panel: : ~Number. Panel() {} 11

Example of Number. Panel Flexibility Frame display(Location(100, 100), Shape(200, 200)); Panel panel(display, Location(10, 10),

Example of Number. Panel Flexibility Frame display(Location(100, 100), Shape(200, 200)); Panel panel(display, Location(10, 10), Shape(180, 40)); Number. Panel number. Panel; Number number(100); Cycler octal(8); Jump. Counter length(0); number. Panel. Show. In(panel); // add different kinds of counters to the panel number. Panel. Add((Displayable. Number*)&number); number. Panel. Add((Displayable. Number*)&cycler); number. Panel. Add((Displayable. Number*)&octal); 12

Example of Number. Panel Flexibility (Continued) // manipulate individual counter objects number. Next(); octal.

Example of Number. Panel Flexibility (Continued) // manipulate individual counter objects number. Next(); octal. Next(); length. Next(50); // display all of the new values number. Panel. Show(); 13

Implicit vs. Explicit Type Casting Explicit Number *number; Displayable. Number *displayable =(Displayable. Number*)number Implicit

Implicit vs. Explicit Type Casting Explicit Number *number; Displayable. Number *displayable =(Displayable. Number*)number Implicit Number *number; Displayable. Number *displayable = number; Avoid implicit typecasting. Say what you mean in your code! 14

Implicit vs. Explicit Type Casting In Parameter Passing Given the following method: Number. Panel:

Implicit vs. Explicit Type Casting In Parameter Passing Given the following method: Number. Panel: : Add(Displayable. Number * dn); Call with Explicit Type Cast Number. Panel panel; Number *n = new Number(100); panel. Add((Displayable. Number*)n); Call with Implicit Type Cast Number. Panel panel; Number *c = new Number(100); panel. Add(c); Other developers may not realize that Add takes a Displayable. Number 15

Widening vs. Narrowing z. Widening y. Type casting from a derived class to a

Widening vs. Narrowing z. Widening y. Type casting from a derived class to a base class. y. Always Safe! Can be checked by the compiler. z. Narrowing y. Type casting from a base class to a derived class. y. Safety depends on the programmer. 16

Widening/Narrowing Example Displayable. Number *DNptr; Number *number = new Number(100); Cycler *cycler; DNptr =

Widening/Narrowing Example Displayable. Number *DNptr; Number *number = new Number(100); Cycler *cycler; DNptr = (Displayable. Number*)number; // safe; it widens cycler = (Cycler*)DNptr; cycler->Next(); // oops! // who knows what this will do! 17