![]() |
|
#1
|
|||
|
|||
Write a C++ program involving operator overloadingPlease see my other post for my (not-so-great) backstory. Here's the assignment:
Write a C++ program involving operator overloading. I have started a class PIGS. You are to take what I have started and add: 1. 2 constructors 2. 6 operator overloading functions The default constructor in to create a white PIG of 200 pounds. The parameterized constructor is to fill in the color and size with the parameters passed. What you are to overload: 1. The binary + to add two PIG objects a. Add the two sizes b. Take the color of the first operand 2. The binary + to add a float to a PIG object a. Just add the number to the size 3. The binary + to add a PIG object to a float Just add the number to the size 4. The incrementing operator ++ (prefix) a. Just add one to the size--make it work like normal prefix 5. The incrementing operator ++ (postfix) a. Just added one to the size--make it work like normal postfix 6. The overloaded assignment operator += a. Just added the float to the size--should work like normal += THE CODE TO USE CPP / C++ / C Code:
Like before, since I'm totally lost, a detailed walkthrough would be GREATLY appreciated. Thanks for your time. Sincerely, Jason Seehusen Last edited by admin : 11-Dec-2006 at 01:33.
Reason: Please insert your C code between [cpp] & [/cpp] tags
|
|||
|
#2
|
||||
|
||||
Re: Another Homework Help (Also Due 12/14/06)Welcom, Jason. We will definitely do what we can to help you with your asssignments. But you have to supply us with information we can help you with.
Quote:
Please read the Guidelines, especially #1, #2, #3, and definitely #5. #4 can be important, too. Quote:
__________________
During the election they said Obama could only be elected when pigs fly. Well, we currently have an epidemic of Swine Flu. Coincidence? |
|
#3
|
|||
|
|||
Re: Write a C++ program involving operator overloadingI started a very quick hack to see if I couldn't help get you started. I changed PIGS to be a Pig class, since it makes better sense, particularly since it represents a single pig instance and not a "collection" of pigs.
Pig.h CPP / C++ / C Code:
Pig.cpp CPP / C++ / C Code:
main.cpp CPP / C++ / C Code:
Output: Code:
...note that I commented out a section of the original code in "main" since there isn't a way that I know of to implement it and I didn't want to spend any more time researching it or considering it. I also implement equality and inequality operations, which were not explicitly required in the posted requirements. I didn't do any elaborate testing or reconcilling of the output to ensure that it accurately met the requirements, so you may want to determine that information for yourself. What do you expect for 10-minutes? You can do away with the virtual keyword on the dtor, too. Force of habbit for me, since I don't usually write classes that have no virtual operations. The key points to consider when reviewing this code is that I just tossed it together, so it may or may not be adequate for your needs. The "requirements" are (seemingly) intentionally ambiguous and inside of main there are several "implicit" requirements, such as the implementation of the streaming operations (>> and <<), which must be implemented in the Pig class in order for main to compile function as expected. As I said, maybe Dave or someone can take a look at it, but I don't know how to add a Pig to a float constant, so I didn't even try to implement that functionality, rather I just commented out the code that was in main. The alternative that I can think of is modifying the code in main such that it uses an operation to get access to the Pig::m_size member such that we have it and then add it to the float constant. Oh well, you can see that I didn't bother with it. If you look at the code that I wrote, particularly the .h file, you will see what operations (aka methods/functions) are required by the requirements...or at least those that I chose to support. The notion that we will happily add floats to pigs doesn't make very much sense in "the real world," but as an academic exercise, it seems like a viable objective. Note also that I modified the code so that it doesn't have multiple variable declarations on a single line, which is incredibly novice, IMO, particularly when one is being initialized with args and the others are left to their default ctors. Hopefully this code will help get your head around some of the issues related to the requirements. You just have to tell me why there's a cast in the prefix operator impl. :davis: |
Recent GIDBlog
Problems with the Navy (Chiefs) by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Implement an integer array hierarchy | jjrsbigo | C++ Forum | 3 | 12-Dec-2006 07:56 |
| It's not homework but just want to learn | Buggaya | C Programming Language | 8 | 08-Jun-2006 16:25 |
| DO my homework for me now | CableGuy | GIDForums™ | 12 | 13-Sep-2005 10:38 |
| help!!!!!!!! C++ homework | newbie | C++ Forum | 2 | 20-Jan-2004 10:41 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The