Department of Computer Science



next up previous
Next: Unit 3, Arrays and Up: Course study guide Previous: Unit 1, Introduction to


Unit 2, Functions and classes


Subsections

Preview

The first topic in this unit deals with more basics: the character type, and functions. The second topic introduces classes.

Objectives

At the end of this you should be able to:

Unit

  1. The first topic is about characters, there are two sub-sections and an exercise:
    1. First read about the third example in sub-section Third Example. This introduces the char type (only barely a type!). It shows the difference between input using >> and get().
    2. Next the fourth example. It introduces character constants and a slightly more complicated program.
    3. At this point you should attempt this exercise. Note that this requires arithmetic on character values. A possible answer is in the appendix.
  2. The next topic concerns functions. In C++ not all functions have to be methods, they can exist outside classes.
    1. Read about the basic structure and calling of functions.
    2. Functions with no results (called procedures or subroutines in other languages) are described in the next sub-section.
    3. The next sub-section describes different ways of passing parameters to functions.
    4. Lastly there are two exercises. The first is quite obvious. The second is harder. It asks for a function to take two positive integers and find the greatest common divisor. You are strongly advised NOT to use division and remainders but instead use Euclid's method.

      The method is:

      1. first check if the 2 numbers are the same if so stop and return that as the result,
      2. find which of the 2 numbers is larger and replace it by the result of subtracting the smaller number from it,
      3. go back to 1.
      For example:
      21 15
      21-15=6 15
      6 15-6=9
      6 9-6=3
      6-3=3 3
      3 3 return 3

      Possible solutions are in the answer appendix.
  3. The third topic is the introduction of simple classes. To experienced object oriented programmers the class is quite trivial, its real purpose is just to examine some of the C++ rules for writing classes. The things to look out for are: the way a class occur alongside a function, the syntax of writing classes, the way data hiding is indicated, and the role of the constructor (the creation method in Eiffel).

    Also in this topic is a description of how to put a class definition in a separate header file. It must be emphasised that this is very different from Eiffel, the header file is not compiled separately, instead it is textually included into the file all client files that want to use it.

    Finally there is a trivial exercise, the first exercise is to add a single method to the Accumulator class and the second to devise a simple class called MaxMin. Possible solution programs in the appendix.

Summary

This unit has introduced a lot of small concepts:

To contents



next up previous
Next: Unit 3, Arrays and Up: Course study guide Previous: Unit 1, Introduction to


Page generated: 2002-11-04 by Bob Dickerson

© University of Hertfordshire Higher Education Corporation (1998)

Disclaimer