Chapter 7

Most of the information on this and other chapters refers directly to material in Wilson, L.B. and Clark, R.G., Comparative Programming Languages (Third Edition, updated by R.G. Clark), Addison-Wesley, September 2000, ISBN 0-201-71012-9.

The material is intended to supplement the textbook by providing further examples and discussion. It is not self-standing, but assumes that you have a copy of the book to hand.

Section 7.2 Arrays

Associative arrays

Supplementary Questions

  1. What is logically wrong with the following C++ function definition where Stype is the name of a class?
    Stype* fun() {
        Stype a[10];
        ...
        return a;
    } // fun
    
    What would happen when we made the call:
    Stype* b = fun();
    
    What change should we make to the function definition?

  2. Distinguish between name and structural equivalence of types. Which do you think is to be preferred in the case of a) arrays and b) classes?

Section 7.5 Parametrised types

Supplementary Questions

  1. Consider the following Ada package:
    package vec is
        type vector is private;
        function add(a, b: in vector) return vector;
        function largest(a: in vector) return real;
    private
        type vector is array (1 .. 6) of real;
    end vec;
    
    Show how you would declare variables of type vector in Ada and give examples of calls of add and largest.

    Convert the above Ada package into a generic package which can be instantiated to deal with vectors of any length and whose elements are of any scalar type.