chapter 11 and 12: abstract data types and encapsulation constructs; support for object orientation...

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

Upload: piers-walton

Post on 26-Dec-2015

226 views

Category:

Documents


1 download

TRANSCRIPT

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

Chapter 11 and 12: Abstract Data Types and Encapsulation Constructs; Support for Object

Orientation

Lesson 11

Page 2: 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

04/19/23Chapts 11&12:ADTs, Encaps, and OO2

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

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

04/19/23Chapts 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

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

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

04/19/23Chapts 11&12:ADTs, Encaps, and OO4

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

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

04/19/235 Chapts 11&12:ADTs, Encaps, and OO

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

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

04/19/23Chapts 11&12:ADTs, Encaps, and OO6

objA

objB

objB.hello()

objA.helloYourSelf()

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

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 parameterized04/19/23Chapts 11&12:ADTs, Encaps, and OO7

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

Parameterized ADTs in C++ (continued) The stack element type can be parameterized

by making the class a templated class Should remember from project 6

04/19/23Chapts 11&12:ADTs, Encaps, and OO8

template <class dataType> class Stack{ public: Stack(); ~Stack(); void push(dataType data); dataType pop(); void clear_stack(); void print_stack(); int size_of_stack() const; private: node<dataType> *pntrToHeadNode;};

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

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

primitives04/19/23Chapts 11&12:ADTs, Encaps, and OO9

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

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 encapsulation04/19/2310 Chapts 11&12:ADTs, Encaps, and OO

Page 11: Chapter 11 and 12: Abstract Data Types and Encapsulation Constructs; Support for Object Orientation Lesson 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

04/19/2311 Chapts 11&12:ADTs, Encaps, and OO

Should remember from Project 5: Modularizing your

code

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

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

04/19/2312 Chapts 11&12:ADTs, Encaps, and OO

Classes definitely a form of encapsulation (which is why they are in the same lesson)

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

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

04/19/2313 Chapts 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;};

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

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

04/19/2314 Chapts 11&12:ADTs, Encaps, and OO

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

C# Assemblies

A collection of files Used in the “assemble” of a

dynamic link library (or even executable)

04/19/23Chapts 11&12:ADTs, Encaps, and OO15

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

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

04/19/23Chapts 11&12:ADTs, Encaps, and OO16

vector<Employee*>::iterator myIterator;std::cout << "print some text\n";

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

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

04/19/23Chapts 11&12:ADTs, Encaps, and OO17

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

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)

04/19/23Chapts 11&12:ADTs, Encaps, and OO18

require ‘myStuffMod’…MyStuff.mymethod1(x)

Module MyStuff PI = 3.114159265 def MyStuff.mymethod1(p1) … end

def MyStuff.mymethod2(p2) … endend

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

04/19/23Chapts 11&12:ADTs, Encaps, and OO19

Support for Object OrientationChapter 12

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

OO Implementation Approaches1. 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

04/19/23Chapts 11&12:ADTs, Encaps, and OO20

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

Messages

Calls to methods in objects are often called messages

Collection of methods called the message protocol or message interface

04/19/23Chapts 11&12:ADTs, Encaps, and OO21

objA

objB

objB.hello()

objA.helloYourSelf()

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

Nested classes? Have we seen? Could have put node inside of the stack class

Instead of as a class defined in the H file

04/19/23Chapts 11&12:ADTs, Encaps, and OO22

Stack

NodeNode

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

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

04/19/2323 Chapts 11&12:ADTs, Encaps, and OO

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

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

04/19/23Chapts 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};

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

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?

04/19/23Chapts 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{ …};

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

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)

04/19/23Chapts 11&12:ADTs, Encaps, and OO26

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

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)

04/19/23Chapts 11&12:ADTs, Encaps, and OO27

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

Data storage

Support for polymorphism Class instance record (CIR: where allocated?)

04/19/23Chapts 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();}

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

Data storage (multiple inheritance) Order matters Which code executes: myC.fun(); ?

04/19/23Chapts 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();}

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

Where is everything? Could this work?

04/19/23Chapts 11&12:ADTs, Encaps, and OO30

#include <iostream>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 <iostream>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);}

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

Let’s see if we can guesse Note the static

and the virtual

04/19/23Chapts 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"; }};

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

What do you think the output will be?

04/19/23Chapts 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?

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

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)

04/19/23Chapts 11&12:ADTs, Encaps, and OO33

Class Static Data

Class Static

Methods

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

The end

04/19/2334 Chapts 11&12:ADTs, Encaps, and OO