operator+ question in C++

Posted on 2009-05-03
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;


Question by:akohan
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 86

Expert Comment

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

The important part is missing ;o)

Author Comment

ID: 24292323

missing ?!
LVL 11

Expert Comment

ID: 24292491
Could you please show your class PiggyBank and its function implementation?
Best Regards,
Deepu Abraham K
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Author Comment

ID: 24292714



#include <iostream>
using namespace std;

class PiggyBank {
      PiggyBank( );

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

      void feedCoins( int quarters, int dimes, int nickels, int pennies );
      int  breakOpen( );
      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


LVL 11

Expert Comment

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

LVL 11

Expert Comment

ID: 24292802
- operator overloading aswell
LVL 11

Assisted Solution

DeepuAbrahamK earned 50 total points
ID: 24292834
LVL 53

Expert Comment

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 ?
LVL 39

Accepted Solution

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.

Featured Post

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

705 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