340 likes | 475 Views
Chapter 11 and 12: Abstract Data Types and Encapsulation Constructs; Support for Object Orientation. Lesson 11. Object Orientation. Been using it for a while CS101 and 102: Java This class: C++ and Ruby. How it works. Abstract Data Types. Abstraction
E N D
Chapter 11 and 12: Abstract Data Types and Encapsulation Constructs; Support for Object Orientation Lesson 11
Object Orientation • Been using it for a while • CS101 and 102: Java • This class: C++ and Ruby How it works Chapts 11&12:ADTs, Encaps, and OO
Abstract Data Types • Abstraction • A view or representation of an entity that includes only the most significant attributes • Simplification • Ubiquitous in programming: even a “float” is an abstraction inti; float sum; sum = 0.0; for(i=0;i<10;i++){ sum += .1; } printf("%1.10f\n",sum); 1.0000001192 Chapts 11&12:ADTs, Encaps, and OO
Abstract Data Type • An Abstract Data Type is a user-defined data type that satisfies the following two conditions: • Both representation and operations are defined in a single syntactic unit • The representation is hidden Abstract Data Type Chapts 11&12:ADTs, Encaps, and OO
Examples • Most commonly ADTs defined in “class” definitions • E.g. C++, Java, C#, Ruby, SmallTalk, Eiffel • There are exceptions • E.g. Ada: defined in nestable packages Classes Chapts 11&12:ADTs, Encaps, and OO
An interesting issue… • C++ and Java • Some design paradigms call for message exchanges between objects, or it might just be nice for two classes to have knowledge of one another • In C++ one approach might be for each object to have a pointer to the other object Can you do this in C++? objB.hello() objA objB objA.helloYourSelf() Chapts 11&12:ADTs, Encaps, and OO
Parameterized Abstract Data Types • Parameterized ADTs allow storage of any data type • Example: a stack that can hold any type of data • Also known as generic classes • In C++ known as templates • Ruby is dynamically typed; therefore, its arrays, vectors, queues, stacks, etc. are parameterized Parameterized ADTs Chapts 11&12:ADTs, Encaps, and OO
Parameterized ADTs in C++ (continued) • The stack element type can be parameterized by making the class a templated class • Should remember from project 6 template <class dataType> class Stack{ public: Stack(); ~Stack(); void push(dataType data); dataType pop(); void clear_stack(); void print_stack(); intsize_of_stack() const; private: node<dataType> *pntrToHeadNode; }; Chapts 11&12:ADTs, Encaps, and OO
Parameterized Classes in Java 5.0 (and C#) • Called Generics • All collections are generics • Most common: LinkedList and ArrayList) • They can hold any descendent of “Object” class (i.e. pretty much any class) • 3 issues prior to Java 5.0 • Since interpreter didn’t know what data type was coming off a generic data structure had to cast • Couldn’t force the collection to support only one type • Couldn’t store primitives • Fixed first two: still can’t store primitives Generics in Java Chapts 11&12:ADTs, Encaps, and OO
Encapsulation Constructs • Large programming projects: • Need organization • Completely separate files containing related code • Name spaces (so many commands and vars already taken in libraries) • Partial compilation • Separate files compiled independently • Such collections are called encapsulation Encapsulation Chapts 11&12:ADTs, Encaps, and OO
Encapsulation in C • Files containing one or more subprograms can be independently compiled • The interface is defined in a header file • #include preprocessor specification – used to include header files in applications Should remember from Project 5: Modularizing your code Chapts 11&12:ADTs, Encaps, and OO
Encapsulation in C++ • Can define header and code files, similar to those of C • Or, classes can be used for encapsulation • The class is used as the interface (prototypes) • The member definitions are defined in a separate file • Friends provide a way to grant access to private members of a class • Can define another class or another external procedure as a friend Classes definitely a form of encapsulation (which is why they are in the same lesson) Chapts 11&12:ADTs, Encaps, and OO
Language Examples: C++ (continued) • Friend functions or classes - to provide access to private members to some unrelated units or functions • Necessary in C++ • Might list a binary tree as a friend class to the node class Node { private: int data; int key; // ... // class BinaryTree can now access data directly friend class BinaryTree; }; Chapts 11&12:ADTs, Encaps, and OO
Ada Packages • Ada packages can have multiple ADTs, and they can be given special access to one another (like friend classes) • Ada packages can be compiled separately • A package’s specification and body parts can be compiled separately Chapts 11&12:ADTs, Encaps, and OO
C# Assemblies • A collection of files • Used in the “assemble” of a dynamic link library (or even executable) Chapts 11&12:ADTs, Encaps, and OO
Naming Encapsulations • Large programs define many global names; need a way to divide into logical groupings • A naming encapsulation is used to create a new scope for names • C++ Namespaces • Can place each library in its own namespace and qualify names used outside with the namespace • C# also includes namespaces vector<Employee*>::iterator myIterator; std::cout << "print some text\n"; Chapts 11&12:ADTs, Encaps, and OO
Java Packages • Packages can contain more than one class definition; classes in a package are partial friends • Protected modifier allows fellow package members access • Clients of a package can use fully qualified name or use the import declaration Chapts 11&12:ADTs, Encaps, and OO
Ruby modules • Typically encapsulate collections of constants and methods • Access to the contents of a module is requested with the require method (or load; require can handle binary modules) Module MyStuff PI = 3.114159265 def MyStuff.mymethod1(p1) … end def MyStuff.mymethod2(p2) … end end require ‘myStuffMod’ … MyStuff.mymethod1(x) Chapts 11&12:ADTs, Encaps, and OO
Support for Object OrientationChapter 12 Chapts 11&12:ADTs, Encaps, and OO
OO Implementation Approaches • Exclusive use of objects (everything is an object) • Full support for all existing imperative language features and add OO to the language • Exclusive use of objects except for support for primitives All or nothing? Chapts 11&12:ADTs, Encaps, and OO
Messages • Calls to methods in objects are often called messages • Collection of methods called the message protocolor message interface objB.hello() objA objB objA.helloYourSelf() Chapts 11&12:ADTs, Encaps, and OO
Nested classes? • Have we seen? • Could have put node inside of the stack class • Instead of as a class defined in the H file Stack Node Chapts 11&12:ADTs, Encaps, and OO
Software reuse: drives need of OOP • A class is never quite perfect for the end user • Can reuse what want and add other needed parts through inheritance Inheritance Chapts 11&12:ADTs, Encaps, and OO
Inheritance: Subclasses vs. Subtypes • Stack might inherit from a LinkedList class • If all public aspects of the linked list class are accessible when using the stack class the stack class is a subtype • If want to hide some parent attributes or methods but make them available to children can declare as protected-derived class DerivedClass : protected BaseClass{ //can access public and protected portions //of Base from this class but users of this //class cannot }; class BaseClass{ public: … protected: … private: … }; Must reexport any public methods that still want from base Chapts 11&12:ADTs, Encaps, and OO
Multiple inheritance • Some languages allow • C++ does • SmallTalk, not so much • Adds complexity • If both parents have the same variable/method name, which inherit? • What if both bases were derived from a single parent and both overrode a method. Which to use? Base Class 1 Base Class 2 Derived Class class D: public A, public B{ … }; Chapts 11&12:ADTs, Encaps, and OO
Allocation and deallocation and polymorphism • Objects as local variables • Allocated on stack, right? • How much room must be allocated if want to support polymorphism? • i.e. if a BaseClass object is passed in as an argument, but the function must be able to deal with it or any of its DerivedClass objects • How fix? (remember project 7) Allocate from heap.Now can just allocate a pointer on the stack Chapts 11&12:ADTs, Encaps, and OO
More polymorphism • How do we tell the compiler that a user might want to override a base class method (i.e. dynamic binding)? • Virtual keyword • Why do you think we needed a virtual destructor, also? • If need dynamic binding there might be other differences between base and derived • Would want to run the derived’s destructor (i.e. virtual) Dynamic Binding Chapts 11&12:ADTs, Encaps, and OO
Data storage • Support for polymorphism • Class instance record (CIR: where allocated?) class B : public A{ intc,d; void draw(); void area(); void sift(); } class A{ inta,b; virtual void draw(); virtual void area(); } Chapts 11&12:ADTs, Encaps, and OO
Data storage (multiple inheritance) • Order matters • Which code executes: myC.fun(); ? class A{ int a; virtual void fun(); virtual void init(); } class C : public A, public B { int c; void fun(); void dud(); } class B { int b; virtual void sum(); } Chapts 11&12:ADTs, Encaps, and OO
Where is everything? • Could this work? #include <iostream> using namespace std; class Base{ public: static void initialize(){ cout << "look ma, no object\n"; } }; int main(intargc, char * argv[]){ Base *myBase; myBase = NULL; myBase->initialize(); return(0); } Chapts 11&12:ADTs, Encaps, and OO
Let’s see if we can guesse • Note the staticand the virtual class Base{ public: static void initialize(){ cout << "look ma, no object\n"; } virtual void virt(){ cout << "hi I am virtual\n"; } }; class DerivedClass : public Base{ public: void howdy(){ cout << "howdy\n"; } void virt(){ cout << "hi, I'm D's virtual\n"; } }; Chapts 11&12:ADTs, Encaps, and OO
What do you think the output will be? int main(intargc, char * argv[]){ Base *myBase; myBase = NULL; myBase->initialize(); DerivedClassmyD; printf("address of base initialize: %p\n",(void*)myBase->initialize); printf("address of derived initialize: %p\n",(void*)myD.initialize); printf("address of derived howdy: %p\n",(void*) &DerivedClass::howdy); printf("address of base virt: %p\n",(void*) &Base::virt); printf("address of derived virt: %p\n",(void*) &DerivedClass::virt); return(0); } Will base virt be at a different address than derived? How about base and derived initialize? Chapts 11&12:ADTs, Encaps, and OO
How a null object can access a method • Every class has a “sliver” of data stored in the “data” section of a program • It contains static variables including pointers to static methods • And it contains offsets to data and pointers to non-virtual methods (used when instantiating CIR’s) Stack Heap Class Static Methods Text Data Class Static Data Chapts 11&12:ADTs, Encaps, and OO
The end Chapts 11&12:ADTs, Encaps, and OO