DOC PREVIEW
LSU CSC 4101 - Data Abstraction and OO

This preview shows page 1-2-3 out of 9 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 9 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 9 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 9 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 9 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

CSC 4101: Programming Languages 11Data Abstraction and OOData Abstraction and OOTextbook: Chapter 9 (incl. CD material)2Data Abstraction and OOData Abstraction and OOz Modules or Objects/Classesz Reduces conceptual load by modularizing codez Contains faults to small parts of codez Makes program components more independent3Why OO?Why OO?z 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 codeCSC 4101: Programming Languages 24Programming StyleProgramming Stylez C++class C { … public: virtual int foo() { … }}class D :public C { virtual int foo() { … }}class E :public C { virtual int foo() { … }}z Cint 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()}5OO vs. ADT/Functional StyleOO vs. ADT/Functional Stylez Object-oriented programming– Extending a data structure is easy– Adding new code to an existing data structure is hardz ADT/Functional/Imperative Style– Adding new code is easy– Extending a data structure is hard6ObjectObject--Oriented ProgrammingOriented Programmingz User-defined types• Classesz Encapsulation• Private fieldsz Subtype polymorphism• Inheritance• Structural subtypingz Code reuse• InheritanceCSC 4101: Programming Languages 37What is a Type?What is a Type?z Built-in types: int, char, etc.z Class typesz Interface types (in Java)z 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 C8SubtypingSubtypingz C is a subtype of B (C <: B)z or a C object isa B objectz or a C object can be used wherever a B object is expectedz or C is more specific than Bz The set of C instances is a subset of the set of B instances9Implementation of SubtypingImplementation of Subtypingz The layout of a C object includes a B object p B * p = new C();z If (virtual) methods are overridden, we need to select the appropriate method at run timeBCCSC 4101: Programming Languages 410Selection of Virtual MethodsSelection of Virtual Methodsclass 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 */11Method Selection AlgorithmMethod Selection Algorithmz Method callC * p = new D();p->foo(42, p)z 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))z Run-time virtual method dispatch– Look up the code for the method signature foo(int,C) in the virtual function table12Implementation of Method DispatchImplementation of Method Dispatchz Conceptually: an object contains a list of pointers to its methodsz C++: the object contains a pointer to the virtual function tablez Java: an object pointer consists of a pointer to the object and a pointer to the dispatch tableCSC 4101: Programming Languages 513Other Design IssuesOther Design Issuesz Single or multiple inheritance– C++: class C : public A, public B {};– Java: class C extends B { }z C++-style virtual inheritance– class C : public virtual A { };– allows objects to share a common part from multiple base classes14Other Design IssuesOther Design Issuesz Single dispatch or multimethods– single dispatch: virtual functions– multiple dispatch: run-time overloadingz Function call or message passing– C++/Java: o.foo()– Smalltalk: o fooz Typed or untypedz Interface types vs. abstract classes15Multiple InheritanceMultiple Inheritanceclass 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);};CSC 4101: Programming Languages 616Implementation of Multiple Implementation of Multiple InheritanceInheritancez Concatenate all the pieces in layoutz Multiple vtables per object (C++)XyB_vptrzC_vptrC::fooC::bar17Implementation of Multiple Implementation of Multiple InheritanceInheritancez Adjust `this’ pointerB * p = new C;int i = p->foo(42);z is translated toint i = ((p->B_vptr)[1].fn)(p+(p->B_vptr)[1].delta, 42)18Virtual InheritanceVirtual Inheritancez Sharing of common partclass A;class B : public virtual A; Aclass C : public virtual A; B Cclass D : public B, public C; Dz Implementation: B and C parts contain pointers to common A partCSC 4101: Programming Languages 719MultimethodsMultimethodsz Instead of run-time method selection based on receiver (virtual in C++)z Run-time method selection based on all argumentsz Like overloading but with method selection at run timez Allows different program structurez Languages: CLOS, Cecil, Brew20Multimethod ImplementationMultimethod Implementationz Generic functions dispatch using an n-dimensional table lookupz Exampleint foo (C);int foo (D);C p = new D();int i = foo(p); // calls foo(D)21Function Call vs. Message Function Call vs. Message PassingPassingz Function call syntax (C++)p.foo(5);p->foo(5);z Message passing syntax (SmallTalk)p foo 5.6 * 7.someArray at: 1 put: 5.x = 0 ifTrue: [y <- 1]ifFalse: [y <- 2].CSC 4101: Programming Languages 822Typed vs. UntypedTyped vs. Untypedz Statically typed (C++, Java, etc.)– no `message not understood’ errors at run time– more efficient dispatch since layout of dispatch table is knownz Untyped (SmallTalk, CLOS, Cecil)– more flexibility– try to infer types for optimizing dispatch23Abstract ClassesAbstract Classesclass A {public:virtual int foo () = 0;};class C : public A { /* ... */ }z Defines typez Implementation is supplied in subclass24Interface TypesInterface Typesinterface I {int foo ();}class C implements I { /* ... */ }z Separation of interface and implementationz Cleaner design of type hierarchiesCSC 4101: Programming Languages 925Structural Structural SubtypingSubtypingor Retroactive or Retroactive AbstractionAbstractionclass C { public int foo () { ... } }interface I {int foo ();}z Structural SubtypingI p = new C();z Retroactive Abstractionclass C implements I;I p = new C();26Signatures in G++Signatures in G++signature I {int foo ();};class C { public: int foo (); };I * p = new C;z Implemented in G++ Versions 2.6-2.8z Use `-fhandle-signatures’


View Full Document

LSU CSC 4101 - Data Abstraction and OO

Download Data Abstraction and OO
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Data Abstraction and OO and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Data Abstraction and OO 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?