Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

CAN SOMEONE HELP ME WITH THIS OR EXPLAIN HOW TO DO IT. Use C++ and linux Create public member function getExpDates() forclass ItemList to return

CAN SOMEONE HELP ME WITH THIS OR EXPLAIN HOW TO DO IT. Use C++ and linux

  1. Create public member functiongetExpDates() forclass ItemList to return the expiration dates in the list. The dates are returned through the dynamically allocated array "expDates" and the function returns the number of dates in the list.

You have to allocate just a big enough array to hold the dates. For example, if there are 10 items in the list, you should allocate an array of 10 dates. Each element in the array is aDate object.

int getExpDates(Date * & expDates) const;

First add the member function prototype insideclass ItemList initemList.h, then put the member function implementation initemList.cppand finally invoke/test the function inmain.

You need to display the retrieved dates. Please label your output clearly.

Hint: remember to allocate the array before you try to populate it and also remember to deallocate the array after you are done using it. If you forget to allocate the array, your code will crash. If you forget to deallocate the array, you will get memory leaks. Many people hate C++ for these two frequently encountered enemies, segmentation fault and memory leaks.

items.txt

apple:0.99:2022/3/20 banana:0.69:2021/4/28 cookie:0.50:2023/2/12 donut:1.00:2021/10/28 egg:3.88:2022/1/30 fish:5.88:2021/2/15 milk:2.99:2021/3/2 yogurt:6.38:2021/6/13

itemList.h

#pragma once #include "item.h"

class ItemList { private: const static int INIT_CAPACITY = 1; //set low for test purpose const static int GROWTH_FACTOR = 2; InventoryItem * list; int size; int capacity; void expandArray(); public: ItemList(); ItemList(const ItemList& aList); ItemList(ItemList&& aList); ~ItemList();

const ItemList& operator= (const ItemList& aList); ItemList& operator= (ItemList&& aList);

int getSize() const; InventoryItem& operator[] (int index); const InventoryItem& operator[] (int index) const;

void append(const InventoryItem& anItem); void printList() const; void readList(istream& in); };

itemList.cpp

#include "itemList.h" ItemList::ItemList() { size = 0; capacity = INIT_CAPACITY; list = new InventoryItem[capacity]; }

ItemList::ItemList(const ItemList& aList) { size = aList.size; capacity = aList.capacity; list = new InventoryItem[capacity]; int index; for(index = 0; index < size; index++) { list[index] = aList.list[index]; } }

ItemList::ItemList(ItemList&& aList) { this->size = aList.size; this->capacity = aList.capacity; this->list = aList.list; aList.list = nullptr; aList.size = 0; aList.capacity = 0; }

ItemList::~ItemList() { if(this->list) { delete [] this->list; this->list = nullptr; } } ItemList& ItemList::operator= (ItemList&& aList) { if (this == &aList) { return *this; } this->size = aList.size; this->capacity = aList.capacity; if (this->list) { delete [] this->list; } this->list = aList.list; aList.list = nullptr; aList.size = 0; aList.capacity = 0; return *this; }

const ItemList& ItemList::operator= (const ItemList& aList) { if(this == &aList) { return *this; } size = aList.size; capacity = aList.capacity; if(list != nullptr) { delete [] list; } list = new InventoryItem[capacity]; int index; for(index = 0; index < size; index++) { list[index] = aList.list[index]; } return *this; }

int ItemList::getSize() const { return size; }

InventoryItem& ItemList::operator[] (int index) { return list[index]; }

const InventoryItem& ItemList::operator[] (int index) const { return list[index]; }

void ItemList::expandArray() { capacity = capacity * GROWTH_FACTOR; InventoryItem * tempList = new InventoryItem[capacity]; int index; for(index = 0; index < size; index++) { tempList[index] = list[index]; } delete [] list; list = tempList; }

void ItemList::append(const InventoryItem& anItem) { if(size == capacity) { expandArray(); } list[size] = anItem; size++; } void ItemList::readList(istream& in) { char itemName[MAX_CHAR]; float itemPrice; Date expDate; int year; int month; int day; InventoryItem anItem;

in.get(itemName, MAX_CHAR, ':'); while (!in.eof()) { in.get(); in >> itemPrice; in.get(); in >> year; in.get(); in >> month; in.get(); in >> day; in.ignore(MAX_CHAR, ' ');

anItem.setItemName(itemName); anItem.setItemPrice(itemPrice); expDate.setDate(year, month, day); anItem.setExpDate(expDate);

append(anItem);

in.get(itemName, MAX_CHAR, ':'); } } void ItemList::printList() const { int index; for(index = 0; index < size; index++) { list[index].print(); } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Financial management theory and practice

Authors: Eugene F. Brigham and Michael C. Ehrhardt

12th Edition

978-0030243998, 30243998, 324422695, 978-0324422696

Students also viewed these Programming questions