Solved

Operator overload +

Posted on 2004-03-26
8
312 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
  • 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
maxBlock challenge 30 128
splitOdd10 challenge 5 105
Need To Find FireFox Binary Path For Python Selenium On Windows 7 15 578
Re-position sub-options beneath the TAB 7 88
Navigation is an important part of web design from a usability perspective. But it is often a pain when it comes to a developer’s perspective. By navigation, it often means menuing. This is less theory and more practical of how to get a specific gro…
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

776 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