1 / 34

Lesson 11

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

qamar
Download Presentation

Lesson 11

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 11 and 12: Abstract Data Types and Encapsulation Constructs; Support for Object Orientation Lesson 11

  2. 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

  3. 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

  4. 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

  5. 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

  6. 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

  7. 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

  8. 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

  9. 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

  10. 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

  11. 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

  12. 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

  13. 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

  14. 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

  15. 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

  16. 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

  17. 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

  18. 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

  19. Support for Object OrientationChapter 12 Chapts 11&12:ADTs, Encaps, and OO

  20. 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

  21. 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

  22. 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

  23. 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

  24. 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

  25. 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

  26. 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

  27. 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

  28. 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

  29. 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

  30. 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

  31. 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

  32. 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

  33. 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

  34. The end Chapts 11&12:ADTs, Encaps, and OO

More Related