C++/Operator Overloading. Hello, if anyone would be able to help me with this pr
ID: 3683882 • Letter: C
Question
C++/Operator Overloading. Hello, if anyone would be able to help me with this problem it would be greatly appreciated. I am mainly having a difficult with the =, ++, --. Thank you in advance. Create a new project an include the following lab8b_inventory.h and lab8b_inventory.cpp file.
class Lab8B_Inventory {
private: int units;
public: Lab8B_Inventory()
{
units=0;
}
Lab8B_Inventory(int u)
{
units=u;
}
int getUnits() const
{ return units;}
};
#include < iostream >
#include < string >
#include "Lab8B_Inventory.h"
using namespace std;
int main()
{
Lab8B_Inventory store1(200);
Lab8B_Inventory store2(100);
Lab8B_Inventory total;
total=store1+store2;
cout << "Store 1:" << store1.getUnits() << endl;
cout << "Store 2:" << store2.getUnits() << endl;
cout << "Store Total: " << total.getUnits() << endl;
system ("PAUSE");
return 0;
}
Include the following binary operator overload function to handle the + or addition of two inventory objects.
Lab8B_Inventory operator+(const Lab8B_Inventory &right)
{
int totalUnits = units + right.units;;
Lab8B_Inventory temp(totalUnits);
return temp;
}
Next include overload operators for =, ++, --, and -. Include some code in Lab8B_Inventory.cpp to test these functions. The output as follows:
Addition
Store 1: 200
Store 2: 100
Store Total:300
Subtraction
Store 1: 200
Store 2: 100
Store Total: 100
Increment
Store 1:201
Store 2: 101
Store Total: 201
Decrement
Store 1: 200
Store 2: 100
Store Total: 200
Assignment
Store 1: 200
Store 2: 200
Store Total: 200
Explanation / Answer
############# 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;
}
void operator++ ()
{
++units; // increment this object
//return Lab8B_Inventory(units);
}
void operator-- ()
{
--units; // increment this object
//return Lab8B_Inventory(units);
}
};
################ 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;
total=store1+store2;
cout << "Store 1:" << store1.getUnits() << endl;
cout << "Store 2:" << store2.getUnits() << endl;
cout << "Store Total: " << total.getUnits() << endl;
//system ("PAUSE");
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.