370 likes | 626 Views
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
E N D
Programming Languages and DesignLecture 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
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
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
Data Abstraction and Object-Oriented • Modules or Objects/Classes • Reduces conceptual load bymodularizing code • Contains faults to small parts of code • Makes program components more independent
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
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() }
Object-Oriented vs. ADT/Functional Style • Object-oriented programming • Extending a data structure is easy • Adding new code to an existing datastructure is hard • ADT/Functional/Imperative Style • Adding new code is easy • Extending a data structure is hard
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
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 anysubclass of C
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 aB object is expected • or C is more specific than B • The set of C instances is a subset ofthe set of B instances
p: B B *p = new C(); C Implementation of Subtyping • The layout of a C object includes a Bobject • If (virtual) methods are overridden,we need to select the appropriatemethod at run time – dynamic binding
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 */
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 statictypes of the arguments ((int,C)) • Run-time virtual method dispatch • Look up the code for the method signaturefoo(int,C) in the virtual function table
Implementation of Method Dispatch • Conceptually, an object contains alist of pointers to its methods • C++: the object contains a pointer tothe virtual function table • Java: an object pointer consists of apointer to the object and a pointer tothe dispatch table
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: Structures in Object-Oriented Programming
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
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
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; //??
struct S { private: …… }; struct S { …… }; class S { …… }; class S { public: …… }; Struct vs. Class
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 x D: y p: x = 42 q: x = 42 y = 3 Inheritance
C part q: x = 42 y = 3 Subtyping Class D : public C { …… }; C *q = new D(42, 3); A D object can be used where a C object is expected D *q = new C(42); is not allowed
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(); Virtual Methods
p: x = 42 print D :: print y = 3 x = 42 print y = 3 Virtual Methods (cont’) • Conceptually, • Actual implementation: p -> print() ==> ((*(p -> vtbl))[1])(p) C_D_vtable p: D :: print
Function Call vs. MessagePassing • 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].
Typed vs. Untyped • Statically typed (C++, Java, etc.) • no ‘message not understood’ errors atrun time • more efficient dispatch since layout ofdispatch table is known • Untyped (SmallTalk, CLOS, Cecil) • more flexibility • try to infer types for optimizing dispatch
Abstract Classes (C++) class A { public: virtual int foo () = 0; }; class C : public A { /* ... */ } • Defines type • Implementation is supplied insubclass
Interface Types (Java) interface I { int foo (); } class C implements I { /* ... */ } • Separation of interface andimplementation • Cleaner design of type hierarchies
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 partfrom multiple base classes • Single dispatch or multimethods • single dispatch: virtual functions • multiple dispatch: run-time overloading
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); };
p: x y C :: foo B_vptr z C_vptr C :: bar Implementation of MultipleInheritance • Concatenate all the pieces in layout • Multiple vtables per object (C++)
Implementation of MultipleInheritance (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)
A B C D Virtual Inheritance • Sharing of common part class A; class B : public virtual A; class C : public virtual A; class D : public B, public C; • Implementation: B and C partscontain pointers to common A part
Multimethods • Instead of run-time method selectionbased on receiver (virtual in C++) • Run-time method selection based onall arguments • Like overloading but with methodselection at run time • Allows different program structure • Languages: CLOS, Cecil, Brew
Multimethod Implementation • Generic functions dispatch using ann-dimensional table lookup • Example int foo (C); int foo (D); C p = new D(); int i = foo(p); // calls foo(D)
Structural Subtyping or RetroactiveAbstraction 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();
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