* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Download Object-Oriented Programming - Department Of Computer Science
Functional programming wikipedia , lookup
Reactive programming wikipedia , lookup
Structured programming wikipedia , lookup
Virtual synchrony wikipedia , lookup
Join-pattern wikipedia , lookup
Object-relational impedance mismatch wikipedia , lookup
Go (programming language) wikipedia , lookup
Objective-C wikipedia , lookup
Falcon (programming language) wikipedia , lookup
Design Patterns wikipedia , lookup
Abstraction (computer science) wikipedia , lookup
Class (computer programming) wikipedia , lookup
Name mangling wikipedia , lookup
C Sharp syntax wikipedia , lookup
Programming Languages and
Design
Lecture 5
Object-Oriented Programming
Instructor: Li Ma
Department of Computer Science
Texas Southern University, Houston
February, 2008
Review of Previous Lectures
Introduction to programming languages
Syntax specifications of programming languages
Semantic specifications of programming
languages
Functional Programming
Properties
Lambda calculus
Scheme
2
Today’s Lecture
Object-Oriented Programming
Abstract Data Type
Objects and classes
Inheritance, polymorphism
Virtual methods, dynamic binding
References
“Programming Language Pragmatics”, Michael Scott, Chapter 9
“Concepts of Programming Languages”, John C. Mitchell,
Chapter 10, 12, & 13
“Foundations of Programming Languages: Design and
Implementation”, S. H. Roosta, Chapter 8 & 9
3
Features of Object-Oriented
Programming
Data Abstraction
“What you can do?” and “How you can do it?” are independent
Encapsulation
Information hiding, protect from unauthorized access
Polymorphism
Overloading, or same function body but different argument types
Inheritance
Reuse of code
Dynamic Binding
At run time instead of compile time, a message is attached to a
method
4
Data Abstraction and Object-Oriented
Modules or Objects/Classes
Reduces conceptual load by
modularizing code
Contains faults to small parts of code
Makes program components more independent
5
Why Object-Oriented?
Groups data with code
Data is isolated in private object fields
Methods operating on the data are bundled with the
data in classes
Other code can’t mess with the data
Classes can be easily added or removed from the
code
6
Programming Style
C++
class C { … public: virtual int foo() { … }}
class D :public C { virtual int foo() { … }}
class E :public C { virtual int foo() { … }}
C
int foo(C * obj) {
if (obj is a D)
... // code from D::foo()
else if (obj is an E)
... // code from E::foo()
else
... // code from C::foo()
}
7
Object-Oriented vs. ADT/Functional
Style
Object-oriented programming
Extending a data structure is easy
Adding new code to an existing data
structure is hard
ADT/Functional/Imperative Style
Adding new code is easy
Extending a data structure is hard
8
Implementation of Object-Oriented
Programming
User-defined types
Classes
Encapsulation
Private fields – member variables
Subtype polymorphism
Inheritance – subclass
Structural subtyping – override virtual function
Code reuse
Inheritance – subclass
9
What is a Type?
Built-in types
int, char, etc.
Programmer-defined types
Classes
Interface types (in Java)
Think of a type as a set of values
int = -2^31 .. 2^31-1
C = set of instances of class C or any
subclass of C
10
Subtyping
C is a subtype of B (C <: B)
or a C object is a B object
or a C object can be used wherever a
B object is expected
or C is more specific than B
The set of C instances is a subset of
the set of B instances
11
Implementation of Subtyping
The layout of a C object includes a B
object
p:
B
C
B *p = new C();
If (virtual) methods are overridden,
we need to select the appropriate
method at run time – dynamic binding
12
Selection of Virtual Methods
class B {
public int foo () { return 0; }
}
class C extends B {
public int foo () { return 1; }
}
B obj = new C();
int i = obj.foo (); /* executes C.foo */
13
Method Selection Algorithm
Method call
C * p = new D();
p->foo(42, p)
Compile-time overload resolution
Find the receiver’s (p’s) static type (C)
Inside C find an appropriate method for the static
types of the arguments ((int,C))
Run-time virtual method dispatch
Look up the code for the method signature
foo(int,C) in the virtual function table
14
Implementation of Method Dispatch
Conceptually, an object contains a
list of pointers to its methods
C++: the object contains a pointer to
the virtual function table
Java: an object pointer consists of a
pointer to the object and a pointer to
the dispatch table
15
Structures in Object-Oriented
Programming
struct S {
int x;
float y;
};
S a;
a.x = 42;
a.y = 3.14;
S *p = &a;
S *q = new S;
int i = p -> x; // i is 42
int j = q -> x; // j has no value
a:
x = 42
y = 3.14
p:
q:
16
Methods
struct S {
int x;
float y;
int getX() { return x; }
};
S *p = new S;
p -> x = 42;
p -> y = 3.14;
int i = p -> getX(); // i is 42
17
Constructors
struct S {
int x;
float y;
S(int a, float b) { x = a; y = b; }
int getX() { return x; }
};
S *p = new S(42, 3.14);
int i = p -> getX(); // i is 42
18
Visibility Specifiers
struct S {
private:
int x;
float y;
public:
S(int a, float b) { x = a; y = b; }
int getX() { return x; }
};
S *p = new S(42, 3.14);
int i = p -> getX(); // i is 42
Int j = p -> x; //??
19
Struct vs. Class
struct S {
private:
……
};
class S {
……
};
struct S {
……
};
class S {
public:
……
};
20
Inheritance
Class C {
int x;
public:
C(int a) { x = a; }
int getX() { return x; }
};
Class D : public C {
int y;
public:
D(int a, int b) : C(a)
{ y = b; }
int getY() { return y; }
};
C *p = new C(42);
D *q = new D(42, 3);
C:
x
D:
x
y
p:
x = 42
q:
x = 42
y=3
21
Subtyping
Class D : public C {
……
};
C *q = new D(42, 3);
q:
x = 42
C part
y=3
A D object can be used where a C object is expected
D *q = new C(42); is not allowed
22
Virtual Methods
Class C {
protected:
int x;
public:
C(int a) { x = a; }
virtual void print () {
cout << x;
}
};
Class D : public C {
int y;
public:
D(int a, int b) : C(a) { y = b; }
virtual void print () {
cout << ‘(‘ << x << “, “ << y << ‘)’;
}
};
C *p = new D(42, 3);
p -> print();
23
Virtual Methods (cont’)
Conceptually,
p:
x = 42
print
y=3
D :: print
Actual implementation:
p:
p -> print()
x = 42
print
y=3
C_D_vtable
D :: print
==> ((*(p -> vtbl))[1])(p)
24
Function Call vs. Message
Passing
Function call syntax (C++)
p.foo(5);
p->foo(5);
Message passing syntax (SmallTalk)
p foo 5.
6 * 7.
someArray at: 1 put: 5.
x = 0 if True: [y <- 1]
If False: [y <- 2].
25
Typed vs. Untyped
Statically typed (C++, Java, etc.)
no ‘message not understood’ errors at
run time
more efficient dispatch since layout of
dispatch table is known
Untyped (SmallTalk, CLOS, Cecil)
more flexibility
try to infer types for optimizing dispatch
26
Abstract Classes (C++)
class A {
public:
virtual int foo () = 0;
};
class C : public A { /* ... */ }
Defines type
Implementation is supplied in
subclass
27
Interface Types (Java)
interface I {
int foo ();
}
class C implements I { /* ... */ }
Separation of interface and
implementation
Cleaner design of type hierarchies
28
Other Design Issues
Single or multiple inheritance
C++: class C : public A, public B {};
Java: class C extends B { }
C++-style virtual inheritance
class C : public virtual A { };
allows objects to share a common part
from multiple base classes
Single dispatch or multimethods
single dispatch: virtual functions
multiple dispatch: run-time overloading
29
Multiple Inheritance
class A { int x; };
class B {
int y;
public:
virtual int foo (int);
};
class C : public A, public B {
int z;
public:
virtual int foo (int);
virtual void bar (int);
};
30
Implementation of Multiple
Inheritance
Concatenate all the pieces in layout
Multiple vtables per object (C++)
p:
x
y
B_vptr
z
C_vptr
C :: foo
C :: bar
31
Implementation of Multiple
Inheritance (cont’)
Adjust `this’ pointer
B *p = new C;
int i = p->foo(42);
is translated to
int i = ((p->B_vptr)[1].fn)
(p+(p->B_vptr)[1].delta, 42)
32
Virtual Inheritance
Sharing of common part
class A;
class B : public virtual A;
class C : public virtual A;
class D : public B, public C;
A
B
C
D
Implementation: B and C parts
contain pointers to common A part
33
Multimethods
Instead of run-time method selection
based on receiver (virtual in C++)
Run-time method selection based on
all arguments
Like overloading but with method
selection at run time
Allows different program structure
Languages: CLOS, Cecil, Brew
34
Multimethod Implementation
Generic functions dispatch using an
n-dimensional table lookup
Example
int foo (C);
int foo (D);
C p = new D();
int i = foo(p); // calls foo(D)
35
Structural Subtyping or Retroactive
Abstraction
class C { public int foo () { ... } }
interface I {
int foo ();
}
Structural Subtyping
I p = new C();
Retroactive Abstraction
class C implements I;
I p = new C();
36
Signatures in G++
signature I {
int foo ();
};
class C { public: int foo (); };
I * p = new C;
Implemented in G++ Versions 2.6-2.8
Use ‘-fhandle -signatures’ option
37