Inheritance and Polymorphism in OOP: Study of CSE 452 (Fall 2008) - Prof. Laura Dillon, Assignments of Programming Languages

An in-depth exploration of inheritance and polymorphism in object-oriented programming (oop), as presented in l. Dillon's cse 452 course during the fall 2008 semester. The execution of a call in c, dynamic binding, and modelling single and multiple inheritance. It also discusses the need for a 'view' of each parent in an heir and the adaptation of parents in an inheritance hierarchy.

Typology: Assignments

Pre 2010

Uploaded on 07/28/2009

koofers-user-2oz-3
koofers-user-2oz-3 🇺🇸

5

(1)

10 documents

1 / 7

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
L. Dillon, CSE 452, Fall 2008 1
Binding is the process by which
A routine invocation is attached to code that is executed
An attribute reference is connected a memory location
Binding can be done
At compile time — static binding, using static linking
At program load timestatic binding, using dynamic
linking
At run time — dynamic binding
Binding
L. Dillon, CSE 452, Fall 2008 2
In the .h (header) file:
extern int foo (arg c);
extern float my_variable;
In the .c file:
int foo (arg c);
float my_variable;
Static binding in C
L. Dillon, CSE 452, Fall 2008 3
On the caller side:
Push the actual parameters on the stack.
Push the return address on the stack
Jump to the code for the callee
Address to jump to is “filled out” during linking
On callee side:
Push space for local variables and initialize, as needed
Execute the statements in the routine’s body
Read the return address from the stack
Pop local variables, return address and actual params
Jump to return address
Execution of a call in C
L. Dillon, CSE 452, Fall 2008 4
pf3
pf4
pf5

Partial preview of the text

Download Inheritance and Polymorphism in OOP: Study of CSE 452 (Fall 2008) - Prof. Laura Dillon and more Assignments Programming Languages in PDF only on Docsity!

L. Dillon, CSE 452, Fall 2008

1

Binding

is the process by which

A routine invocation is attached to code that is executed

An attribute reference is connected a memory location

Binding can be done•

At compile time —

static binding, using static linking

At program load time

static binding, using dynamic

At run time — linking

dynamic binding

Binding

L. Dillon, CSE 452, Fall 2008

In the .h (header) file:

extern

int

foo

(arg

c);

extern

float

my_variable;

In the .c file:

int

foo

(arg

c);

float

my_variable;

Static binding in C

L. Dillon, CSE 452, Fall 2008

3

On the caller side:

Push the actual parameters on the stack.

Push the return address on the stack

Jump to the code for the callee

Address to jump to is “filled out” during linking

On callee side:

Push space for local variables and initialize, as needed

Execute the statements in the routine’s body

Read the return address from the stack

Pop local variables, return address and actual params

Jump to return address

Execution of a call in C

L. Dillon, CSE 452, Fall 2008

L. Dillon, CSE 452, Fall 2008

5

Identifiers used in program are relative to an object Dynamic binding is used in OO

In

a

, attribute

a

belongs to

Current

In

foo(...)

, routine

foo

belongs to

Current

In

obj.foo(...)

, routine

foo

belongs to target object

obj

Routines may be redefined in subclasses

Dynamic binding and OO

L. Dillon, CSE 452, Fall 2008

Objects and class tables

class

A

create

set

feature

a:

INTEGER

set(n:

INTEGER)

is

do

a

n

end

get_a

INTEGER

is

do

Result

a

end

f(n:

INTEGER):

like

Current

is

do

create

Result.set(n

a)

end

Current

A class table

INT field

return first field of Currentstore n in first field of Current

call Result.set(n+a):initialize the new A object’s class table ptr & fields (to default vals);allocate space for a new A object, and bind it to Result;

push return address & jump to first routine in Current’s class tableread n; add first field of Current; push sum onto stack (for callee’s n)push Result onto stack (for callee’s Current)

return r

L. Dillon, CSE 452, Fall 2008

7

Modelling single inheritance

class

B

inherit

A

redefine

set

end

create

set

feature

b:

INTEGER

set(n:

INTEGER)

is

do

a

n;

b

n

end

get_b:

INTEGER

is

do

Result

b

end

end

B^ A

A class table

INT field

B class table

INT fieldINT field

B’s get_bB’s fB’s set A’s fA’s get_aA’s set

L. Dillon, CSE 452, Fall 2008 class

A

create

set

feature

a: INTEGER

set(n:

INTEGER)

is

do

a := n end

f(n:

INTEGER):

like

Current

is

do

create

Result.set(n

a) end

get_a

INTEGER

is

do

Result

a end

end

A

class

B

inherit

A

redefine

set

end

create

set

feature

b: INTEGER

set(n:

INTEGER)

is

do

a := n; b := n end

get_b:

INTEGER

is

do

Result

b end

end

B

class

MAIN

create

make

feature

make

is

local

a: A

b1,

b2:

B

do

create

b1.set(1)

a :=

b1.f

b ?=

a

end

end

MAIN

L. Dillon, CSE 452, Fall 2008

13

Modelling multiple inheritance

Need a “view” of each parent in an heir:

C

A

B

a

b

c

f

ctp: g a:

ctp: b:

g:^ B’s table f: A’s table

B’s gcode forA’s f code for

h

x:

C

C-as-A table h: f:

C-as-B table g:

B’s gcode forA’s f code for

ctp: a:

ctp: c:b:

C’s h code for

x:

...

Fx.g: (^) push

(^) x (^) onto stack (for callee’s

(^) Current

F

(^) push return address & jump to code for

(^) g (^) (found at

(^) ((x+

Code for

B

’s (^) g (^) expects:

F

b (^) at (^) Current+

F

code for

(^) g (^) at

**Current

size 4) (assuming ptrs & ints of

L. Dillon, CSE 452, Fall 2008

Modelling multiple inheritance

Need a “view” of each parent in an heir:

C

A

B

a

b

c

f

ctp: g a:

ctp: b:

g: B’s table f: A’s table

B’s gcode forA’s f code for

h

x:

C

C-as-A table h: f:

C-as-B table g:

B’s gcode forA’s f code for

ctp: a:

ctp: c:b:

C’s h code for

x:

...

F x.g: push

(^) x+

(^) onto stack (for callee’s

(^) Current

F

push return address & jump to code for

(^) g (^) (found at

(^) ((x+

Code for

B

’s (^) g (^) expects:

F

(^) b (^) at (^) Current+

F

(^) code for

(^) g (^) at

**Current

size 4) (assuming ptrs & ints of

L. Dillon, CSE 452, Fall 2008

15

Modelling multiple inheritance

Need a “view” of each parent in an heir:

C

A

B

a

b

c

f

ctp: g a:

ctp: b:

g:^ B’s table f: A’s table

B’s gcode forA’s f code for

h

x:

C

C-as-A table h: f:

C-as-B table g:

B’s gcode forA’s f code for

ctp: a:

ctp: c:b:

C’s h code for

x:

...

y := x :

F

(^) “normal” assignment

y:

A

z:

B

y:

...

L. Dillon, CSE 452, Fall 2008

Modelling multiple inheritance

Need a “view” of each parent in an heir:

C

A

B

a

b

c

f

ctp: g a:

ctp: b:

g: B’s table f: A’s table

B’s gcode forA’s f code for

h

x:

C

C-as-A table h: f:

C-as-B table g:

B’s gcode forA’s f code for

ctp: a:

ctp: c:b:

C’s h code for

x:

...

y:

A

z:

B

z := x :

F

store

x+

(^) into location bound to

(^) z

z:

...

Modelling adaptation of parent(s)

C

A

B

a

b

c

f

hg

redefine

h

rename

f as

k

ctp: a:

ctp: b:

h: g: B’s table f: A’s table

B’s hcode forB’s gcode forA’s f code for

Modelling adaptation of parent(s)

C

A

B

a

b

c

f

hg

redefine

h

rename

f as k

ctp: a:

ctp: b:

h: g: B’s table f: A’s table

B’s hcode forB’s gcode forA’s f code for

Adapt views of parent(s) in an heir:

x:

C

h: 8 g: 0 C-as-B table h: 0 k: 0 C-as-A table

B’s gcode forA’s f code for

ctp: a:

ctp: c:b:

x:

...

C’s h code for

Modelling adaptation of parent(s)

C

A

B

a

b

c

f

hg

redefine

h

rename

f as

k

ctp: a:

ctp: b:

h: g: B’s table f: A’s table

B’s hcode forB’s gcode forA’s f code for

Adapt views of parent(s) in an heir:

x:

C

h: 8 g: 0 C-as-B table h: 0 k: 0 C-as-A table

B’s gcode forA’s f code for

ctp: a:

ctp: c:b:

x:

...

C’s h code for

F x.k: (^) push

(^) x (^) onto stack (for callee’s

(^) Current

F

(^) push return address & jump to code for

(^) k (^) (code for A’s f, found at

(x)

obvious amounts.)address calculation by theconstants used in thepresence affects thebe explained later; theirthese “displacements” willing slides; the purpose ofhere and in the remain-address calculationsthe tables in theignoring the numbers in (For simplicity, I am

Modelling adaptation of parent(s)

C

A

B

a

b

c

f

hg

redefine

h

rename

f as k

ctp: a:

ctp: b:

h: g: B’s table f: A’s table

B’s hcode forB’s gcode forA’s f code for

x:

C

y:

A

Adapt views of parent(s) in an heir:

h: 8 g: 0 C-as-B table h: 0 k: 0 C-as-A table

B’s gcode forA’s f code for

ctp: a:

ctp: c:b:

x:

...

C’s h code for

y:

...

F y.f: push

(^) y (^) onto stack (for callee’s

(^) Current

F

push return address & jump to code for

(^) f (^) (found at

(^) (y)

y := x

F

(^) “normal” assignment

L. Dillon, CSE 452, Fall 2008

25

class

A

feature a: INTEGER

set(n:

INTEGER)

is

do

a :=

n end

f(n:

INTEGER):

like

Current

is

do

create

Result

Result.set(n

a)

end

get

INTEGER

is

do

Result

a end

end

A

class

B

feature b: INTEGER

set(n:

INTEGER)

is

do

b := n end

get:

INTEGER

is

do

Result

b end

end

B

class

C

inherit

A

rename set

as

set1,

get

as

get

end

B

rename set

as

set

redefine

get

end

feature c:

INTEGER

get

INTEGER

is

do

Result

c

b end

g is

do c := a

b end

end

C

class

MAIN

create

make

feature make

is

local a: A; b: B;

c:C

do

create

c

c.set1(1);

c.set2(2);

c.g

a := c; b:=

c;

end

end

MAIN

L. Dillon, CSE 452, Fall 2008

Exercise

a.a;the ellipses?What results are returned by the following invocations atclass (feature) tables.ellipses (.. .) in execution of Main. Include drawings of theDraw the stack and the heap when control reaches the

a.get

b.b;

b.get

c.a;

c.b;

c.c;

c.get1;

c.get