1 challenge. 2 converter.java package prg; public class converter { private int value; public...

20
1 Challenge

Upload: caroline-williamson

Post on 03-Jan-2016

221 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: 1 Challenge. 2 Converter.java package prg; public class Converter { private int value; public Converter(int v) { value = v; } public int dollarToShekel()

1

Challenge

Page 2: 1 Challenge. 2 Converter.java package prg; public class Converter { private int value; public Converter(int v) { value = v; } public int dollarToShekel()

2

Converter.javapackage prg;

public class Converter { private int value; public Converter(int v) { value = v; }

public int dollarToShekel() { return value * 4; } public int hourToSec() { return value * 60; }}

Bug!

Page 3: 1 Challenge. 2 Converter.java package prg; public class Converter { private int value; public Converter(int v) { value = v; } public int dollarToShekel()

3

Tester.javapackage prg;import junit.framework.*;import junit.textui.TestRunner;

public class Tester extends TestCase { private Converter c; public void setUp() { c = new Converter(10); }

public void test1() { Assert.assertEquals(40, c.dollarToShekel()); } public void test2() { Assert.assertEquals(36000, c.hourToSec()); } public static void main(String[] args) { TestRunner.run(Tester.class); }}

Page 4: 1 Challenge. 2 Converter.java package prg; public class Converter { private int value; public Converter(int v) { value = v; } public int dollarToShekel()

4

Output..F

Time: 0.003

There was 1 failure:

1) test2(prg.Tester)junit.framework.AssertionFailedError: expected:<36000> but was:<600>

at prg.Tester.test2(Tester.java:13)

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)

at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)

at prg.Tester.main(Tester.java:16)

FAILURES!!!

Tests run: 2, Failures: 1, Errors: 0

Page 5: 1 Challenge. 2 Converter.java package prg; public class Converter { private int value; public Converter(int v) { value = v; } public int dollarToShekel()

5

Multiple Inheritance

Dispatching Virtual Base Classes Dispatching with Virtual Bases

(Our reference language is C++)

Page 6: 1 Challenge. 2 Converter.java package prg; public class Converter { private int value; public Converter(int v) { value = v; } public int dollarToShekel()

6

Memory Layout and Construction Order

D

B2B1

Classes

An Object of Class D

D Data Members

B2 Data Members

B1 Data Members

Construction order

this

Page 7: 1 Challenge. 2 Converter.java package prg; public class Converter { private int value; public Converter(int v) { value = v; } public int dollarToShekel()

7

VTBL and Multiple Inheritancestruct B1 {

virtual void f() { ... }};struct B2 { virtual void f() { ... }

virtual void g() { ... }};struct D: public B1, public B2 { void f() { ... }};

vptrB2 Part

vptrB1 Part

D Part

&B2::g (B2)

&D::f 0

&B2::g 0

&D::f (B2)

(B2)

VTBL for B1 and D

VTBL for B2

Page 8: 1 Challenge. 2 Converter.java package prg; public class Converter { private int value; public Converter(int v) { value = v; } public int dollarToShekel()

8

Extended VTBL Each VTBL entry includes:

Pointer to a function. Delta to add to the this pointer

C Code for implementing the virtual method call p->f().

vtbl_entry *vptr = *(vtbl_entry **)p;vptr += index(f);void *new_this = (char *)p + vptr->delta;(*vptr->func)(new_this);

Relatively simple, portable and quite efficient. Contradicts the C++ Spirit:

You pay the price of multiple inheritance even if it is not used at all.

Page 9: 1 Challenge. 2 Converter.java package prg; public class Converter { private int value; public Converter(int v) { value = v; } public int dollarToShekel()

9

Ordinary VTBL + Thunks

vptrB2 Part

vptrB1 Part

D Part

&D::f

&B2::g

this -=(B2);goto D::f;

(B2)

VTBL for B1 and D

VTBL for B2

this +=(B2);goto B2::g;

Page 10: 1 Challenge. 2 Converter.java package prg; public class Converter { private int value; public Converter(int v) { value = v; } public int dollarToShekel()

10

Diamond: same base class occurs in more than one ancestor.

Occurs in every large system. Must occur if the inheritance hierarchy has a common root.

Classical Example:

Diamond Inheritance

File

Input File Output File

Input Output File

Page 11: 1 Challenge. 2 Converter.java package prg; public class Converter { private int value; public Converter(int v) { value = v; } public int dollarToShekel()

11

Approaches to Repeated Inheritance Forbid the situation:

A diamond occurs naturally in many cases. Demand complicates the design

Single copy of common base class: Not always what you want.

Multiple copies of common base class: Not always what you want.

Either single or multiple copies of common base class. Eiffel’s approach: decision made by designer of D. C++’s approach: decision made by designer of B1 and B2.

B

B1 B2

D

Page 12: 1 Challenge. 2 Converter.java package prg; public class Converter { private int value; public Converter(int v) { value = v; } public int dollarToShekel()

12

Multiple Occurrences of a Base

class DiskPointer: public List::Node {// The link is used to maintain a linked // list of all Disk Pointers.

};

class DisplayedObject: public List::Node {// The link is used to maintain a linked // list of all DisplayedObjects.

};

class DisplayedDiskPointer: public DiskPointer,public DisplayedObject

{// This class is used for visualization of the// caching algorithm.

};

Page 13: 1 Challenge. 2 Converter.java package prg; public class Converter { private int value; public Converter(int v) { value = v; } public int dollarToShekel()

13

Two Separate Objects from a Common Base Class

List::Node

DisplayedObject DiskPointer

DisplayedDiskPointer

List::Node

Classdiagram

DiskList

DisplayList

Objectdiagram

Page 14: 1 Challenge. 2 Converter.java package prg; public class Converter { private int value; public Converter(int v) { value = v; } public int dollarToShekel()

14

Single Occurrence of a Common Base Class

The virtual keyword causes InitCheckedArray to contain a single subobject of class Array instead of two.

class Array { const int n; int *const data; //...};class CheckedArray: public virtual Array { //...};class InitArray: public virtual Array {

// ...};class InitCheckedArray: public CheckedArray, public InitArray {

// ...};

Page 15: 1 Challenge. 2 Converter.java package prg; public class Converter { private int value; public Converter(int v) { value = v; } public int dollarToShekel()

15

The Standard C++ Stream I/O Library

ifstream

istream

ofstream

ostream

fstreambase

fstream

ios

iostream

File Streams

I/O Streamsvv vv

vv vv

Page 16: 1 Challenge. 2 Converter.java package prg; public class Converter { private int value; public Converter(int v) { value = v; } public int dollarToShekel()

16

Memory Layout of Virtual Base Class

D

BVClasses

An Object of Class D

virtual

D Data Members

B Data Members

V Data Members

Page 17: 1 Challenge. 2 Converter.java package prg; public class Converter { private int value; public Converter(int v) { value = v; } public int dollarToShekel()

17

Memory Layout of a Diamond

** The virtual base is not always in the same location. **

virtual virtual

B2B1

V

D

a B2 object

a D object

B2 Data Members

V Data Members

D Data Members

B1 Data Members

B2 Data Members

V Data Members

Page 18: 1 Challenge. 2 Converter.java package prg; public class Converter { private int value; public Converter(int v) { value = v; } public int dollarToShekel()

18

Virtual Bases and Casting There is no “back pointer” from a sub-object

corresponding to a virtual base class to the enclosing object.

The sub object of the virtual base class will be in different locations, depending on the dynamic type of the whole object.

You only pay for what you buy Therefore, we cannot downcast from a virtual base

Unless we use RTTI

Page 19: 1 Challenge. 2 Converter.java package prg; public class Converter { private int value; public Converter(int v) { value = v; } public int dollarToShekel()

19

Down-casting from a Virtual Base struct X { }; struct Y : virtual X { };

X* x = new Y();

Y* y1= (Y*) x; // Compiler error

Y* y2 = (Y*) ((void*) x);

// Runs, // but yields wrong result

Y* y3 = dynamic_cast<Y*>(x); // OK

Page 20: 1 Challenge. 2 Converter.java package prg; public class Converter { private int value; public Converter(int v) { value = v; } public int dollarToShekel()

20

Determining Ambiguity?

R* r = new R(); r->m();

Examine the object graph of R (not its class graph): If R has two (or more) definitions for m() such that no

one overrides the other, then r->m() is ambiguous

If these two definitions have a common ancestor that defines m(), then even new R() is ambiguous