24
Principles of programming languages 10: Object oriented languages Isao Sasano Department of Information Science and Engineering

Principles of programming languages 10: Object oriented languages Isao Sasano Department of Information Science and Engineering

Embed Size (px)

Citation preview

Page 1: Principles of programming languages 10: Object oriented languages Isao Sasano Department of Information Science and Engineering

Principles of programming languages 10: Object oriented languages

Isao Sasano

Department of Information Science and Engineering

Page 2: Principles of programming languages 10: Object oriented languages Isao Sasano Department of Information Science and Engineering

Division of programsIt is better to develop large programs by dividing into smaller programs. Procedures provide one of the most basic way for division.

Modules provide a way to put together variables, procedures, and types which relate to each other. Modula-2 (a successor of Pascal) supports modules as its construct.Modules enable us to hide implementation of data types processed by procedures.

Page 3: Principles of programming languages 10: Object oriented languages Isao Sasano Department of Information Science and Engineering

ModulesModules group declarations of variables, procedures, types, and so on. By exporting types in modules, we effectively obtain user-defined data types. [Implementation of stack in a module(Modula-2) ]type stack;function pop(s: stack): integer;procedure push(a: integer; s: stack);function newstack: stack;type stack = ↑rep; rep = record …function newstack: stack; var s: stack;begin …

Interface

Implementation

Page 4: Principles of programming languages 10: Object oriented languages Isao Sasano Department of Information Science and Engineering

(reference) abstract data types

Abstract data types consist of types and operations on them. They enable us to define new types similarly to the built-in types such as int.Built-in types such as int accompany operations such as addition, subtraction, multiplication, division, etc. If the language is defined so that accessing to the values of built-in types is only allowed through the accompanying operations, the behaviors of programs are not affected even if the representations of the built-in types are different in different computer architectures. Similarly, behaviors of programs with abstract data types are not affected even if their implementation are changed.

Page 5: Principles of programming languages 10: Object oriented languages Isao Sasano Department of Information Science and Engineering

(Reference) Abstract data types (cont.)

Languages like Standard ML support abstract data types. Standard ML is statically typed and checks whether or not the values of abstract data types are only accessed by the accompanying operations. (Programs that access the values of abstract data types not by the accompanying operations become type error in compile time.)

Page 6: Principles of programming languages 10: Object oriented languages Isao Sasano Department of Information Science and Engineering

Classes in C++Classes in C++ are generalizations of records, which are called structures in C.After declaring a class, we can use it as a type name, declare variables of the type, and generate an object of the type.

struct Stack { int top; char elements [101]; char pop(); void push (char); Stack(); };

(ex.) class Stack { public: int top; char elements [101]; char pop(); void push (char); Stack();};

(Cf.) C++ is designed by porting classes in Simula 67 to C.

Page 7: Principles of programming languages 10: Object oriented languages Isao Sasano Department of Information Science and Engineering

Class declarations in C++

The declaration struct X { <declarations> };is equivalent to the declaration class X { public : <declarations> }and the declaration class X { <declarations> };is equivalent to the declaration struct X { private : <declarations> };

Page 8: Principles of programming languages 10: Object oriented languages Isao Sasano Department of Information Science and Engineering

An example of class declaration class Stack { int top; int size; char *elements; public: Stack (int n) {size=n; elements = new char[size]; top=0;} ~Stack() { delete elements; } void push (char a) {top++; elements[top] = a; char pop() {top--; return elements[top+1];};

Page 9: Principles of programming languages 10: Object oriented languages Isao Sasano Department of Information Science and Engineering

Generation and deletion of objectsIn C++ objects are generated by new and deleted by delete. For any type T, the expression new Tgenerates an object of type T and the value of the expression is the pointer to the generated object. delete pdeletes the object pointed by p.(ex.) In the previous example, elements = new char [size]generates an array of length size of type char. elements[0], elements[1], …, elements[size-1] are the elements of the array. By delete elements the array object pointed by elements is deleted.

Page 10: Principles of programming languages 10: Object oriented languages Isao Sasano Department of Information Science and Engineering

Templates (ex.) template <class T> class Stack { int top; int size; T * elements; public: Stack (int n) {size=n; elements = new T[size]; top=0;} ~Stack() { delete elements; } void push (T a) { top++; elements[top]=a; } T pop() { top--; return elements[top+1]; } };

The expression new Stack<int> s(99) generates a Stack object whose elements are of type int.

Page 11: Principles of programming languages 10: Object oriented languages Isao Sasano Department of Information Science and Engineering

Comparison between C and C++C++ was designed and implemented by Bjarne Stroustrup in 1983. C++ was intended to be as an extension of C and most of programs in C is programs in C++ that have the same meaning. But the are some programs that have different meaning in C and C++.Comments are /* … */ in C and // … in C++. (In ISO C99, //… are also comments)

(ex.) int f (int a, int b) { return a //* */ b ;}

The expression a //* */ b is a/b in ISO C89 and a in ISO C99 and C++.

Page 12: Principles of programming languages 10: Object oriented languages Isao Sasano Department of Information Science and Engineering

Comparison between C and C++ (Cont.)

int x[99]; void f(void) { struct x {int a;}; sizeof (x); }

In C++, if a structure is declared with its name, the name itself represents the structure type. (ex.) struct test {int a;} test x;In C, we need to write struct test x;. (In C++ we can also write struct test x;.)

(ex.) sizeof(x) is the size of the array x in C while it is the size of the structure x in C++. In C we need to write sizeof (struct x) for the size of the structure x.

Page 13: Principles of programming languages 10: Object oriented languages Isao Sasano Department of Information Science and Engineering

In C, sizeof(‘a’) is equal to sizeof(int).In C++, sizeof(‘a’) is equal to sizeof(char).

In C, the size of an enumerated type is equal to sizeof(int).In C++, the size of enumerated types depends.

(ex.) Under the declaration enum color {RED, BLUE, YELLOW};sizeof(enum color) and sizeof(int) are same in C but they may not be same in C++.

Comparison between C and C++ (Cont.)

Page 14: Principles of programming languages 10: Object oriented languages Isao Sasano Department of Information Science and Engineering

Object-oriented languagesObject-oriented languages were invented to describe some simulations. Objects are elements in simulations.

(ex.) Simula( Simulation language, 1967)• It was developed by Ole-Johan Dahl, Kristen

Nygaard.• It is the first object-oriented language, although

the term “object-oriented” were not used at the time.

• It was designed as an extension of ALGOL.• Description of airport system was an important

example.

Page 15: Principles of programming languages 10: Object oriented languages Isao Sasano Department of Information Science and Engineering

Object-oriented languagesExamples of objects in airports: customers, counters, queues, tickets, etc.

(external view)Computations proceed by passing messages between objects.

(Internal view) When receiving a message, a corresponding function is executed. The function is referred to as method or member function.

Page 16: Principles of programming languages 10: Object oriented languages Isao Sasano Department of Information Science and Engineering

Hierarchy of classesShape

Box Ellipse

Circle

Line Text

Box Ellipse Line TextCircle

Shape

Page 17: Principles of programming languages 10: Object oriented languages Isao Sasano Department of Information Science and Engineering

Inheritance

• Derived class inherits the member variables and member functions of its parent (base) class. (The member variables and member functions of the base class becomes the ones in its derived classes.)

• In the derived classes, member variables and member functions can be additionally defined. When the name is same, the definition is overrided.

(Note) “overload” is different from “override”. “Overload” is to define methods of the same name with the the number of arguments or the types of arguments being different.

Page 18: Principles of programming languages 10: Object oriented languages Isao Sasano Department of Information Science and Engineering

Examples in C++In C++, inheritance is described as follows.

class Box : public Shape { … }

Inherit all the members with keeping the visibility.

In C++, super classes (or parent classes) are called base classes and subclasses (or child classes) are called derived classes.

class Box : private Shape { … }

Inherited members are private by default.

Page 19: Principles of programming languages 10: Object oriented languages Isao Sasano Department of Information Science and Engineering

Virtual functions

class B { public: virtual char f () { return ‘B’; } char g() { return ‘B’; } char testF() {return f(); } char testG() { return g(); } };

class D : public B { public: char f () { return ‘D’; } char g() { return ‘D’; } }; #include <iostream> int main (void) { D d; std::cout << d.testF() << d.testG() << "\n"; return 0; }

d.testF() returns ’D’ and d.testG() returns ’B’.

Attaching the keyword virtual to a method declaration, which of the methods is executed is determined in runtime, not in compile time.

Page 20: Principles of programming languages 10: Object oriented languages Isao Sasano Department of Information Science and Engineering

NoteMethods are compiled to functions with having one additional argument this in C++ compiler. The testF method is compiled as follows.

Corresponding to this, d.testF() is compiled to testF(d). In the computation of testF(d), d->f() is executed. The method f is virtual in the class B, d->f() is compiled so that the method f that corresponds to the class of the object d. In this example, the method f in the class D is called and ‘D’ is returned.

char testF() { return f(); }

char testF (B * this) { return this->f(); }

Page 21: Principles of programming languages 10: Object oriented languages Isao Sasano Department of Information Science and Engineering

Features of C++• C++ was designed to keep the backward

compatibility with C as much as possible. • Objects are extension of structures in C. That is,

objects can be allocated in the activation records (in the stack) as well as in the heap.

• Imperative style of programming is possible. C++ does not force the programmer to program in object-oriented style.

• Multiple inheritance is supported. (This is out of the scope of this class.)

Page 22: Principles of programming languages 10: Object oriented languages Isao Sasano Department of Information Science and Engineering

Summary of OO language

• Dynamic lookup (which of the methods is executed is determined in runtime. )

• Abstraction (public members serve as interface and private ones are hidden from the outside. )

• Subtyping (Where objects of some class are expected, objects of the classes that inherits the class with the public keyword can be used. )

• Inheritance (The amount of code is reduced so that it is easier to modify code. )

OO languages have the following features.

Page 23: Principles of programming languages 10: Object oriented languages Isao Sasano Department of Information Science and Engineering

NoteSubtyping and inheritance are different.(ex.) Queue --- first-in, first-outStack --- last-in, first-outDequeue --- queue where inputs and outputs can be done in the both side

Queue class and Stack class can be implemented as a derived class of Dequeue class by privately inheriting members by default and make public some methods.But Queue and Stack are not subtype of Dequeue.

Page 24: Principles of programming languages 10: Object oriented languages Isao Sasano Department of Information Science and Engineering

(Just for Reference) Data invariant• Properties that hold when the control is not in the

objects. • (ex.) bounded buffer (queue with bounded length)

– It has two methods put(x) and get().– It stores objects in an array and members front and rear

represents the range, where the next of the last element of the array is the first element of the array.

Objects are designed considering data invariant.

The buffer is empty when front is equal to rear. The buffer is full when the next of rear is front. Elements are in the order of putting them between

front and rear.