Implementation of OO Languages - PowerPoint PPT Presentation

1 / 9
About This Presentation
Title:

Implementation of OO Languages

Description:

Implementation of OO Languages Efficient use of instructions and program storage E.g., a C++ object is stored as a struct of member variables and inherited variables ... – PowerPoint PPT presentation

Number of Views:58
Avg rating:3.0/5.0
Slides: 10
Provided by: wus73
Category:

less

Transcript and Presenter's Notes

Title: Implementation of OO Languages


1
Implementation of OO Languages
  • Efficient use of instructions and program storage
  • E.g., a C object is stored as a struct of
    member variables and inherited variables are
    augmented with additional ones
  • Methods as functions with an extra this argument
    for object
  • Inheritance, dynamic binding raise additional
    issues
  • E.g., use of C virtual function table (v-tbl)
    to dispatch calls
  • Language features influence object lifetimes
  • E.g., C stack objects lifetimes are
    automatically scoped to the duration of a
    function call (created and destroyed with it)
  • Can exploit this to manage heap objects as in the
    common resource allocation is initialization
    (RAII) coding idiom tie the lifetime of a heap
    object to that of a stack object (e.g., a smart
    pointer) which is in turn tied to the lifetime of
    a function

2
Further Data Abstraction Ideas in C
  • Class constructors/destructors have special
    properties
  • Destructors/destructors run in opposite orders
    (next slides)
  • Constructors can use base/member class
    initialization, e.g., class Baz public Foo int
    j Baz () Foo(0), j(0)
  • Static binding by default, use virtual to make
    dynamic
  • Pure virtual methods (declare with 0) make class
    abstract
  • Multiple inheritance is allowed (e.g., mix-ins
    design style)
  • C also has interface polymorphism via templates
  • Including parameterized/subtype polymorphism
  • Highly efficient if used correctly, but, watch
    out for by-value return semantics (pop,
    destructor, etc. need to be efficient)
  • Can improve significantly on purely
    object-oriented approaches, e.g., containers
    (Scott examples 9.8 vs. 9.45)

3
C Class and Member Construction Order
  • class A
  • public
  • A(int i) m_i(i)
  • cout ltlt "A ltlt endl
  • A() coutltlt"A"ltltendl
  • private
  • int m_i
  • class B public A
  • public
  • B(int i, int j)
  • A(i), m_j(j)
  • cout ltlt B ltlt endl
  • B() cout ltlt B ltlt endl
  • private
  • int m_j
  • int main (int, char )
  • B b(2,3)
  • In the main function, the B constructor is called
    on object b
  • Passes in integer values 2 and 3
  • B constructor calls A constructor
  • passes value 2 to A constructor via base/member
    initialization list
  • A constructor initializes m_i
  • with the passed value 2
  • Body of A constructor runs
  • Outputs A
  • B constructor initializes m_j
  • with passed value 3
  • Body of B constructor runs
  • outputs B

4
C Class and Member Destruction Order
  • class A
  • public
  • A(int i) m_i(i)
  • cout ltlt "A ltlt endl
  • A() coutltlt"A"ltltendl
  • private
  • int m_i
  • class B public A
  • public
  • B(int i, int j) A(i), m_j(j)
  • cout ltlt B ltlt endl
  • B() cout ltlt B ltlt endl
  • private
  • int m_j
  • int main (int, char )
  • B b(2,3)
  • return 0
  • B destructor called on object b in main
  • Body of B destructor runs
  • outputs B
  • B destructor calls destructor of m_j
  • int is a built-in type, so its a no-op
  • B destructor calls A destructor
  • Body of A destructor runs
  • outputs A
  • A destructor calls destructor of m_i
  • again a no-op
  • Compare orders of construction and destruction of
    base, members, body
  • at the level of each class, order of steps is
    reversed in constructor vs. destructor
  • ctor base class, members, body
  • dtor body, members, base class

5
C Virtual Functions
  • class A
  • public
  • A () coutltlt" A"
  • virtual A () coutltlt" A"
  • virtual f(int)
  • class B public A
  • public
  • B () A() coutltlt" B"
  • virtual B() coutltlt" B"
  • virtual f(int) override //C11
  • int main (int, char )
  • // prints "A B"
  • A ap new B
  • // prints "B A" would only
  • Used to support polymorphism with pointers and
    references
  • Declared virtual in a base class
  • Can overridde in derived class
  • Overriding only happens when signatures are the
    same
  • Otherwise it just overloads the function or
    operator name
  • More about overloading next lecture
  • Ensures derived class function definition is
    resolved dynamically
  • E.g., that destructors farther down the hierarchy
    get called
  • Use final (C11) to prevent overriding of a
    virtual method
  • Use override (C11) in derived class to ensure
    that the signatures match (error if not)

6
C Virtual Functions, Continued
  • class A
  • public
  • void x() coutltlt"Ax"
  • virtual void y() coutltlt"Ay"
  • class B public A
  • public
  • void x() coutltlt"Bx"
  • virtual void y() coutltlt"By"
  • int main ()
  • B b
  • A ap b B bp b
  • b.x () // prints "Bx"
  • b.y () // prints "By"
  • bp-gtx () // prints "Bx"
  • bp-gty () // prints "By"
  • Only matter with pointer or reference
  • Calls on object itself resolved statically
  • E.g., b.y()
  • Look first at pointer/reference type
  • If non-virtual there, resolve statically
  • E.g., ap-gtx()
  • If virtual there, resolve dynamically
  • E.g., ap-gty()
  • Note that virtual keyword need not be repeated in
    derived classes
  • But its good style to do so
  • Caller can force static resolution of a virtual
    function via scope operator
  • E.g., ap-gtAy() prints Ay

7
Pure Virtual Functions in C
  • class A
  • public
  • virtual void x() 0
  • virtual void y() 0
  • class B public A
  • public
  • virtual void x()
  • class C public B
  • public
  • virtual void y()
  • int main ()
  • A ap new C
  • ap-gtx ()
  • A is an abstract (base) class
  • Similar to an interface in Java
  • Declares pure virtual functions (0)
  • May also have non-virtual methods, as well as
    virtual methods that are not pure virtual
  • Derived classes override pure virtual methods
    (and thus become concrete)
  • B overrides x(), C overrides y()
  • Cant instantiate an abstract class
  • class that declares pure virtual functions
  • or inherits ones that are not overridden
  • A and B are abstract, can create a C
  • Can still have a pointer or reference to an
    abstract class type
  • Useful for polymorphism

8
Design with Pure Virtual Functions
  • Pure virtual functions let us specify interfaces
    appropriately
  • But let us defer implementation decisions until
    later (subclasses)
  • As the type hierarchy is extended, pure virtual
    functions are replaced
  • By virtual functions that fill in (and may
    override) the implementation details
  • Key idea refinement

Animal
move()0
Fish
Mammal
move()
move()
swim()
walk()
Bird
Penguin
Sparrow
move()
move()
waddle()
walk()
swim()
fly()
9
Todays Studio Exercises
  • Well keep working with classes (for logic
    programming applications)
  • Developing clauses, checking for type
    compatibility
  • Todays exercises are again all in C
  • Please re-use your code from the previous studio
    session
  • As always, please ask us for help as needed
  • When done, send e-mail with Object-Oriented
    Programming Studio II in the subject line, to
    cse425_at_seas.wustl.edu
Write a Comment
User Comments (0)
About PowerShow.com