Download presentation
Presentation is loading. Please wait.
Published byPiers Walton Modified over 9 years ago
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 9/8/2015Chapts 11&12:ADTs, Encaps, and OO2
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 9/8/2015Chapts 11&12:ADTs, Encaps, and OO3 int i; float sum; sum = 0.0; for(i=0;i<10;i++){ sum +=.1; } printf("%1.10f\n",sum); int i; float sum; sum = 0.0; for(i=0;i<10;i++){ sum +=.1; } printf("%1.10f\n",sum); 1.0000001192
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 9/8/2015Chapts 11&12:ADTs, Encaps, and OO4
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 9/8/20155Chapts 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 9/8/2015Chapts 11&12:ADTs, Encaps, and OO6 objAobjB objB.hello() objA.helloYourSelf()
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 9/8/2015Chapts 11&12:ADTs, Encaps, and OO7
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 9/8/2015Chapts 11&12:ADTs, Encaps, and OO8 template class Stack{ public: Stack(); ~Stack(); void push(dataType data); dataType pop(); void clear_stack(); void print_stack(); int size_of_stack() const; private: node *pntrToHeadNode; }; template class Stack{ public: Stack(); ~Stack(); void push(dataType data); dataType pop(); void clear_stack(); void print_stack(); int size_of_stack() const; private: node *pntrToHeadNode; };
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 9/8/2015Chapts 11&12:ADTs, Encaps, and OO9
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 9/8/201510Chapts 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 9/8/201511Chapts 11&12:ADTs, Encaps, and OO Should remember from Project 5: Modularizing your code
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 9/8/201512Chapts 11&12:ADTs, Encaps, and OO Classes definitely a form of encapsulation (which is why they are in the same lesson)
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 9/8/201513Chapts 11&12:ADTs, Encaps, and OO class Node { private: int data; int key; //... // class BinaryTree can now access data directly friend class BinaryTree; }; class Node { private: int data; int key; //... // class BinaryTree can now access data directly friend class BinaryTree; };
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 9/8/201514Chapts 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) 9/8/2015Chapts 11&12:ADTs, Encaps, and OO15
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 9/8/2015Chapts 11&12:ADTs, Encaps, and OO16 vector ::iterator myIterator; std::cout << "print some text\n"; vector ::iterator myIterator; std::cout << "print some text\n";
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 9/8/2015Chapts 11&12:ADTs, Encaps, and OO17
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) 9/8/2015Chapts 11&12:ADTs, Encaps, and OO18 require ‘myStuffMod’ … MyStuff.mymethod1(x) require ‘myStuffMod’ … MyStuff.mymethod1(x) Module MyStuff PI = 3.114159265 def MyStuff.mymethod1(p1) … end def MyStuff.mymethod2(p2) … end Module MyStuff PI = 3.114159265 def MyStuff.mymethod1(p1) … end def MyStuff.mymethod2(p2) … end
19
9/8/2015Chapts 11&12:ADTs, Encaps, and OO19 Support for Object Orientation Chapter 12
20
OO Implementation Approaches 1. Exclusive use of objects (everything is an object) 2. Full support for all existing imperative language features and add OO to the language 3. Exclusive use of objects except for support for primitives 9/8/2015Chapts 11&12:ADTs, Encaps, and OO20
21
Messages Calls to methods in objects are often called messages Collection of methods called the message protocol or message interface 9/8/2015Chapts 11&12:ADTs, Encaps, and OO21 objAobjB objB.hello() objA.helloYourSelf()
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 9/8/2015Chapts 11&12:ADTs, Encaps, and OO22 Stack Node
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 9/8/201523Chapts 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 9/8/2015Chapts 11&12:ADTs, Encaps, and OO24 class BaseClass{ public: … protected: … private: … }; class BaseClass{ public: … protected: … private: … }; class DerivedClass : protected BaseClass{ //can access public and protected portions //of Base from this class but users of this //class cannot }; class DerivedClass : protected BaseClass{ //can access public and protected portions //of Base from this class but users of this //class cannot };
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? 9/8/2015Chapts 11&12:ADTs, Encaps, and OO25 Base Class 1 Base Class 2 Derived Class class D: public A, public B{ … }; class D: public A, public B{ … };
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) 9/8/2015Chapts 11&12:ADTs, Encaps, and OO26
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) 9/8/2015Chapts 11&12:ADTs, Encaps, and OO27
28
Data storage Support for polymorphism Class instance record (CIR: where allocated?) 9/8/2015Chapts 11&12:ADTs, Encaps, and OO28 class A{ int a,b; virtual void draw(); virtual void area(); } class A{ int a,b; virtual void draw(); virtual void area(); } class B : public A{ int c,d; void draw(); void area(); void sift(); } class B : public A{ int c,d; void draw(); void area(); void sift(); }
29
Data storage (multiple inheritance) Order matters Which code executes: myC.fun(); ? 9/8/2015Chapts 11&12:ADTs, Encaps, and OO29 class A{ int a; virtual void fun(); virtual void init(); } class A{ int a; virtual void fun(); virtual void init(); } class B { int b; virtual void sum(); } class B { int b; virtual void sum(); } class C : public A, public B { int c; void fun(); void dud(); } class C : public A, public B { int c; void fun(); void dud(); }
30
Where is everything? Could this work? 9/8/2015Chapts 11&12:ADTs, Encaps, and OO30 #include using namespace std; class Base{ public: static void initialize(){ cout << "look ma, no object\n"; } }; int main(int argc, char * argv[]){ Base *myBase; myBase = NULL; myBase->initialize(); return(0); } #include using namespace std; class Base{ public: static void initialize(){ cout << "look ma, no object\n"; } }; int main(int argc, char * argv[]){ Base *myBase; myBase = NULL; myBase->initialize(); return(0); }
31
Let’s see if we can guesse Note the static and the virtual 9/8/2015Chapts 11&12:ADTs, Encaps, and OO31 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"; } }; 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"; } };
32
What do you think the output will be? 9/8/2015Chapts 11&12:ADTs, Encaps, and OO32 int main(int argc, char * argv[]){ Base *myBase; myBase = NULL; myBase->initialize(); DerivedClass myD; 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); } int main(int argc, char * argv[]){ Base *myBase; myBase = NULL; myBase->initialize(); DerivedClass myD; 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? Will base virt be at a different address than derived? How about base and derived initialize?
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) 9/8/2015Chapts 11&12:ADTs, Encaps, and OO33 Class Static Data Class Static Methods
34
The end 9/8/201534Chapts 11&12:ADTs, Encaps, and OO
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.