Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
E-Bookshop-master / uploads / file / 0152_T_Sebesta_programming.pdf
Скачиваний:
263
Добавлен:
28.06.2021
Размер:
5.2 Mб
Скачать

Programming Exercises

521

7.Explain the dangers of C’s approach to encapsulation.

8.Why didn’t C++ eliminate the problems discussed in Problem 7?

9.What are the advantages and disadvantages of the Objective-C approach to syntactically distinguishing class methods from instance methods?

10.In what ways are the method calls in C++ more or less readable than those of Objective-C?

11.What are the arguments for and against the Objective-C design that method access cannot be restricted?

12.Why are destructors rarely used in Java but essential in C++?

13.What are the arguments for and against the C++ policy on inlining of methods?

14.Describe a situation where a C# struct is preferable to a C# class.

15.Explain why naming encapsulations are important for developing large programs.

16.Describe the three ways a client can reference a name from a namespace in C++.

17.The namespace of the C# standard library, System, is not implicitly available to C# programs. Do you think this is a good idea? Defend your answer.

18.What are the advantages and disadvantages of the ability to change objects in Ruby?

19.Compare Java’s packages with Ruby’s modules.

P R O G R A M M I N G E X E R C I S E S

1.Design an abstract data type for a matrix with integer elements in a language that you know, including operations for addition, subtraction, and matrix multiplication.

2.Design a queue abstract data type for float elements in a language that you know, including operations for enqueue, dequeue, and empty. The dequeue operation removes the element and returns its value.

3.Modify the C++ class for the abstract stack type shown in Section 11.4.2 to use a linked list representation and test it with the same code that appears in this chapter.

4.Modify the Java class for the abstract stack type shown in Section 11.4.4 to use a linked list representation and test it with the same code that appears in this chapter.

5.Write an abstract data type for complex numbers, including operations for addition, subtraction, multiplication, division, extraction of each of

522

Chapter 11

Abstract Data Types and Encapsulation Constructs

the parts of a complex number, and construction of a complex number from two floating-point constants, variables, or expressions. Use Ada, C++, Java, C#, or Ruby.

6.Write an abstract data type for queues whose elements store 10-character names. The queue elements must be dynamically allocated from the heap. Queue operations are enqueue, dequeue, and empty. Use either Ada, C++, Java, C#, or Ruby.

7.Write an abstract data type for a queue whose elements can be any primitive type. Use Java 5.0, C# 2005, C++, or Ada.

8.Write an abstract data type for a queue whose elements include both a 20-character string and an integer priority. This queue must have the following methods: enqueue, which takes a string and an integer as parameters; dequeue, which returns the string from the queue that has the highest priority; and empty. The queue is not to be maintained in priority order of its elements, so the dequeue operation must always search the whole queue.

9.A deque is a double-ended queue, with operations adding and removing elements from either end. Modify the solution to Programming Exercise 7 to implement a deque.

10.Write an abstract data type for rational numbers (a numerator and a denominator). Include a constructor and methods for getting the numerator, getting the denominator, addition, subtraction, multiplication, division, equality testing, and display. Use Java, C#, C++, Ada, or Ruby.

12

Support for Object-

Oriented Programming

12.1Introduction

12.2Object-Oriented Programming

12.3Design Issues for Object-Oriented Languages

12.4Support for Object-Oriented Programming in Smalltalk

12.5Support for Object-Oriented Programming in C++

12.6Support for Object-Oriented Programming in Objective-C

12.7Support for Object-Oriented Programming in Java

12.8Support for Object-Oriented Programming in C#

12.9Support for Object-Oriented Programming in Ada 95

12.10 Support for Object-Oriented Programming in Ruby

12.11 Implementation of Object-Oriented Constructs

523

524

Chapter 12 Support for Object-Oriented Programming

This chapter begins with a brief introduction to object-oriented programming, followed by an extended discussion of the primary design issues for inheritance and dynamic binding. Next, the support for object-oriented program-

ming in Smalltalk, C++, Objective-C, Java, C#, Ada 95, and Ruby is discussed. The chapter concludes with a short overview of the implementation of dynamic bindings of method calls to methods in object-oriented languages.

12.1 Introduction

Languages that support object-oriented programming now are firmly entrenched in the mainstream. From COBOL to LISP, including virtually every language in between, dialects that support object-oriented programming have appeared. C++, Objective-C, and Ada 95 support procedural and data-oriented programming, in addition to object-oriented programming. CLOS, an object-oriented version of LISP (Paepeke, 1993), also supports functional programming. Some of the newer languages that were designed to support object-oriented programming do not support other programming paradigms but still employ some of the basic imperative structures and have the appearance of the older imperative languages. Among these are Java and C#. Ruby is a bit challenging to categorize: It is a pure objectoriented language in the sense that all data are objects, but it is a hybrid language in that one can use it for procedural programming. Finally, there is the pure object-oriented but somewhat unconventional language: Smalltalk. Smalltalk was the first language to offer complete support for object-oriented programming. The details of support for object-oriented programming vary widely among languages, and that is the primary topic of this chapter.

This chapter relies heavily on Chapter 11. It is, in a sense, a continuation of that chapter. This relationship reflects the reality that object-oriented programming is, in essence, an application of the principle of abstraction to abstract data types. Specifically, in object-oriented programming, the commonality of a collection of similar abstract data types is factored out and put in a new type. The members of the collection inherit these common parts from that new type. This feature is inheritance, which is at the center of object-oriented programming and the languages that support it.

The other characterizing feature of object-oriented programming, dynamic binding of method calls to methods, is also extensively discussed in this chapter.

Although object-oriented programming is supported by some of the functional languages, for example, CLOS, OCaml, and F#, those languages are not discussed in this chapter.

Соседние файлы в папке file