Department of Computer Science
Next: Unit 3, Arrays and
Up: Course study guide
Previous: Unit 1, Introduction to
Unit 2, Functions and classes
Subsections
The first topic in this unit deals with more
basics: the character type, and functions.
The second topic introduces classes.
At the end of this you should be able to:
- to write programs using the char data-type,
- write programs that use arithmetic on characters because C++ treats the
char as a small int,
- write programs that use char I/O and that can
detect the end-of-file.
- write functions (NB this is C++ not Eiffel, functions
can exist outside classes),
- use value and reference parameters,
- write a program containing a simple class and using
an object,
- separate the class definition into a header file,
- The first topic is about characters, there are two sub-sections and
an exercise:
- 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().
- Next the fourth example.
It introduces character constants and a slightly more complicated program.
- At this point you should attempt this exercise.
Note that this requires arithmetic on character values. A possible answer
is in the appendix.
- The next topic concerns functions. In C++ not all functions
have to be methods, they can exist outside classes.
- Read about the basic structure and calling of functions.
- Functions with no results (called procedures or subroutines in other
languages) are described in the next sub-section.
- The next sub-section
describes different ways of passing parameters to functions.
- 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:
- first check if the 2 numbers are the same
if so stop and return that as the result,
- find which of the 2 numbers is larger and
replace it by the result of subtracting the
smaller number from it,
- 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.
- 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.
This unit has introduced a lot of small concepts:
- The character type char and how, in many ways, it is
more like a small integer (a byte) rather than a distinct type.
- functions that can exist as separate entities from classes,
how they are written, how they are called.
- classes (very simple ones) and they differences in definition
from Eiffel, although I hope it is possible to see the similarities too.
- finally as part of the classes topic, there is the concept of the
header file for class declarations.
To contents
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