Solved

operator+ question in C++

Posted on 2009-05-03
9
504 Views
Last Modified: 2012-08-14

hello group,

What is wrong with the following code?

The program is not working after this line:
 
 PiggyBank Combined = bank1 + bank2;



PiggyBank bank1;
PiggyBank bank2;
bank1.setSize( 20 );
bank1.feedCoins( 10, 1, 0, 1 );
bank2.setSize( 20 );
bank2.feedCoins( 10, 0, 0, 0 );
PiggyBank combined = bank1 + bank2;
cout << "combined's value is: " << combined.getValue( ) / 100 << "." <<
     combined.getValue( ) % 100 << endl;

PiggyBank other = combined - bank1;
cout << "other's value is: " << other.getValue( ) / 100 << "." <<
     other.getValue( ) % 100 << endl;
if (combined > other) {
  cout << "looks like combined is bigger..." << endl;
}
else {
  cout << "looks like other is bigger..." << endl;
}

if (bank1 > other) {
  cout << "looks like bank1 is bigger..." << endl;
}
else {
  cout << "looks like other is bigger..." << endl;
}
if (bank2 < bank1) {
  cout << "looks like bank2 is smaller..." << endl;
}
else {
  cout << "looks like bank1 is smaller..." << endl;
}
 

 
Thanks.

0
Comment
Question by:akohan
9 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 24292055
>>What is wrong with the following code?

The important part is missing ;o)
0
 

Author Comment

by:akohan
ID: 24292323

missing ?!
0
 
LVL 11

Expert Comment

by:DeepuAbrahamK
ID: 24292491
Could you please show your class PiggyBank and its function implementation?
Best Regards,
Deepu Abraham K
0
 

Author Comment

by:akohan
ID: 24292714

Sure,

#ifndef PIGGYBANK_H
#define PIGGYBANK_H

#include <iostream>
using namespace std;

class PiggyBank {
public:
      PiggyBank( );

      int  getValue( );
      void setSize( int size );
      int  getSize( );
      int  getCoins( );

      void feedCoins( int quarters, int dimes, int nickels, int pennies );
      int  breakOpen( );
private:
      int  myValue;           // the monetary value of this piggybank, in pennies
      int  myNumberOfCoins;   // the actual number of coins in this bank
      int  mySize;            // the most coins this bank can possibly hold
};

#endif

0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 11

Expert Comment

by:DeepuAbrahamK
ID: 24292801
You need to implement few functions:
copy constructor
+ operator overloading

0
 
LVL 11

Expert Comment

by:DeepuAbrahamK
ID: 24292802
- operator overloading aswell
0
 
LVL 11

Assisted Solution

by:DeepuAbrahamK
DeepuAbrahamK earned 50 total points
ID: 24292834
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 24293382
>> The program is not working after this line:

How is it not working ? What do you see, and what did you expect ?

Could you post all of the code ?
0
 
LVL 39

Accepted Solution

by:
itsmeandnobodyelse earned 75 total points
ID: 24298268
You would need to add something like

  friend PiggyBank operator+(const PiggyBank& pb1, const PiggyBank& pb2)
  {
      PiggyBank pb;
 
      ...
      return pb;
  }

to your class definition.

The ... should be replaced by code where you assign the members myValue, mySize, ... of pb with appropriate values computed from the input objects.

>>> You need to implement few functions:
>>> copy constructor
Copy constructor wasn't needed as the compiler will create one for you which simply makes a copy of the three int members what is good so.
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now