Object-Oriented Programming
• A design method as well as a programming paradigm
– For example, CRC cards, noun-verb parsing of requirements
• Hinges on inheritance-based polymorphism
– Classes define behaviors and structures of objects
– Subclasses may refine or extend base classes
– Extensions external to classes may be supported (e.g., C#)
• Distinct objects (class instances) interact
– The sets of objects (and behaviors) may vary dynamically
• OO paradigm introduces several key ideas
– Independent objects with separate state
– Encapsulate all but the most necessary details (e.g.,
public/private/protected access in C++)
– Abstraction, polymorphism, substitution, extensibility
CSE 425: Object-Oriented Programming I
Design Trade-Offs in OO Languages
• Classes must declare all information compiler needs
– E.g., sizes of by-value member variables, public interfaces
– E.g., definitions of any methods the compiler should inline
• Inlining copies method’s code to each place it’s called
– Saves performance overhead of a function call each time
– But requires more space for duplicate copies of instructions
• How classes interact with the type system
– Can exclude classes from type checking (not very safe)
– Can use classes as type constructors (as in C++)
– Can express entire type system as classes (as in Smalltalk)
• Controlled violations of encapsulation may be allowed
– E.g., C++ friend declaration to support symmetric operators
CSE 425: Object-Oriented Programming I
OO Language Example: Smalltalk
• A purely object-oriented language
– Classes, constants, etc. are all objects
• Powerful but somewhat elaborate execution model
– Send a message to a receiver, which dispatches a method
– Useful for event handling, simulation, similar applications
• Methods read or modify objects’ state
– I.e., accessor and mutator methods
• Method calls are performed using reference semantics
– Passes an alias to an object rather than copying its value
• An object may refer to itself in a method
• Objects are constructed via the class object
– Send the new message to the class type
CSE 425: Object-Oriented Programming I
OO Language Example: Java
• An almost purely object-oriented language
– E.g., integers are not objects, but can be wrapped with them
• Much more common execution model
– Call a method directly
– Preserves flexibility but with a more understandable style
• Methods again read or modify objects’ state
– I.e., accessor and mutator methods
• Method calls again use reference semantics
– Passes an alias to an object rather than copying its value
– But, Java may bind a call statically if possible for efficiency
• Classes declare object constructor methods
– Default constructors, constructor chaining are supported
CSE 425: Object-Oriented Programming I
OO Language Example: C++
• Classes declared, objects instantiated (similar to Java)
– Except, can instantiate an object directly on the stack as in
EvenIntegerPredicate eip; // default ctor
– Also can instantiate heap object via new (returns ptr) as in
TruePredicate * tpp = new TruePredicate;
• Object destructors managed differently for these cases
– Return from a function calls local stack object destructors automatically
– The program itself (or “smart pointer” it declares) must call delete directly
(delete tpp; or shared_ptr<TruePredicate> tppsp (tpp);)
– C++11 smart pointers implement reference counting and other object
lifetime management semantics
• C++ allows object methods to be defined (in .cpp files)
separately from their class declarations (in .h files)
– Also can define within declaration, which inlines the method
• Class methods are passed implicit this parameter (arity is +1)
CSE 425: Object-Oriented Programming I
Today’s Studio Exercises
• We’ll code up predicate classes for logic programming
– Developing a class hierarchy with abstract/concrete methods
– Please see slides for next studio (and/or read ahead in the
text book for more about abstract vs. concrete classes)
– E.g., C++ methods are polymorphic only if declared virtual
• Today’s exercises are all in C++
– Logic programs fit well within object-oriented features
– As always, please ask us for help as needed
• When done, e-mail your answers with subject line
“Object-Oriented Programming Studio I” to the
[email protected] course e-mail account
CSE 425: Object-Oriented Programming I
Descargar

Document