Hello, please help with the following, Thank you. Modify your program to overloa
ID: 3866433 • Letter: H
Question
Hello, please help with the following, Thank you.
Modify your program to overload the insertion (<<) and extraction (>>) operators in classInventoryType.
Submission instructions:
1. Submit screenshots demoing your program for both operators.
2. Submit a copy of the class definition and your test program (function main() ) in a text file.
3. Submit a copy of the input file used to test program.
************************************************************This is my program***************************
myClient.cpp
#include <iostream>
#include "InventoryItem.h"
using namespace std;
int main()
{
InventoryItem item;
item.setDescription("Item1");
cout << "Description obtained: " << item.getDescription() << endl;
item.setQuantityOnHand(10);
cout << "Quantity obtained: " << item.getQuantityOnHand() << endl;
InventoryItem item2("Item2", 20);
cout << "Description obtained: " << item2.getDescription() << endl;
cout << "Quantity obtained: " << item2.getQuantityOnHand() << endl;
system("pause");
return 0;
}
InventoryItem.h
#ifndef INVENTORYITEM_H
#define INVENTORYITEM_H
#include <string>
using namespace std;
class InventoryItem
{
private:
string description;
int quantityOnHand;
public:
InventoryItem();
InventoryItem(string, int q);
string getDescription();
int getQuantityOnHand();
void setDescription(string);
void setQuantityOnHand(int);
};
#endif
InventoryItem.cpp
#include <iostream>
#include <iomanip>
#include <string>
#include <cctype>
#include "InventoryItem.h"
using namespace std;
InventoryItem::InventoryItem()
{
}
InventoryItem::InventoryItem(string d, int q)
{
description = d;
if (q < 0) q = 0;
quantityOnHand = q;
}
string InventoryItem::getDescription() {
return description;
}
Explanation / Answer
Given below is the code with the operators << and >> implemented. In the code given in the question, the setDescription() , setQuantityOnHand() and getQuantityOnHand() were missing in the cpp file. So added those too. The client file is modified to use the << and >> for I/O. Please do rate the answer if it helped. Thank you.
InventoryItem.h
#ifndef INVENTORYITEM_H
#define INVENTORYITEM_H
#include <iostream>
using namespace std;
class InventoryItem
{
private:
string description;
int quantityOnHand;
public:
InventoryItem();
InventoryItem(string, int q);
string getDescription();
int getQuantityOnHand();
void setDescription(string);
void setQuantityOnHand(int);
friend istream& operator >> (istream &in, InventoryItem &item);
friend ostream& operator << (ostream &os, InventoryItem &item);
};
#endif
InventoryItem.cpp
#include <iostream>
#include <iomanip>
#include <string>
#include <cctype>
#include "InventoryItem.h"
using namespace std;
InventoryItem::InventoryItem()
{
}
InventoryItem::InventoryItem(string d, int q)
{
description = d;
if (q < 0) q = 0;
quantityOnHand = q;
}
string InventoryItem::getDescription() {
return description;
}
int InventoryItem::getQuantityOnHand()
{
return quantityOnHand;
}
void InventoryItem::setDescription(string desc) {
description = desc;
}
void InventoryItem::setQuantityOnHand(int q) {
if(q < 0) q = 0;
quantityOnHand = q;
}
istream& operator >> (istream &in, InventoryItem &item)
{
string desc;
int qty;
in >> desc;
in >> qty;
item.setDescription(desc);
item.setQuantityOnHand(qty);
return in;
}
ostream& operator << (ostream &out, InventoryItem &item)
{
out << "Description obtained: " << item.getDescription() << endl;
out << "Quantity obtained: " << item.getQuantityOnHand() << endl;
return out;
}
myClient.cpp
#include <iostream>
#include <fstream>
#include "InventoryItem.h"
using namespace std;
int main()
{
InventoryItem item;
//using set functions
item.setDescription("Item1");
item.setQuantityOnHand(10);
cout << item << endl; //using << operator
//using parametrizied constructor
InventoryItem item2("Item2", 20);
cout << item2 << endl; //using << operator
//reading from inventory.txt file
string filename;
cout << "Enter input filename containing inventory data: ";
cin >> filename;
ifstream infile(filename);
if(!infile.is_open())
{
cout << "Could not read input file " << filename << endl;
return 1;
}
InventoryItem item4;
cout << "Reading data from file using >> .... " << endl;
while(infile >> item4)
cout << item4 << endl;
infile.close();
return 0;
}
inputfile : inventory.txt
bag 5
shoes 10
shirt 25
output
Description obtained: Item1
Quantity obtained: 10
Description obtained: Item2
Quantity obtained: 20
Enter input filename containing inventory data: inventory.txt
Reading data from file using >> ....
Description obtained: bag
Quantity obtained: 5
Description obtained: shoes
Quantity obtained: 10
Description obtained: shirt
Quantity obtained: 25
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.