www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/09/24/17:31:35

Message-ID: <360AD0D9.1D22@arcticmail.com>
From: Nick J Chackowsky <nick AT arcticmail DOT com>
Reply-To: nick AT arcticmail DOT com
MIME-Version: 1.0
Newsgroups: comp.os.msdos.djgpp
Subject: Developing a Rational Unit
Lines: 43
Date: Thu, 24 Sep 1998 21:11:27 GMT
NNTP-Posting-Host: 142.13.16.203
NNTP-Posting-Date: Thu, 24 Sep 1998 16:11:27 CDT
Organization: MBnet Networking Inc.
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

Mighty impressed by this DJGPP stuff, folks! I can't believe it's free;
I'm letting a LOT of people know about it.

I teach computer science to high school students, and have been
gradually developing a rational number unit with them, first
representing fractions as discrete integers, then as a structure, and
now, as a class. There certainly seem to be a LOT of ways of handling
the overloading of operators in a situation like this. I've seen:

class Rat {
  int num, den;
public:
  ...
  Rat operator + (Rat r);
  ...
};

as well as

Rat operator +(Rat left, Rat right); which is NOT declared within the
scope of the class. 

And finally, *something* like:

friend Rat operator + (...?...);

I'm wondering if there is a preferred method of declaring and
implementing overloaded operators of this sort. I've managed to get
methods 1 and 2 working, but I really don't properly understand the
third method yet. Perhaps someone could point me to a good source?  My
current source is: Lambert and Nance, _Fundamentals of C++:
Understanding Programming and Problem Solving_ which is GOOD, but
introduces a construct such as
   int numerator() const;  (an accessor to the class's numerator) 

without explaining the reason for the const keyword, or 
   istream& operator >> (...) 

without explaining the & in that context. I clearly need something a bit
better as a text. Perhaps someone knows of a free (hope hope hope)
source with modern code constructs explained?

Nick.

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019