GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 10-Dec-2006, 23:32
jjrsbigo jjrsbigo is offline
New Member
 
Join Date: Dec 2006
Posts: 3
jjrsbigo is an unknown quantity at this point

Write a C++ program involving operator overloading


Please 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:
#include <iostream> 
#include <string> 
using namespace std; 

class PIGS 
{private: 
string color; 
float size; 
public: 
PIGS(); 
PIGS(string, float); 
// place declarations here 



}; 

int main() 
{PIGS p1, p2("BLACK",150),p3; 
cout << p1 << endl; 
cout << p2 << endl; 
cin >> p3; 
cout << p3 << endl; 
p3 = p2 + p1; 
cout << p3 << endl; 
cout << p2 << endl; 
cout << p1 << endl; 
p3 = p2 + 10.5; 
cout << p3 << endl; 
p3 = 11.6 + p2; 
cout << p3 << endl; 
p3 = p1++; 
cout << p3 << endl; 
cout << p1 << endl; 
p3 = ++p1; 
cout << p3 << endl; 
cout << p1 << endl; 
p1+=5.6; 
cout << p1 << endl; 
return 0; 
} 


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  
Old 11-Dec-2006, 00:47
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,335
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all

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:
Originally Posted by jjrsbigo
Here's the assignment:
We do require you to post code that we can help you with. We are not here to teach, but help you understand what you're having trouble with -- specific things, not general "how do I do this program?"

Please read the Guidelines, especially #1, #2, #3, and definitely #5. #4 can be important, too.

Quote:
Originally Posted by jjrsbigo
...my professor's not a huge help (he expects me to understand this). A walkthrough this problem with detailed instructions would be greatly appreciated !
But it is his job to help you understand it, it's your job to sit down with him and have him explain what you don't understand.
__________________

During the election they said Obama could only be elected when pigs fly. Well, we currently have an epidemic of Swine Flu. Coincidence?
  #3  
Old 13-Dec-2006, 07:47
davis
 
Posts: n/a

Re: Write a C++ program involving operator overloading


I 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:
#ifndef _Pig_h_
#define _Pig_h_ 1

#include <string>
#include <fstream>

class Pig 
{
public: 
    Pig(); 
    Pig( std::string color, float size );
    Pig( const Pig& rhs );
    virtual ~Pig();

    const float operator+( const Pig& rhs );
    const float operator+( const float size );

    const Pig& operator++();
    const Pig& operator++( const int i=0 );
    void operator+=( const float size );

    const Pig& operator=( const Pig& rhs ); 
    void operator=( const float f ); 

    bool operator==( const Pig& rhs );
    bool operator!=( const Pig& rhs );
    
    friend std::ostream& operator<<( std::ostream& os, const Pig& rhs );
    friend std::istream& operator>>( std::istream& is, Pig& rhs );

protected:
private: 
    std::string m_color; 
    float m_size; 
}; 

#endif // ! _Pig_h_ 


Pig.cpp

CPP / C++ / C Code:
#ifndef _Pig_h_
#include <Pig.h>
#endif

Pig::Pig() : m_color( "White" ), m_size( 200 )
{
}

Pig::Pig( std::string color, float size ) : m_color( color ), m_size( size )
{
}

Pig::Pig( const Pig& rhs )
{
    m_color = rhs.m_color;
    m_size = rhs.m_size;
}

Pig::~Pig()
{
}

const float 
Pig::operator+( const Pig& rhs )
{
    return (m_size + rhs.m_size);
}

const float 
Pig::operator+( const float size )
{
    return (m_size + size);
}

const Pig&
Pig::operator++()
{
    ++m_size;
    return *this;
}

const Pig&
Pig::operator++( const int i )
{
    (void)i;
    ++m_size;
    return *this;
}

void
Pig::operator+=( const float size )
{
    m_size += size;
}

const Pig&
Pig::operator=( const Pig& rhs )
{
    if( this != &rhs )
    {
	m_color = rhs.m_color;
	m_size = rhs.m_size;
    }
    return *this;
}

void
Pig::operator=( const float f )
{
   m_size = f;
}

bool
Pig::operator==( const Pig& rhs )
{
    bool bIsEquality = false;
    if( rhs.m_color == m_color && rhs.m_size == m_size )
    {
	bIsEquality = true;
    }
    return bIsEquality;
}

bool
Pig::operator!=( const Pig& rhs )
{
    bool bIsInequality = false;
    if( rhs.m_color != m_color || rhs.m_size != m_size )
    {
	bIsInequality = true;
    }
    return bIsInequality;
}

std::ostream&
operator<<( std::ostream& os, const Pig& rhs )
{
    os << rhs.m_color << rhs.m_size;
    return os;
}

std::istream&
operator>>( std::istream& is, Pig& rhs )
{
    is >> rhs.m_color >> rhs.m_size;
    return is;
}


main.cpp

CPP / C++ / C Code:
#ifndef _Pig_h_
#include <Pig.h>
#endif

#include <iostream>
#include <string>

using namespace std;

int main()
{
    Pig p1;
    Pig p2( "Black", 150.0F );
    Pig p3;

    cout << p1 << endl; 
    cout << p2 << endl; 

    cin >> p3; 
    cout << p3 << endl; 

    p3 = p2 + p1; 
    cout << p3 << endl; 
    cout << p2 << endl; 
    cout << p1 << endl; 

    p3 = p2 + 10.5F; 
    cout << p3 << endl; 

/*
    p3 = 11.6F + p2; 
    cout << p3 << endl; 
*/

    p3 = p1++; 
    cout << p3 << endl; 
    cout << p1 << endl; 

    p3 = ++p1; 
    cout << p3 << endl; 
    cout << p1 << endl; 

    p1 += 5.6F; 
    cout << p1 << endl; 

    return 0; 
};


Output:

Code:
White200 Black150 Grey 300 Grey300 Grey350 Black150 White200 Grey160.5 White201 White201 White202 White202 White207.6

...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 GIDBlogProblems with the Navy (Chiefs) by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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

All times are GMT -6. The time now is 15:25.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.