![]() |
|
#1
|
|||
|
|||
overloading operators in a class.i dont know much about how to over load operatorsin a class so if somebody could give me some pointers i would appreciate it.
here is my class header file CPP / C++ / C Code:
but besides that i dont know what to do for overloading the <<, >>, <, >, <=, >=, ==, and != operators. can somebody please explain? thanks |
|
#2
|
|||
|
|||
Re: overloading operators in a class.Quote:
The first thing to do is to try to figure out what you want to accomplish with each operator that you implement. For example, if you create two instances of your Fraction class now and try to assign one to the other, you will receive a compiler error...remembering that assignment is "=" and equality is "==" in code. So, for an assignment operator, what arguments do you need and what return value must you give back? Do the same for each of the operators you want to implement. CPP / C++ / C Code:
Then decide what "test" must each of these operations "pass" in order to satisfy the needs of the return type. Will the internal representation of the instantiated fractional value be the same (equality) as another instance of the same class..or not (inequality)? These "seem" difficult at first because the syntax is perhaps a bit confusing and, in many cases, you're not simply working with POD. However, in this particular case, you have a very simple representation that will be very easy to implement these operations. I'd take it to another level and try to decide how I might implement operators ++ and -- in both prefix and postfix forms! Imagine a fraction 9/16ths...just for kicks. Wouldn't operator++() add another 16th to it so that the fractional representation is now 10/16ths, which would reduce to 5/8ths? And, wouldn't the decrementing operator subtract a 16th from 9/16ths so that you ended up with an internal representation of 8/16ths or 1/2? Something to kick around. Also, I'd recommend not getting into the habbit of selectively shortening things like: GreatestComDiv Spell it out completely and, whenever possible, try to figure out what the "normal" term is and use it. I believe that GreatestCommonFactor is the preferred term, which could then logically be abbreviated GCF, in this context, though definitely does not necessarily need to be shortened. :davis: |
|
#3
|
|||
|
|||
Re: overloading operators in a class.this is what i have to do. and i have to define <<, and >> operators for use too.
Write a main program to test your operators by using cin and >> to obtain two fractions and cout and << to display the original fractions, and then by creating a new fraction that is the sum of the first two, displaying that fraction (along with appropriate labels!), and then displaying the comparisons of the original two fractions for each of the relational operators. An example of the latter task, if you were to input the two fractions 4/5 and 12/7, would be the display of: 4/5 + 12/7? 4/5 < 12/7? true 4/5 > 12/7? false 4/5 <= 12/7? true 4/5 >= 12/7? false 4/5 == 12/7? false 4/5 != 12/7? true |
|
#4
|
|||
|
|||
Re: overloading operators in a class.Quote:
...it sounds like you've got more than a fair idea of what is needed to be done. Is there a question in all of this somewhere? If you're having difficult in understanding what implementation is required, how about taking a look at this code for some ideas? CPP / C++ / C Code:
It may give you an idea of what you want to do. Obviously, I quickly implemented a test for equality/inequality in something that doesn't really have an exact equality test...except when the two objects are the same object. Since your Fractions are going to be numerically based, it will be much easier to implement a good and representative test for equality. In my "Box" example, I could only use volume and weight as some manner of equality. We can see where the exception to a "real world" box would occur if perhaps the linear dimensions for one box were 100 x 100 x 1 versus 1 x 1 x 10000. The volume would be the same, but the transport "issues" would differ dramatically...so equality by both weight and volume alone would probably not be a real good example of true equality in this particular case. The class would need to be modified to somehow judge if the linear dimensions were outside of some idea of "reasonable proportions" in order to be worthy of a "real world" box as might be used in a shipping calculation or ship/container loading application. In your Fraction class, you can easily support the it using the values. Becareful to ensure that you test to see if 5/10ths is equal to 1/2...etc. That is about all I can think of to suggest at the moment. Also, you may want to ensure that your Fraction doesn't accept just: 1/2 on streamed input, output, but also: 1:2 ...it might be worth some "extra points." :davis: |
|
#5
|
|||
|
|||
Re: overloading operators in a class.well guys this is what i have so far i know there is a few msitakes in my code but i dont know what to do to fix them can somebody please help. here is my files and my errorlog thanks.
Header File CPP / C++ / C Code:
Implementation File CPP / C++ / C Code:
Main Program CPP / C++ / C Code:
ERRORLOG Quote:
|
|
#6
|
||||
|
||||
Re: overloading operators in a class.Quote:
Quote:
__________________
Age is unimportant -- except in cheese |
|
#7
|
|||
|
|||
Re: overloading operators in a class.Quote:
thanks for that help WALT but i am having problems with this part now. CPP / C++ / C Code:
CPP / C++ / C Code:
|
|
#8
|
|||
|
|||
Re: overloading operators in a class.Quote:
Take a look at your store parameters list. You're trying to use it with only two arguments and you've declared it to take three. You either need to overload store so that it takes two int args or call it with a istream reference from your operator+ code. :davis: |
|
#9
|
|||
|
|||
Re: overloading operators in a class.Quote:
i have tryed put the 3 arguments in with the overloading operator+ but it says i can only pass it 1 or 2 arguments and not 3. so what do i need to do to overload the store function? |
|
#10
|
|||
|
|||
Re: overloading operators in a class.Quote:
I dunno, maybe implement a store function that takes the two parameters that you want? Better yet, why not an args ctor that takes the two parameters that you want? ...that would be easiest, especially since you already have one! CPP / C++ / C Code:
...easier? Shouldn't you also be making a lot of parameters const in your declarations/implementations? :davis: |
Recent GIDBlog
Developing GUIs with wxPython (Part 2) by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Introduction to .NET | LuciWiz | .NET Forum | 5 | 09-Aug-2007 04:53 |
| Error C2146: syntax error : missing ',' before identifier 'C4' | mattchew008 | C++ Forum | 2 | 19-Dec-2004 06:06 |
| Help! Some basal questions about MFC | xutingnjupt | MS Visual C++ / MFC Forum | 1 | 05-Dec-2004 03:38 |
| hashing help | saiz66 | C++ Forum | 1 | 06-Jul-2004 06:16 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The