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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!