
-----------------------------------
wtd
Wed Mar 02, 2005 6:46 pm

Fraction class
-----------------------------------
When dealing with numbers, it's not uncommon for us to use floating point numbers.  The problem, however, is that floating point numbers are inherently inaccurate.  The solution?  Create a fraction class that stores the numerator and denominator separately, and overloads a number of operators and functions so that fractions can be dealt with much the same as other numbers.

Consider a simple example:

#include 
#include "Fraction.h"

int main()
{
   Fraction a(1, 3), b(1, 2);

   std::cout  buff;
     return 0;
}

-----------------------------------
jamonathin
Wed Apr 13, 2005 9:03 am


-----------------------------------
you're a bloody genious  :hb:  thanks for your help martin.

-----------------------------------
Martin
Wed Apr 13, 2005 9:15 am


-----------------------------------
No problem. One thing that always confused me when I started learning C++ was the use of the line 'using namespace std;'

C++ has literally thousands of various functions, and, should you use a bunch of libraries, conflicts are going to occur when functions have the same name. 'using namespace std;' says 'use the standard (std) namespace'

A namespace is used to battle the problem of conflicting names. With namespaces, functions belong to a collection; in the above case, they belong to the 'std' collection.

The above code that I posted is the exact equivalent of this, without namespaces:

#include 

int main ()
{
     std::cout > buff;
     return 0;
}

Note the std:: at the beginning of the function. This says 'use std's cout function.'

The line 'using namespace std;' says 'whenever a function's not defined by the code, check to see if it's defined in std'

Now I suggest we leave wtd's fraction class topic alone. Make a new topic if you need more help.

-----------------------------------
Justin_
Thu Feb 02, 2006 5:32 pm


-----------------------------------
Did you code this wtd?  

Looks to be pretty slick coding  :D

-----------------------------------
wtd
Thu Feb 02, 2006 5:46 pm


-----------------------------------
Yep, and thanks.  :)

-----------------------------------
Justin_
Thu Feb 02, 2006 11:10 pm


-----------------------------------
might want to work on your commenting though :p


btw, what do you do for a living wtd?  And how old are you?

-----------------------------------
wtd
Thu Feb 02, 2006 11:22 pm


-----------------------------------
What's wrong with my use of comments?

I'm older than dirt, and am currently unemployed as required by Canadian law since I'm working through the immigration process.

-----------------------------------
Hikaru79
Thu Feb 02, 2006 11:29 pm


-----------------------------------
What's wrong with my use of comments?

Um, I might be missing something, but ... there is no use of comments.

-----------------------------------
wtd
Thu Feb 02, 2006 11:31 pm


-----------------------------------
What's wrong with my use of comments?

Um, I might be missing something, but ... there is no use of comments.

Yes, I'm guessing that's what Justin_ is taking exception with.

-----------------------------------
Justin_
Thu Feb 02, 2006 11:51 pm


-----------------------------------

What's wrong with my use of comments?


That question stimulates a good laugh anyway :? 


I'm older than dirt, and am currently unemployed as required by Canadian law since I'm working through the immigration process. 


I'm sorry to hear that.  Where are you native to if not Canada?  There's a good loophole in the immigration system, all you have to do is marry a Canadian citizen and your in the door.

-----------------------------------
wtd
Thu Feb 02, 2006 11:56 pm


-----------------------------------

What's wrong with my use of comments?


That question stimulates a good laugh anyway :? 

I don't use comments when my code is self-documenting.  The code spoke for itself.

I take it you find this concept strange.


I'm older than dirt, and am currently unemployed as required by Canadian law since I'm working through the immigration process. 


I'm sorry to hear that.  Where are you native to if not Canada?  There's a good loophole in the immigration system, all you have to do is marry a Canadian citizen and your in the door.

I'm originally from the US, and I am married to a Canadian citizen.  The notion that doing so guarantees a person citizenship in Canada is a myth.  It "gets your foot in the door", but nothing is certain.

-----------------------------------
Justin_
Fri Feb 03, 2006 12:11 pm


-----------------------------------
I knew you would suggest that your code was self documenting, which it is, but it is always nice to throw in a few comments on some parts of the code you may have had trouble with, or something to that extent.

-----------------------------------
wtd
Fri Feb 03, 2006 4:06 pm


-----------------------------------
I didn't have trouble with any of it.  It was a bit tedious, but not difficult.  :)
