Looking for tutorial example classes

Discussion in 'C++' started by Stefan Ram, May 24, 2011.

  1. Stefan Ram

    Stefan Ram Guest

    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
    Stefan Ram, May 24, 2011
    #1
    1. Advertising

  2. Stefan Ram

    Balog Pal Guest

    "Stefan Ram" <-berlin.de>
    > 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.
    Balog Pal, May 24, 2011
    #2
    1. Advertising

  3. Stefan Ram

    Paul N Guest

    On May 24, 4:29 pm, -berlin.de (Stefan Ram) wrote:
    >   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.
    Paul N, May 24, 2011
    #3
  4. * 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

    --
    blog at <url: http://alfps.wordpress.com>
    Alf P. Steinbach /Usenet, May 25, 2011
    #4
  5. Stefan Ram

    Öö Tiib Guest

    On May 24, 6:29 pm, -berlin.de (Stefan Ram) wrote:
    >
    >   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.
    Öö Tiib, May 25, 2011
    #5
  6. Stefan Ram

    Björn Guest

    Stefan Ram wrote:

    > 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
    Björn, May 25, 2011
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Nachiketh
    Replies:
    0
    Views:
    565
    Nachiketh
    Feb 23, 2004
  2. John Gabriele
    Replies:
    0
    Views:
    855
    John Gabriele
    Oct 30, 2003
  3. Rensjuh
    Replies:
    7
    Views:
    964
    Mabden
    Sep 2, 2004
  4. W. Watson
    Replies:
    13
    Views:
    1,063
    W. Watson
    Sep 20, 2007
  5. Linuxmank
    Replies:
    36
    Views:
    1,495
    Linuxmank
    Aug 8, 2009
Loading...

Share This Page