Looking for tutorial example classes

S

Stefan Ram

Do you know of class requirements that could be used as
examples in teaching to explaing classes with overloaded
operators? I though of the following ideas, but have not
yet found a good solution.

complex numbers

People often criticize this, because they say that this is
already available in ::std and one should not reinvent the
wheel. Moreover, my students are beginners often with very
poor math capabilities, so some of them might be alienated
by anything that uses too much math (or, any math at all).

a value with an error, like (2,42±0,02 m)

I like this one, because I always wanted to program it for
myself, but it might be too much math for my students, again.

a value with a unit, like 2 m (two meters)

This also is nice, but might already be too difficult for
some students, again.

a pair like hh:mm (hours and minutes)

This is the one which I like the most so far, because it
does contain some math, but the math seems to be simple
enough in this case. We would have, for example,
02:40 + 03:40 = 06:20.

However, to show the idea of a class as a compound of
several primitive values, this should be implemented as

{ private: int hours; int minutes; public: ... }

, but it seems to me that a better implementation would
use just

{ private: int minutes; public: ... }

and uses hours only for formatted output (display).
But then, addition would degrade into nothing more than
simple integer addition, which might raise the question of
why this has to be a class at all.

I also thought of my (boring) standard example of a bank
account, but it would not make sense to add two accounts.
(At most, I might add a number [inpayment] to it. But this
also seems quite trivial, because the account is nothing
more than a balance value wrapped in a class, so you do
nothing more than to wrap the standard addition of numbers.)

So does anyone have any other idea? The class should

- not implement something already available in
standard C++

- have a need for at least two private fields
of data

- use some math (e.g., when overloading »+«),
but the math should be very simple
 
B

Balog Pal

Stefan Ram said:
Do you know of class requirements that could be used as
examples in teaching to explaing classes with overloaded
operators? I though of the following ideas, but have not
yet found a good solution.

complex numbers

People often criticize this, because they say that this is
already available in ::std and one should not reinvent the
wheel.

Using it as example has nothing to do with reinventing the wheel. But if
that really bothers you, use 'rational' instead of complex. BigNumber
(arbitrary-precision integral) is another fair choice.
 
P

Paul N

  Do you know of class requirements that could be used as
  examples in teaching to explaing classes with overloaded
  operators?
(snip)

  So does anyone have any other idea? The class should

      - not implement something already available in
        standard C++

      - have a need for at least two private fields
        of data

      - use some math (e.g., when overloading »+«),
        but the math should be very simple

One possibility is a string class, but this fails all three of your
tests :)
Alternatively, integers of more than the usual length (either a fixed
longer-than-usual length, or arbitrary precision as Balog has
suggested) - passes two of your tests.

Paul.
 
A

Alf P. Steinbach /Usenet

* Stefan Ram, on 24.05.2011 17:29:
So does anyone have any other idea? The class should

- not implement something already available in
standard C++

- have a need for at least two private fields
of data

- use some math (e.g., when overloading »+«),
but the math should be very simple

points and vectors.

there is the additional problem of what operation '*' should map to for vectors. :)


Cheers & hth.,

- Alf
 
Ö

Öö Tiib

  So does anyone have any other idea? The class should

      - not implement something already available in
        standard C++

      - have a need for at least two private fields
        of data

      - use some math (e.g., when overloading »+«),
        but the math should be very simple

So your students are not expected to know various fields of
mathematics or physics too well. That is bad. Almost anything
implemented to support a model of some algebra can be made to look
better by overloading some operators.

Maybe take something that deals with discrete values and functions.
For example from matematical logics, set theory, combinatorics or
graph theory. These start with simple enough things.
 
B

Björn

Stefan said:
complex numbers

People often criticize this, because they say that this is
already available in ::std and one should not reinvent the
wheel. Moreover, my students are beginners often with very
poor math capabilities, so some of them might be alienated
by anything that uses too much math (or, any math at all).

For teaching purposes I'd rather welcome parts of the STL, because people
would learn STL as well. Of course, the resulting class should behave
exactly as the original in ::std as far as it's implemented (e.g., have the
same error handling).

But if the students do not know complex numbers at all I'd not recommend
that.
So does anyone have any other idea? The class should

- not implement something already available in
standard C++

- have a need for at least two private fields
of data

- use some math (e.g., when overloading »+«),
but the math should be very simple

What about modulo operations? You could start with the modulo as a private
constant and if you plan to teach templates, later, you can reuse your
example and make it a template parameter.

Regards
Björn
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,260
Messages
2,571,038
Members
48,768
Latest member
first4landlord

Latest Threads

Top