SlideShare a Scribd company logo
1 of 5
Download to read offline
In C++
For this following code please separate it into 5 different source codes. The code is already given
to you.
main.cpp main
Grocery.hpp class definition
Grocery.cpp class definition for any member functions
Phistory.hpp class definition
Phistory.cpp class definition for any member functions
#include
#include
#include
using namespace std;
// Define the PriceHistory class
class PriceHistory {
private:
string date; // Date in YYYY-MM-DD format
float price;
public:
// Default constructor
PriceHistory() {
date = "";
price = 0.0;
}
// Parameterized constructor
PriceHistory(string d, float p) {
date = d;
price = p;
}
// Getter for date
string getDate() const {
return date;
}
// Getter for price
float getPrice() const {
return price;
}
};
// Define the Grocery class
class Grocery {
private:
string upc;
string description;
float cost;
float sellingPrice;
int inventory;
char status;
PriceHistory priceHistory[25];
int numHistories;
public:
static int numGroceries;
// Default constructor
Grocery() {
upc = "";
description = "";
cost = 0.0;
sellingPrice = 0.0;
inventory = 0;
status = ' ';
numHistories = 0;
}
// Parameterized constructor
Grocery(string u, string d, float c, float s, int i, char st) {
upc = u;
description = d;
cost = c;
sellingPrice = s;
inventory = i;
status = st;
numHistories = 0;
}
// Getter for upc
string getUPC() const {
return upc;
}
// Getter for description
string getDescription() const {
return description;
}
// Getter for cost
float getCost() const {
return cost;
}
// Getter for selling price
float getSellingPrice() const {
return sellingPrice;
}
// Getter for inventory
int getInventory() const {
return inventory;
}
// Getter for status
char getStatus() const {
return status;
}
// Getter for numHistories
int getNumHistories() const {
return numHistories;
}
// Add a new price history
void addPriceHistory(string date, float price) {
if (numHistories < 25) {
priceHistory[numHistories++] = PriceHistory(date, price);
}
}
// Display the grocery item
void display() const {
cout << upc << "t" << description << "t" << cost << "t" << sellingPrice << "t" <<
inventory << "t" << status << endl;
}
};
int Grocery::numGroceries = 0;
int main() {
const int MAX_GROCERIES = 100;
Grocery groceries[MAX_GROCERIES];
// Open the input file
ifstream infile("a4data.txt");
if (!infile.is_open()) {
cout << "Error opening input file." << endl;
return 1;
}
// Read the input file
string line;
while (getline(infile, line)) {
string upc = line.substr(0, 6);
string description = line.substr(6, 20);
float cost = stof(line.substr(26, 6));
float sellingPrice = stof(line.substr(32, 6));
int inventory = stoi(line.substr(38, 3));
char status = line[41];
// Add the grocery item to the array
if (Grocery::numGroceries < MAX_GROCERIES) {
groceries[Grocery::numGroceries++] = Grocery(upc, description, cost, sellingPrice,
inventory, status);
}
}
// Print the grocery items
cout <<
"=================================================================" <<
endl;
cout << "UPCtDescriptionttCosttPricetInventorytStatus" << endl;
cout <<
"=================================================================" <<
endl;
for (int i = 0; i < Grocery::numGroceries; i++) {
groceries[i].display();
}
return 0;
}

More Related Content

Similar to In C++For this following code please separate it into 5 different .pdf

C#i need help creating the instance of stream reader to read from .pdf
C#i need help creating the instance of stream reader to read from .pdfC#i need help creating the instance of stream reader to read from .pdf
C#i need help creating the instance of stream reader to read from .pdfajantha11
 
#include iostream #includeData.h #includePerson.h#in.pdf
#include iostream #includeData.h #includePerson.h#in.pdf#include iostream #includeData.h #includePerson.h#in.pdf
#include iostream #includeData.h #includePerson.h#in.pdfannucommunication1
 
The Ring programming language version 1.7 book - Part 87 of 196
The Ring programming language version 1.7 book - Part 87 of 196The Ring programming language version 1.7 book - Part 87 of 196
The Ring programming language version 1.7 book - Part 87 of 196Mahmoud Samir Fayed
 
Relay Modern: architecture and workflow
Relay Modern: architecture and workflowRelay Modern: architecture and workflow
Relay Modern: architecture and workflowAlex Alexeev
 
#ifndef CRYPTO_HPP#define CRYPTO_HPP#include functional#.docx
#ifndef CRYPTO_HPP#define CRYPTO_HPP#include functional#.docx#ifndef CRYPTO_HPP#define CRYPTO_HPP#include functional#.docx
#ifndef CRYPTO_HPP#define CRYPTO_HPP#include functional#.docxgertrudebellgrove
 
12.9 Program Online shopping cart (continued) (C++)This program e.pdf
12.9 Program Online shopping cart (continued) (C++)This program e.pdf12.9 Program Online shopping cart (continued) (C++)This program e.pdf
12.9 Program Online shopping cart (continued) (C++)This program e.pdffasttracksunglass
 
struct procedure {    Date dateOfProcedure;    int procedureID.pdf
struct procedure {    Date dateOfProcedure;    int procedureID.pdfstruct procedure {    Date dateOfProcedure;    int procedureID.pdf
struct procedure {    Date dateOfProcedure;    int procedureID.pdfanonaeon
 
VISUALIZAR REGISTROS EN UN JTABLE
VISUALIZAR REGISTROS EN UN JTABLEVISUALIZAR REGISTROS EN UN JTABLE
VISUALIZAR REGISTROS EN UN JTABLEDarwin Durand
 
Please help me understand why when I run my code it does not make me.pdf
Please help me understand why when I run my code it does not make me.pdfPlease help me understand why when I run my code it does not make me.pdf
Please help me understand why when I run my code it does not make me.pdfrmwaterlife
 
C++ help finish my code Topics class definitions, arrays of objec.pdf
C++ help finish my code Topics class definitions, arrays of objec.pdfC++ help finish my code Topics class definitions, arrays of objec.pdf
C++ help finish my code Topics class definitions, arrays of objec.pdfinfo189835
 
重構—改善既有程式的設計(chapter 8)part 2
重構—改善既有程式的設計(chapter 8)part 2重構—改善既有程式的設計(chapter 8)part 2
重構—改善既有程式的設計(chapter 8)part 2Chris Huang
 
Writing Go(od) Tests (FOSDEM 2020)
Writing Go(od) Tests (FOSDEM 2020)Writing Go(od) Tests (FOSDEM 2020)
Writing Go(od) Tests (FOSDEM 2020)Nikki Attea
 
Paradigmas de Linguagens de Programacao - Aula #4
Paradigmas de Linguagens de Programacao - Aula #4Paradigmas de Linguagens de Programacao - Aula #4
Paradigmas de Linguagens de Programacao - Aula #4Ismar Silveira
 
Core csharp and net quick reference
Core csharp and net quick referenceCore csharp and net quick reference
Core csharp and net quick referenceilesh raval
 
Core c sharp and .net quick reference
Core c sharp and .net quick referenceCore c sharp and .net quick reference
Core c sharp and .net quick referenceArduino Aficionado
 

Similar to In C++For this following code please separate it into 5 different .pdf (18)

C#i need help creating the instance of stream reader to read from .pdf
C#i need help creating the instance of stream reader to read from .pdfC#i need help creating the instance of stream reader to read from .pdf
C#i need help creating the instance of stream reader to read from .pdf
 
#include iostream #includeData.h #includePerson.h#in.pdf
#include iostream #includeData.h #includePerson.h#in.pdf#include iostream #includeData.h #includePerson.h#in.pdf
#include iostream #includeData.h #includePerson.h#in.pdf
 
Penjualan swalayan
Penjualan swalayanPenjualan swalayan
Penjualan swalayan
 
Virtual inheritance
Virtual inheritanceVirtual inheritance
Virtual inheritance
 
Functional C++
Functional C++Functional C++
Functional C++
 
The Ring programming language version 1.7 book - Part 87 of 196
The Ring programming language version 1.7 book - Part 87 of 196The Ring programming language version 1.7 book - Part 87 of 196
The Ring programming language version 1.7 book - Part 87 of 196
 
Relay Modern: architecture and workflow
Relay Modern: architecture and workflowRelay Modern: architecture and workflow
Relay Modern: architecture and workflow
 
#ifndef CRYPTO_HPP#define CRYPTO_HPP#include functional#.docx
#ifndef CRYPTO_HPP#define CRYPTO_HPP#include functional#.docx#ifndef CRYPTO_HPP#define CRYPTO_HPP#include functional#.docx
#ifndef CRYPTO_HPP#define CRYPTO_HPP#include functional#.docx
 
12.9 Program Online shopping cart (continued) (C++)This program e.pdf
12.9 Program Online shopping cart (continued) (C++)This program e.pdf12.9 Program Online shopping cart (continued) (C++)This program e.pdf
12.9 Program Online shopping cart (continued) (C++)This program e.pdf
 
struct procedure {    Date dateOfProcedure;    int procedureID.pdf
struct procedure {    Date dateOfProcedure;    int procedureID.pdfstruct procedure {    Date dateOfProcedure;    int procedureID.pdf
struct procedure {    Date dateOfProcedure;    int procedureID.pdf
 
VISUALIZAR REGISTROS EN UN JTABLE
VISUALIZAR REGISTROS EN UN JTABLEVISUALIZAR REGISTROS EN UN JTABLE
VISUALIZAR REGISTROS EN UN JTABLE
 
Please help me understand why when I run my code it does not make me.pdf
Please help me understand why when I run my code it does not make me.pdfPlease help me understand why when I run my code it does not make me.pdf
Please help me understand why when I run my code it does not make me.pdf
 
C++ help finish my code Topics class definitions, arrays of objec.pdf
C++ help finish my code Topics class definitions, arrays of objec.pdfC++ help finish my code Topics class definitions, arrays of objec.pdf
C++ help finish my code Topics class definitions, arrays of objec.pdf
 
重構—改善既有程式的設計(chapter 8)part 2
重構—改善既有程式的設計(chapter 8)part 2重構—改善既有程式的設計(chapter 8)part 2
重構—改善既有程式的設計(chapter 8)part 2
 
Writing Go(od) Tests (FOSDEM 2020)
Writing Go(od) Tests (FOSDEM 2020)Writing Go(od) Tests (FOSDEM 2020)
Writing Go(od) Tests (FOSDEM 2020)
 
Paradigmas de Linguagens de Programacao - Aula #4
Paradigmas de Linguagens de Programacao - Aula #4Paradigmas de Linguagens de Programacao - Aula #4
Paradigmas de Linguagens de Programacao - Aula #4
 
Core csharp and net quick reference
Core csharp and net quick referenceCore csharp and net quick reference
Core csharp and net quick reference
 
Core c sharp and .net quick reference
Core c sharp and .net quick referenceCore c sharp and .net quick reference
Core c sharp and .net quick reference
 

More from siva009113

In Chapter 2, a discussion of how social media influences the politi.pdf
In Chapter 2, a discussion of how social media influences the politi.pdfIn Chapter 2, a discussion of how social media influences the politi.pdf
In Chapter 2, a discussion of how social media influences the politi.pdfsiva009113
 
In March 2019, Zambia, a small open economy, lifted its export ban o.pdf
In March 2019, Zambia, a small open economy, lifted its export ban o.pdfIn March 2019, Zambia, a small open economy, lifted its export ban o.pdf
In March 2019, Zambia, a small open economy, lifted its export ban o.pdfsiva009113
 
in JavsScript we have folders and files. Which folder would these tw.pdf
in JavsScript we have folders and files. Which folder would these tw.pdfin JavsScript we have folders and files. Which folder would these tw.pdf
in JavsScript we have folders and files. Which folder would these tw.pdfsiva009113
 
IN JAVA PLEASEmake sure of the variables are correct and the s.pdf
IN JAVA PLEASEmake sure of the variables are correct and the s.pdfIN JAVA PLEASEmake sure of the variables are correct and the s.pdf
IN JAVA PLEASEmake sure of the variables are correct and the s.pdfsiva009113
 
IN C CODEGiven an IntNode struct and the operating functions for a.pdf
IN C CODEGiven an IntNode struct and the operating functions for a.pdfIN C CODEGiven an IntNode struct and the operating functions for a.pdf
IN C CODEGiven an IntNode struct and the operating functions for a.pdfsiva009113
 
In an agricultural experiment there are 25 varieties of maize, label.pdf
In an agricultural experiment there are 25 varieties of maize, label.pdfIn an agricultural experiment there are 25 varieties of maize, label.pdf
In an agricultural experiment there are 25 varieties of maize, label.pdfsiva009113
 

More from siva009113 (6)

In Chapter 2, a discussion of how social media influences the politi.pdf
In Chapter 2, a discussion of how social media influences the politi.pdfIn Chapter 2, a discussion of how social media influences the politi.pdf
In Chapter 2, a discussion of how social media influences the politi.pdf
 
In March 2019, Zambia, a small open economy, lifted its export ban o.pdf
In March 2019, Zambia, a small open economy, lifted its export ban o.pdfIn March 2019, Zambia, a small open economy, lifted its export ban o.pdf
In March 2019, Zambia, a small open economy, lifted its export ban o.pdf
 
in JavsScript we have folders and files. Which folder would these tw.pdf
in JavsScript we have folders and files. Which folder would these tw.pdfin JavsScript we have folders and files. Which folder would these tw.pdf
in JavsScript we have folders and files. Which folder would these tw.pdf
 
IN JAVA PLEASEmake sure of the variables are correct and the s.pdf
IN JAVA PLEASEmake sure of the variables are correct and the s.pdfIN JAVA PLEASEmake sure of the variables are correct and the s.pdf
IN JAVA PLEASEmake sure of the variables are correct and the s.pdf
 
IN C CODEGiven an IntNode struct and the operating functions for a.pdf
IN C CODEGiven an IntNode struct and the operating functions for a.pdfIN C CODEGiven an IntNode struct and the operating functions for a.pdf
IN C CODEGiven an IntNode struct and the operating functions for a.pdf
 
In an agricultural experiment there are 25 varieties of maize, label.pdf
In an agricultural experiment there are 25 varieties of maize, label.pdfIn an agricultural experiment there are 25 varieties of maize, label.pdf
In an agricultural experiment there are 25 varieties of maize, label.pdf
 

Recently uploaded

Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...anjaliyadav012327
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDThiyagu K
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpinRaunakKeshri1
 
Russian Call Girls in Andheri Airport Mumbai WhatsApp 9167673311 💞 Full Nigh...
Russian Call Girls in Andheri Airport Mumbai WhatsApp  9167673311 💞 Full Nigh...Russian Call Girls in Andheri Airport Mumbai WhatsApp  9167673311 💞 Full Nigh...
Russian Call Girls in Andheri Airport Mumbai WhatsApp 9167673311 💞 Full Nigh...Pooja Nehwal
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajanpragatimahajan3
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAssociation for Project Management
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesFatimaKhan178732
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
The byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptxThe byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptxShobhayan Kirtania
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 

Recently uploaded (20)

Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
 
Russian Call Girls in Andheri Airport Mumbai WhatsApp 9167673311 💞 Full Nigh...
Russian Call Girls in Andheri Airport Mumbai WhatsApp  9167673311 💞 Full Nigh...Russian Call Girls in Andheri Airport Mumbai WhatsApp  9167673311 💞 Full Nigh...
Russian Call Girls in Andheri Airport Mumbai WhatsApp 9167673311 💞 Full Nigh...
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajan
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and Actinides
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
The byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptxThe byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptx
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 

In C++For this following code please separate it into 5 different .pdf

  • 1. In C++ For this following code please separate it into 5 different source codes. The code is already given to you. main.cpp main Grocery.hpp class definition Grocery.cpp class definition for any member functions Phistory.hpp class definition Phistory.cpp class definition for any member functions #include #include #include using namespace std; // Define the PriceHistory class class PriceHistory { private: string date; // Date in YYYY-MM-DD format float price; public: // Default constructor PriceHistory() { date = ""; price = 0.0; } // Parameterized constructor PriceHistory(string d, float p) { date = d; price = p; } // Getter for date string getDate() const { return date; } // Getter for price float getPrice() const { return price;
  • 2. } }; // Define the Grocery class class Grocery { private: string upc; string description; float cost; float sellingPrice; int inventory; char status; PriceHistory priceHistory[25]; int numHistories; public: static int numGroceries; // Default constructor Grocery() { upc = ""; description = ""; cost = 0.0; sellingPrice = 0.0; inventory = 0; status = ' '; numHistories = 0; } // Parameterized constructor Grocery(string u, string d, float c, float s, int i, char st) { upc = u; description = d; cost = c; sellingPrice = s; inventory = i; status = st; numHistories = 0; } // Getter for upc
  • 3. string getUPC() const { return upc; } // Getter for description string getDescription() const { return description; } // Getter for cost float getCost() const { return cost; } // Getter for selling price float getSellingPrice() const { return sellingPrice; } // Getter for inventory int getInventory() const { return inventory; } // Getter for status char getStatus() const { return status; } // Getter for numHistories int getNumHistories() const { return numHistories; } // Add a new price history void addPriceHistory(string date, float price) { if (numHistories < 25) { priceHistory[numHistories++] = PriceHistory(date, price); } } // Display the grocery item void display() const { cout << upc << "t" << description << "t" << cost << "t" << sellingPrice << "t" <<
  • 4. inventory << "t" << status << endl; } }; int Grocery::numGroceries = 0; int main() { const int MAX_GROCERIES = 100; Grocery groceries[MAX_GROCERIES]; // Open the input file ifstream infile("a4data.txt"); if (!infile.is_open()) { cout << "Error opening input file." << endl; return 1; } // Read the input file string line; while (getline(infile, line)) { string upc = line.substr(0, 6); string description = line.substr(6, 20); float cost = stof(line.substr(26, 6)); float sellingPrice = stof(line.substr(32, 6)); int inventory = stoi(line.substr(38, 3)); char status = line[41]; // Add the grocery item to the array if (Grocery::numGroceries < MAX_GROCERIES) { groceries[Grocery::numGroceries++] = Grocery(upc, description, cost, sellingPrice, inventory, status); } } // Print the grocery items cout << "=================================================================" << endl; cout << "UPCtDescriptionttCosttPricetInventorytStatus" << endl; cout << "=================================================================" << endl;
  • 5. for (int i = 0; i < Grocery::numGroceries; i++) { groceries[i].display(); } return 0; }