1 challenge. 2 converter.java package prg; public class converter { private int value; public...
TRANSCRIPT
1
Challenge
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!
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); }}
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
5
Multiple Inheritance
Dispatching Virtual Base Classes Dispatching with Virtual Bases
(Our reference language is C++)
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
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
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.
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;
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
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
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.
};
13
Two Separate Objects from a Common Base Class
List::Node
DisplayedObject DiskPointer
DisplayedDiskPointer
List::Node
Classdiagram
DiskList
DisplayList
Objectdiagram
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 {
// ...};
15
The Standard C++ Stream I/O Library
ifstream
istream
ofstream
ostream
fstreambase
fstream
ios
iostream
File Streams
I/O Streamsvv vv
vv vv
16
Memory Layout of Virtual Base Class
D
BVClasses
An Object of Class D
virtual
D Data Members
B Data Members
V Data Members
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
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
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
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