Solved

Operator overload +

Posted on 2004-03-26
8
315 Views
Last Modified: 2008-02-26
can someone please help me on operator overloading +
I have supplied the main file and the implementation file.  I am writing an application that records the items (books) sold and calculates/displays the totals (costprice and sellprice) and the average total sales for each day.  i'm not getting the results i'm expecting.  The problem is in the operator overload :
The out-put should be something like this:

report for monday, march 02, 2004
  Total Sales: $180
   Total Cost: $150
   Average: $15.00
report for tues...etc.,
In my main class i record the sales like this and then call the copy constructor:

StockItem text1("W. Savitch", "Absolute C++ 1/e",
"Addison/Wesley", "2002", 75, 90);
    StockItem text2("L. A. Robertson", "Simple Program Design 4/e",
"Thomson", "2003", 70, 80);
    StockItem text3("A. Silberschatz, P. B. Galvin, and G. Gagne",
"Operating System Concepts 6/e", "John Wiley & Sons", "2003", 85, 120);

    SalesRegister monday("Monday March 1, 2004", 0, 0, 0);
    cout << monday.getDescription() << endl;
    monday.RecordSale(text1);
    monday.RecordSale(text1);

// COPY CONSTRUCTOR
    SalesRegister::SalesRegister(const SalesRegister& regObject)
    :Description(regObject.getDescription()),TotalCostPrice(regObject.getTotalCostPrice()),
    TotalSellPrice(regObject.getTotalSellPrice()),
        ItemCount(regObject.getItemCount())
   {
   }
// Calling the copy constrcutor
SalesRegister total(monday + tuesday + wednesday + thursday + friday);

#include<iostream>
#include<iomanip>
#include<string>

   using namespace std;
   using std::ios;

#include "SalesRegister.h"
#include "StockItem.h"

// USER DEFINED CONSTRUCTOR
    SalesRegister::SalesRegister(string Description, int TotalCostPrice,int
    TotalSellPrice, int ItemCount)
   {
      this->setDescription(Description);
      this->setTotalCostPrice(TotalCostPrice);
      this->setTotalSellPrice(TotalSellPrice);
      this->setItemCount(ItemCount);
   
   // INITIALISE PRIVATE MEMBERS TO DEFAULT VALUES
      Description = "";
      TotalCostPrice = 0;
      TotalSellPrice = 0;
      ItemCount = 0;
   }

// COPY CONSTRUCTOR
    SalesRegister::SalesRegister(const SalesRegister& regObject)
    :Description(regObject.getDescription()),TotalCostPrice(regObject.getTotalCostPrice()),
    TotalSellPrice(regObject.getTotalSellPrice()),
        ItemCount(regObject.getItemCount())
   {
   }

// OPERATOR OVERLOADING  + ADDITION
    SalesRegister SalesRegister::operator+(const SalesRegister &temp)
   {
      SalesRegister tmp(*this);
   // PROBLEM HERE
      TotalCostPrice = TotalCostPrice + temp.TotalCostPrice;
      TotalSellPrice = TotalSellPrice + temp.TotalSellPrice;
      temp.ItemCount + 1; // item count for average
   
      return(tmp);
   
   }
// ACCESSOR & MUTATOR FUNCTIONS

    string SalesRegister::getDescription() const
   {
      return(this->Description);
   }

    int SalesRegister::getTotalCostPrice() const
   {
      return(this->TotalCostPrice);
   }

    int SalesRegister::getTotalSellPrice() const
   {
      return(this->TotalSellPrice);
   }

    int SalesRegister::getItemCount() const
   {
      return(this->ItemCount);
   }

    string SalesRegister::setDescription(string Description)
   {
      return(this->Description = Description);
   }

    int SalesRegister::setTotalCostPrice(int TotalCostPrice)
   {
      return(this->TotalCostPrice = TotalCostPrice);
   }

    int SalesRegister::setTotalSellPrice(int TotalSellPrice)
   {
      return(this->TotalSellPrice = TotalSellPrice);
   }

    int SalesRegister::setItemCount(int ItemCount)
   {
      return(this->ItemCount = ItemCount);
   }

    void SalesRegister::RecordSale(StockItem Item)
   {
   
      cout << "\t" << Item.getSellPrice() << "\t" << Item.getAuthor() << ", " 
         << Item.getPublisher() << ", " << Item.getYear() << endl;
   
      TotalCostPrice = Item.getCostPrice();
      TotalSellPrice = Item.getSellPrice();
   
   }

// PRINT THE REPORT
    void SalesRegister::PrintReport()
   {
      cout << endl;
      cout << "Report for " << getDescription() << endl;
      cout << "\t" << "Total Sales: " << "\t$" << getTotalSellPrice() << endl;
      cout << "\t" << "Total Cost: " << "\t$" << getTotalCostPrice() << endl;
   
      float i = TotalSellPrice - TotalCostPrice;
   
      cout.setf(ios::fixed);
      cout.setf(ios::showpoint);
   // Avergage & ItemCount under construction
   //cout << "\t" << "Average Profit: " << "$" << setprecision(2) << i << endl;
   }

// DESTRUCTOR
    SalesRegister::~SalesRegister()
   {
   }


#include <iostream>
#include <string>

using namespace std;

#include "SalesRegister.h"
#include "StockItem.h"

int main()
{
    StockItem text1("W. Savitch", "Absolute C++ 1/e",
"Addison/Wesley", "2002", 75, 90);
    StockItem text2("L. A. Robertson", "Simple Program Design 4/e",
"Thomson", "2003", 70, 80);
    StockItem text3("A. Silberschatz, P. B. Galvin, and G. Gagne",
"Operating System Concepts 6/e", "John Wiley & Sons", "2003", 85, 120);

    SalesRegister monday("Monday March 1, 2004", 0, 0, 0);
    cout << monday.getDescription() << endl;
    monday.RecordSale(text1);
    monday.RecordSale(text1);

    SalesRegister tuesday("Tuesday March 2, 2004", 0, 0, 0);
    cout << endl << tuesday.getDescription() << endl;
    tuesday.RecordSale(text3);
    tuesday.RecordSale(text2);
    tuesday.RecordSale(text1);

    SalesRegister wednesday("Wednesday March 3, 2004", 0, 0, 0);
    cout << endl << wednesday.getDescription() << endl;
    wednesday.RecordSale(text1);
    wednesday.RecordSale(text3);

    SalesRegister thursday("Thursday March 4, 2004", 0, 0, 0);
    cout << endl << thursday.getDescription() << endl;
    thursday.RecordSale(text3);
    thursday.RecordSale(text2);

    SalesRegister friday("Friday March 5, 2004", 0, 0, 0);
    cout << endl << friday.getDescription() << endl;
    friday.RecordSale(text1);
    friday.RecordSale(text2);

    SalesRegister total(monday + tuesday + wednesday + thursday + friday);
    total.setDescription("Total Sales");

    monday.PrintReport();
    tuesday.PrintReport();
    wednesday.PrintReport();
    thursday.PrintReport();
    friday.PrintReport();
    total.PrintReport();

    return(0);
}


0
Comment
Question by:gbilios
[X]
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
  • 3
  • 3
  • 2
8 Comments
 
LVL 9

Accepted Solution

by:
_ys_ earned 20 total points
ID: 10693883
Your operator + overload never changes 'tmp' - the final instance you return. It does in fact alter 'this'.

SalesRegister SalesRegister::operator+(const SalesRegister &temp)
{
    SalesRegister tmp(*this);
   // PROBLEM HERE
    tmp.TotalCostPrice += temp.TotalCostPrice;
    tmp.TotalSellPrice += temp.TotalSellPrice;

// not sure about this ??
    temp.ItemCount + 1; // item count for average

// did you mean ...
    tmp.ItemCount += temp.ItemCount;
   
    return(tmp);
}

You'll realise that everything is just tmp += temp. As such the canonical format of an implementation is:

const SalesRegister SalesRegister::operator+(const SalesRegister &temp)
{
    SalesRegister tmp(*this);
    tmp += temp;
    return(tmp);
}

Reusing operator+= avoids any unexpected results, and ensures consistency among your operators' custom implemtations.
0
 

Author Comment

by:gbilios
ID: 10694143
Thanks for your help.  i worked out the problem.  The one thing remaining is the total sales and cost for the whole week.  

In my main application i call the copy constructor:

SalesRegister total(monday + tuesday + wednesday + thursday + friday);

below is the copy constructor:

// COPY CONSTRUCTOR
    SalesRegister::SalesRegister(const SalesRegister& regObject)
    :Description(regObject.getDescription()),TotalCostPrice(regObject.getTotalCostPrice()),
    TotalSellPrice(regObject.getTotalSellPrice()),
        ItemCount(regObject.getItemCount())
   {


     
   }


0
 
LVL 9

Expert Comment

by:_ys_
ID: 10694250
The copy constructor looks sound, and is already being used from within operator+.

You'll need to verify the output from operator+. I believe that's the source of your problem. Perform a simple monday + tuesday and verify the results.
0
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 

Author Comment

by:gbilios
ID: 10694287
I'm new to programming so it'll take me a while to get everything working in order.
I can't have the code:
tmp += temp;  //I GET A COMPILE ERROR

Do i need to include anything else in the copy constructor?
i modified the code to:

SalesRegister SalesRegister::operator+(const SalesRegister &temp)
{
   SalesRegister tmp(*this);

    tmp.TotalCostPrice + temp.TotalCostPrice;
   tmp.TotalSellPrice + temp.TotalSellPrice;
    tmp.ItemCount + temp.ItemCount;
return(tmp);
}

void RecordSale(StockItem Item)
{
  TotalCostPrice = TotalCostPrice + Item.getCostPrice();
  TotalSellPrice = TotalSellPrice + Item.getSellPrice();
ItemCount++;
0
 

Author Comment

by:gbilios
ID: 10694306
no wonder it aint working, stupid me!!

the code in the operator should've read:

tmp.TotalSellPrice = tmp.TotalSellPrice + temp.TotalSellPrice;
tmp.TotalCostPrice = tmp.TotalCostPrice + temp.TotalCostPrice;

//What was happening when the call to the constructor in the main class:
SalesRegister total(monday + tuesday + wednesday + thursday + friday);

would only get the first instance of the totalcost/totalsell price for monday

everythings okay!!
THANK-YOU!!
0
 
LVL 9

Expert Comment

by:_ys_
ID: 10702747
>> tmp += temp;
This requires you to overload operator+= with your own implementation. Not absolutely necessary, just good pratice.
0
 
LVL 20

Expert Comment

by:Venabili
ID: 10732394
gbilios,

Can you please accept the answer of the expert that helped you? Thanks

Venabili
Programming Languages Page Editor
0
 
LVL 20

Expert Comment

by:Venabili
ID: 10732566
Why did you accepted my answer? I will reopen and accept the answer of the expert

Venabili
0

Featured Post

[Webinar] 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

There is an easy way, in .NET, to centralize the treatment of all unexpected errors. First of all, instead of launching the application directly in a Form, you need first to write a Sub called Main, in a module. Then, set the Startup Object to th…
This article will show, step by step, how to integrate R code into a R Sweave document
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

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