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
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