Lecture 9 Concepts of Programming Languages Arne Kutzner

  • Slides: 23
Download presentation
Lecture 9 Concepts of Programming Languages Arne Kutzner Hanyang University / Seoul Korea

Lecture 9 Concepts of Programming Languages Arne Kutzner Hanyang University / Seoul Korea

Topics • • • The Concept of Abstraction Introduction to Data Abstraction Design Issues

Topics • • • The Concept of Abstraction Introduction to Data Abstraction Design Issues for Abstract Data Types Language Examples Parameterized Abstract Data Types Naming Encapsulations Concepts of Programming Languages 2

The Concept of Abstraction • An abstraction is a view or representation of an

The Concept of Abstraction • An abstraction is a view or representation of an entity that includes only the most significant attributes / aspects • Two forms of abstraction: – process abstraction with subprograms – data abstraction Concepts of Programming Languages 3

Introduction to Data Abstraction • An abstract data type is a user-defined data type

Introduction to Data Abstraction • An abstract data type is a user-defined data type that satisfies the following two conditions: – The representation of, and operations on, instances (objects) of the type are defined in a single syntactic unit – The representation of instances (objects) of the type is hidden from the program units that use these objects • Possible operations are provided in the type's definition Concepts of Programming Languages 4

Advantages of Data Abstraction • Advantages of the first condition – Program organization, modifiability

Advantages of Data Abstraction • Advantages of the first condition – Program organization, modifiability (everything associated with a data structure is together) – Separate compilation • Advantage the secondition – Reliability by hiding the data representations (User code cannot directly access objects of the type or depend on the representation, allowing the representation to be changed without affecting user code Concepts of Programming Languages 5

Language Examples: C++ • Based on C struct type. The class is the encapsulation

Language Examples: C++ • Based on C struct type. The class is the encapsulation device • All of the class instances of a class share a single implementation of the member functions • Each instance of a class has its own copy of the class data members • Instances can be static, stack dynamic, or heap dynamic Concepts of Programming Languages 6

Language Examples: C++ (continued) • Information Hiding / Encapsulation – Private clause for hidden

Language Examples: C++ (continued) • Information Hiding / Encapsulation – Private clause for hidden entities – Public clause for interface entities – Protected clause in the context inheritance Concepts of Programming Languages 7

Language Examples: C++ (continued) • Constructors: – Functions to initialize the data members of

Language Examples: C++ (continued) • Constructors: – Functions to initialize the data members of instances (they do not create the objects) – Can include parameters to provide parameterization of the objects – Implicitly called when an instance is created – Name is the same as the class name Concepts of Programming Languages 8

Language Examples: C++ (continued) • Destructors – Functions to cleanup after an instance is

Language Examples: C++ (continued) • Destructors – Functions to cleanup after an instance is destroyed; usually just to reclaim heap storage – Implicitly called when the object’s lifetime ends – Name is the class name, preceded by a tilde (~) Concepts of Programming Languages 9

An Example in C++ class stack { private: int *stack. Ptr, max. Len, top.

An Example in C++ class stack { private: int *stack. Ptr, max. Len, top. Ptr; public: stack() { stack. Ptr = new int [100]; max. Len = 99; top. Ptr = -1; }; ~stack () {delete [] stack. Ptr; }; void push (int num) {…}; void pop () {…}; int top () {…}; int empty () {…}; } Concepts of Programming Languages 10

Language Examples: C++ (continued) • Friend functions or classes - to provide access to

Language Examples: C++ (continued) • Friend functions or classes - to provide access to private members to some unrelated units or functions – Interesting e. g. in the context of operator overloading Concepts of Programming Languages 11

Language Examples: Java • Similar to C++, except: – All user-defined types are classes

Language Examples: Java • Similar to C++, except: – All user-defined types are classes – All objects are allocated from the heap and accessed through reference variables – Individual entities in classes have access control modifiers (private or public), rather than clauses – Java has a second scoping mechanism, package scope, which has similar effects like friends in C++ Concepts of Programming Languages 12

An Example in Java class Stack. Class { private int [] stack. Ref; private

An Example in Java class Stack. Class { private int [] stack. Ref; private int [] max. Len, top. Index; public Stack. Class() { stack. Ref = new int [100]; max. Len = 99; top. Ptr = -1; }; public void push (int num) {…}; public void pop () {…}; public int top () {…}; public boolean empty () {…}; } Concepts of Programming Languages 13

Language Examples: C# • Based on C++ and Java • Adds two access modifiers,

Language Examples: C# • Based on C++ and Java • Adds two access modifiers, internal and protected internal • All class instances are heap dynamic • Default constructors are available for all classes • Garbage collection is used for most heap objects, so destructors are rarely used • structs are lightweight classes that do not support inheritance Concepts of Programming Languages 14

Language Examples: C# (continued) • Accessor methods (getter and setter) – C# provides properties

Language Examples: C# (continued) • Accessor methods (getter and setter) – C# provides properties as a way of implementing getters and setters without requiring explicit method calls Concepts of Programming Languages 15

C# Property Example public class Weather { public int Degree. Days { get {return

C# Property Example public class Weather { public int Degree. Days { get {return degree. Days; } set { if(value < 0 || value > 30) Console. Write. Line( "Value is out of range: {0}", value); else degree. Days = value; } } private int degree. Days; . . . }. . . Weather w = new Weather(); int degree. Days. Today, old. Degree. Days; . . . w. Degree. Days = degree. Days. Today; . . . old. Degree. Days = w. Degree. Days; Concepts of Programming Languages 16

Parameterized Abstract Data Types • Parameterized ADTs allow designing an ADT that can store

Parameterized Abstract Data Types • Parameterized ADTs allow designing an ADT that can store any type elements (among other things) • Also known as generic classes • C++, Ada, Java since Edition 5. 0, and C# since 2005 provide support for parameterized ADTs Concepts of Programming Languages 17

Parameterized ADTs in C++ • The stack element type can be parameterized by making

Parameterized ADTs in C++ • The stack element type can be parameterized by making the class a templated class template <class Type> class stack { private: Type *stack. Ptr; const int max. Len; int top. Ptr; public: stack() { stack. Ptr = new Type[100]; max. Len = 99; top. Ptr = -1; } … Concepts of Programming Languages } 18

Parameterized Classes in Java 5. 0 • Generic parameters must be classes • Most

Parameterized Classes in Java 5. 0 • Generic parameters must be classes • Most common generic types are the collection types, such as Linked. List and Array. List • Eliminate the need to cast objects that are removed • Eliminate the problem of having multiple types in a structure Concepts of Programming Languages 19

Parameterized Classes in C# 2005 • Similar to those of Java 5. 0 •

Parameterized Classes in C# 2005 • Similar to those of Java 5. 0 • Elements of parameterized structures can be accessed through indexing Concepts of Programming Languages 20

Encapsulation in C • The interface is placed in a header file – The

Encapsulation in C • The interface is placed in a header file – The header contains only a prototype (abstraction), the implementation is hidden • The visibility of static variables (variables with modifier static) of global scope is restricted to the source file, where they are defined Concepts of Programming Languages 21

Naming Encapsulations • A naming encapsulation is used to create a scope for names

Naming Encapsulations • A naming encapsulation is used to create a scope for names • Popular with C++, C#, XML • Example: C++ Namespaces – Can place each library in its own namespace and qualify names used outside with the namespace • Motivation: Large programs define many global names; need a way to divide into logical groupings Concepts of Programming Languages 22

Naming Encapsulations (continued) • Java Packages – Similar to namespace encapsulation – Different packages

Naming Encapsulations (continued) • Java Packages – Similar to namespace encapsulation – Different packages can have classes with equal names – Clients of a package can use fully qualified name or use the import declaration Concepts of Programming Languages 23