Question
In C++ Overload the operator < < for class InventoryItem. The overloaded operator will allow printing the content of an InventoryItem object following the example
In C++
Overload the operator << for class InventoryItem. The overloaded operator will allow printing the content of an InventoryItem object following the example format bellow:
Inventory Item: description=Mars chocolate bar, unit cost=1.09$, quantity is stock=5
InventoryItem.h
class InventoryItem
{
public:
static int number_articles;
// Constructor
InventoryItem(char *desc, double c, int u);
// Destructor
~InventoryItem();
const char *getDescription() const;
double getCost() const;
int getUnits() const;
static int getNumberArticles();
private:
char *description; // The item description
double cost; // The item cost
int units; // Number of units on hand
};
InventoryItem.cpp
#include "InventoryItem.h"
using namespace std;
InventoryItem::InventoryItem(char *desc, double c, int u)
{
description = desc;
cost = c;
units = u;
number_articles++;
}
InventoryItem::~InventoryItem()
{
}
const char *InventoryItem::getDescription() const
{
return description;
}
double InventoryItem::getCost() const
{
return cost;
}
int InventoryItem::getUnits() const
{
return units;
}
int InventoryItem::getNumberArticles(){
return number_articles;
}
test_main.cpp
#include "InventoryItem.h"
#include
using namespace std;
int InventoryItem::number_articles = 0;
int main()
{
char item[] = "Apples";
InventoryItem i1 = InventoryItem(item, 10.09, 5);
cout << "Item Details-";
cout << " Description: " << i1.getDescription();
cout << " Cost: " << i1.getCost();
cout << " Units: " << i1.getUnits();
cout<<" Number of articles: "< cout< return 0; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started