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

Slides:



Advertisements
Similar presentations
Object-Oriented Programming Session 9 Course : T Programming Language Concept Year : February 2011.
Advertisements

MT311 (Oct 2007) Java Application Development Object-Oriented Programming Languages Tutorial 8.
Lecture 10: Part 1: OO Issues CS 540 George Mason University.
Abstract Data Types Data abstraction, or abstract data types, is a programming methodology where one defines not only the data structure to be used, but.
Chapter 12 Support for Object-Oriented Programming.
CPS 506 Comparative Programming Languages Abstract Data Type and Encapsulation.
CS2403 Programming Languages Abstract Data Types and Encapsulation Concepts Chung-Ta King Department of Computer Science National Tsing Hua University.
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
Abstract data types & object-oriented paradigm. Abstraction Abstraction: a view of an entity that includes only the attributes of significance in a particular.
Lecture 9 Concepts of Programming Languages
Abstract Data Types and Encapsulation Concepts
ISBN Chapter 12 Support for Object- Oriented Programming.
C++ fundamentals.
CS 355 – PROGRAMMING LANGUAGES Dr. X. Copyright © 2012 Addison-Wesley. All rights reserved.1-2 Chapter 12 Topics Introduction Object-Oriented Programming.
OOP Languages: Java vs C++
Chapter 11 Abstract Data Types and Encapsulation Concepts.
Programming Languages and Paradigms Object-Oriented Programming.
1 Chapter 11 Abstract Data Types and Encapsulation Constructs.
ISBN Chapter 12 Support for Object-Oriented Programming.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
OOPs Object oriented programming. Based on ADT principles  Representation of type and operations in a single unit  Available for other units to create.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
CS 403 – Programming Languages Class 25 November 28, 2000.
1 Chapter 10: Data Abstraction and Object Orientation Aaron Bloomfield CS 415 Fall 2005.
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
Introduction to Object Oriented Programming CMSC 331.
Learners Support Publications Classes and Objects.
Chapter 11 Abstract Data Types and Encapsulation Concepts.
Copyright © Curt Hill Generic Classes Template Classes or Container Classes.
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
C++ Programming Basic Learning Prepared By The Smartpath Information systems
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
Lecture 10 Concepts of Programming Languages Arne Kutzner Hanyang University / Seoul Korea.
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
Chapter 12 Support for Object-Oriented Programming.
Object-Oriented Programming Chapter Chapter
OOP using C Abstract data types How to accomplish the task??? Requirements Details Input, output, process Specify each task in terms of input.
(1) ICS 313: Programming Language Theory Chapter 12: Object Oriented Programming.
OOPs Object oriented programming. Abstract data types  Representationof type and operations in a single unit  Available for other units to create variables.
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
ISBN Object-Oriented Programming Chapter Chapter
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
1 Chapter 11 © 1998 by Addison Wesley Longman, Inc The Concept of Abstraction - The concept of abstraction is fundamental in programming - Nearly.
1 CS Programming Languages Class 22 November 14, 2000.
(1) ICS 313: Programming Language Theory Chapter 11: Abstract Data Types (Data Abstraction)
1 Copyright © 1998 by Addison Wesley Longman, Inc. Chapter 10 Abstraction - The concept of abstraction is fundamental in programming - Nearly all programming.
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
Structure A Data structure is a collection of variable which can be same or different types. You can refer to a structure as a single variable, and to.
CSC 243 – Java Programming, Spring, 2014 Week 4, Interfaces, Derived Classes, and Abstract Classes.
ISBN Chapter 12 Support for Object-Oriented Programming.
Chapter 12: Support for Object- Oriented Programming Lecture # 18.
Abstract Data Types and Encapsulation Concepts
Support for Object-Oriented Programming
11.1 The Concept of Abstraction
Types of Programming Languages
Lecture 9 Concepts of Programming Languages
Abstract Data Types and Encapsulation Concepts
Abstract Data Types and Encapsulation Concepts
Abstract Data Types and Encapsulation Concepts
Abstract Data Types and Encapsulation Concepts
Classes and Objects.
Support for Object-Oriented Programming
Support for Object-Oriented Programming
UNIT I OBJECT ORIENTED PROGRAMMING FUNDAMENTALS
Abstract Data Types and Encapsulation Concepts
Lecture 10 Concepts of Programming Languages
11.1 The Concept of Abstraction
Lecture 9 Concepts of Programming Languages
Chapter 11 Abstraction - The concept of abstraction is fundamental in
Presentation transcript:

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 9/8/2015Chapts 11&12:ADTs, Encaps, and OO2

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);

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

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

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()

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

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; };

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

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

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

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)

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; };

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

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

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";

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

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 = def MyStuff.mymethod1(p1) … end def MyStuff.mymethod2(p2) … end Module MyStuff PI = def MyStuff.mymethod1(p1) … end def MyStuff.mymethod2(p2) … end

9/8/2015Chapts 11&12:ADTs, Encaps, and OO19 Support for Object Orientation Chapter 12

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

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()

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

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

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 };

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{ … };

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

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

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(); }

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(); }

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); }

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"; } };

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?

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

The end 9/8/201534Chapts 11&12:ADTs, Encaps, and OO