Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ (Intro to Programming 2). Hello! I was wondering if someone would be able to

ID: 3688993 • Letter: C

Question

C++ (Intro to Programming 2). Hello! I was wondering if someone would be able to help with this problem. The code that is listed below complies the only thing I need help figuring out is the overload operators. I have to add the overload operators for =, ++, and --. I'm also not sure how to display the overload operators in the .cpp. Any help would be greatly appreciated, thank you!

Lab8B_Inventory.h

class Lab8B_Inventory
{

private:
   int units;

public:
   Lab8B_Inventory()
   {
       units = 0;
   }
   Lab8B_Inventory(int u)
   {
       units = u;
   }
   int getUnits() const
   {
       return units;
   }
   Lab8B_Inventory operator+(const Lab8B_Inventory &right)
   {
       int totalUnits = units + right.units;
       Lab8B_Inventory temp(totalUnits);
       return temp;
   }
   Lab8B_Inventory operator-(const Lab8B_Inventory &right)
   {
       int totalUnits = units - right.units;
       Lab8B_Inventory temp(totalUnits);
       return temp;
   }
};

Lab8B_Inventory.cpp

#include <iostream>
#include <string>
#include "Lab8B_Inventory.h"
using namespace std;

int main()
{
   Lab8B_Inventory store1(200);
   Lab8B_Inventory store2(100);
   Lab8B_Inventory total;
   Lab8B_Inventory subTotal = (store1 - store2);

   total = store1 + store2;

   cout << "Mikayla Webber's Operator Overloads" << endl;

   cout << "Addition" << endl;
   cout << "Store 1: " << store1.getUnits() << endl;
   cout << "Store 2: " << store2.getUnits() << endl;
   cout << "Store Total: " << total.getUnits() << endl;

   cout << "Subtraction" << endl;
   cout << "Store 1: " << store1.getUnits() << endl;
   cout << "Store 2: " << store2.getUnits() << endl;
   cout << "Store Total: " << subTotal.getUnits() << endl;

   system("PAUSE"); //you need this for windows system
   return 0;
}

Explanation / Answer

#include <iostream>
using namespace std;

//Lab8B_Inventory.h
class Lab8B_Inventory
{
private:
int units;
public:
Lab8B_Inventory()
{
units = 0;
}
Lab8B_Inventory(int u)
{
units = u;
}
int getUnits() const
{
return units;
}
void setUnits( int i )
{
units = i;
}
  
  
Lab8B_Inventory operator+(const Lab8B_Inventory &right)
{
int totalUnits = units + right.units;
Lab8B_Inventory temp(totalUnits);
return temp;
}
Lab8B_Inventory operator-(const Lab8B_Inventory &right)
{
int totalUnits = units - right.units;
Lab8B_Inventory temp(totalUnits);
return temp;
}
};

int main()
{
int tunits;
int subunits;
Lab8B_Inventory store1(200);
Lab8B_Inventory store2(100);
Lab8B_Inventory total;
Lab8B_Inventory subTotal = (store1 - store2);
Lab8B_Inventory sub ;
cout << "Mikayla Webber's Operator Overloads" << endl;
cout << "Addition" << endl;
cout << "Store 1: " << store1.getUnits() << endl;
cout << "Store 2: " << store2.getUnits() << endl;
tunits = store1.getUnits() + store2.getUnits();
total.setUnits(tunits) ;
cout << " Store Total: " << total.getUnits() << endl;
cout << "Subtraction" << endl;
cout << "Store 1: " << store1.getUnits() << endl;
cout << "Store 2: " << store2.getUnits() << endl;
subunits = store1.getUnits() - store2.getUnits();
sub.setUnits(subunits) ;
cout << "Store Subtraction: " << sub.getUnits() << endl;
//getch()); //you need this for windows system
return 0;
}

//____

// you can use friend functions whe we use three objects