Question
I don't know how to write 3 classes to (1 base) and (2 derived) to make them work with a main.cpp that was already given
I don't know how to write 3 classes to (1 base) and (2 derived) to make them work with a main.cpp that was already given to me.
Here is the main.cpp file that was given:
#include
// user made classes #include "apple.h" #include "potato.h"
// using statements using std::cout; using std::cin; using std::endl; using std::vector; using std::ifstream; using std::exit;
// user defined functions int processPotatoFile(vector
int main() { // initial vectors vector
// function to process file processPotatoFile(potatoes); processAppleFile(apples); cout
// display stock list before... cout
// testing functions... // potatoes potatoes[0].setStock(456); potatoes[1].setPrice(1.99); potatoes[2].setType("Purple"); potatoes[2].setOrganic(1);
// apples apples[0].setOrchardLocation("New York, NY"); apples[0].setType("Granny Smith"); apples[2].setPrice(0.45);
// display stock list after... cout
// testing other functions... cout
cout
return 0; }
// processing potato file int processPotatoFile(vector
// initial variables string type, itemID; int quantity, flag; float price;
// static potatoList file infile.open("potatoList.txt");
// checking for file success if(infile.is_open()) { cout
// exits program otherwise else { cout
// loop to read file while(getline(infile, line)) { if(line == "potato") { // initial object // constructor: no parameters Potato temp;
// get potato data... getline(infile, type); getline(infile, itemID); infile >> quantity; infile >> price; infile >> flag;
// ignore newline for next getline... infile.ignore();
// set potato data temp.setType(type); temp.setItemID(itemID); temp.setStock(quantity); temp.setPrice(price); temp.setOrganic(flag);
// add to appropriate vector potatoes.push_back(temp); }
}
return 0; }
int processAppleFile(vector
// initial variables string type, itemID, location; int quantity, flag; float price;
// static appleList file infile.open("appleList.txt");
// checking for file success if(infile.is_open()) { cout
// exits program otherwise else { cout
while(getline(infile, line)) { if(line == "apple") { // get apple data... getline(infile, type); getline(infile, itemID); infile >> quantity; infile >> price; infile >> flag;
// ignore newline... infile.ignore();
// final line... getline(infile, location);
// create object // constructor: with parameters Apple temp(type, location, itemID, quantity, price, flag);
// add to appropriate vector apples.push_back(temp); }
} }
// function to actually display the stock void displayStock(vector displayPotatoStock(potatoes); displayAppleStock(apples); } void displayPotatoStock(vector if(potatoes[i].isOrganic()) { cout else { cout void displayAppleStock(vector if(apples[i].isOrganic()) { cout else { cout Objectives: Continue practicing past concepts Practice building basic C++ classes Practice building C++ classes that use inheritance Assignment: For this lab you will be given a driver file (main.cpp) and two text files (potatoList.txt and appleList.txt) that will be testing classes that you should create. You should not change any of these files. (Note: For your main.cpp -- feel free to comment out portions in development if you don't have a particular class working yet. For changes, we are referring to changing what a particular line actually does/how it does it.) Your job will be to create the classes that will enable this main file to work. There should be three classes: Inventory (base class) Apple (derived class) Potato (derived class) Once you create your classes and are finished, the final project should statically print out what is contained in the code execution portion. That is how you know that you have completed the program. The classes you should create are outlined below in UML Class diagrams. For a UML class diagram cheat sheet, refer to the last page of the lab assignment details. UML Specifications: Inventory: o getItemTotal(int quantity) - returns the item's price multiplied by the parameter quantity o is Organic() - acts as a standard getter o setOrganic(int flag) - takes in an integer -- if the integer is 1 organic should be true, else if the integer is 0, it should be false o getters/setters - act as usual Apple O Apple() - standard empty constructor -- takes all items and sets them to their empty counterparts ("" for strings, O for numbers -- false for booleans); statically sets itemName to "Apples" Apple(with parameters) - takes in user parameters to set apple and inventory data equal to; statically sets itemName to "Apples" For organic: see setOrganic standards above o getters/setters - act as usual Potato O Potato() - standard empty constructor -- takes all items and sets them to their empty counterparts ("" for strings, O for numbers -- false for booleans); statically sets itemName to "Potatoes" Potato(with parameters) - takes in user parameters to set apple and inventory data equal to; statically sets itemName to "Potatoes" For organic: see setOrganic standards above o getPotato Total(float weight) - returns the weight parameter multiplied by the price of the potato o getters/setters - act as usual Inventory Apple Potato -type: string - orchard Location: string -type: string #itemID: string #itemName: string # stock: int #price: float #organic: bool + Apple + Apple(string type, string location, string itemID, int stock, float price, int flag) + getType(): string + getOrchardLocation(): string + setType(string type): void +setOrchardLocation(string location): void + getitemIDO: string + getitemName(): string + getStock(): int + getPrice(): float + isOrganico: bool + setitemID(string itemID): void + setStock(int stock): void + setPrice(float price): void + setOrganic(int flag): void + getitem Total(int quantity): float + Potato + Potato(string type, string itemID, int stock, float price, int flag) + getType(): string + setType(string type): void + getPotato Total(float weight): float Code Execution! Successful file opening. ***STOCK BEFORE CHANGES*** We have the following items in stock: Item: 0001 568 Idaho Potatoes at 0.33 per lb Organic: no Item: 0002 67 Sweet Potatoes at 0.97 per lb Organic: yes Item: 0003 32 Golden Potatoes at 0.57 per lb Organic: no Item: 0004 345 Fuji Apples at $0.66 per item Sourced from: Salt Lake City, UT Organic: yes Item: 0005 53 Pink Lady Apples at $0.92 per item Sourced from: Boise, ID Organic: yes Item: 0006 345 Red Delicious Apples at $0.4 per iten Sourced from: Detroit, MI Organic: no ***STOCK AFTER CHANGES*** lle have the following items in stock: Item: 0001 456 Idaho Potatoes at 0.33 per lb Organic: no Item: 0002 67 Sweet Potatoes at 1.99 per lb Organic: yes Item: 0003 32 Purple Potatoes at 0.57 per lb Organic: yes Item: 0004 345 Granny Smith Apples at $0.66 per iten Sourced from: New York, NY Organic: yes Item: 0005 53 Pink Lady Apples at $0.92 per item Sourced from: Boise, ID Organic: yes Item: 0006 345 Red Delicious Apples at $0.45 per item Sourced from: Detroit, MI Organic: no ***MISCELLANEOUS*** Buying 44 Pink Lady Apples will cost $40.48 Buying 134lbs worth of Idaho Potatoes will cost $44.22 Hints (UML Cheat Sheet): Each portion of a detailed UML Class diagram contains a different part of the class. There's a section for the name, variables, and functions belonging to the class. A break down of these is found below. Contains the class name Inventory #itemID: string #itemName: string #stock: int #price: float #organic: bool Contains all of the class data attributes Contains the class functions + getitemIDO: string + getitemName(): string + getStock(): int + getPrice(): float + isOrganic(): bool + setitemID(string itemID): void + setStock(int stock): void + setPrice(float price): void + setOrganic(int flag): void + getitem Total(int quantity): float Additionally, each variable and function will be preceded by a symbol: +, -, or #. These refer to where your data will be accessible. Each of those mean the following: - Private + Public #Protected For your variables, each will be followed by the data type they should be initialized as. So, "# itemID: string" should be a protected string variable named itemID. Finally, your functions contain any/all parameters (and their data types!) following the function name. They then are following by a colon and the data type they should return. So," + getStock(): int" should be a public function that has no parameters and retums an integer. Objectives: Continue practicing past concepts Practice building basic C++ classes Practice building C++ classes that use inheritance Assignment: For this lab you will be given a driver file (main.cpp) and two text files (potatoList.txt and appleList.txt) that will be testing classes that you should create. You should not change any of these files. (Note: For your main.cpp -- feel free to comment out portions in development if you don't have a particular class working yet. For changes, we are referring to changing what a particular line actually does/how it does it.) Your job will be to create the classes that will enable this main file to work. There should be three classes: Inventory (base class) Apple (derived class) Potato (derived class) Once you create your classes and are finished, the final project should statically print out what is contained in the code execution portion. That is how you know that you have completed the program. The classes you should create are outlined below in UML Class diagrams. For a UML class diagram cheat sheet, refer to the last page of the lab assignment details. UML Specifications: Inventory: o getItemTotal(int quantity) - returns the item's price multiplied by the parameter quantity o is Organic() - acts as a standard getter o setOrganic(int flag) - takes in an integer -- if the integer is 1 organic should be true, else if the integer is 0, it should be false o getters/setters - act as usual Apple O Apple() - standard empty constructor -- takes all items and sets them to their empty counterparts ("" for strings, O for numbers -- false for booleans); statically sets itemName to "Apples" Apple(with parameters) - takes in user parameters to set apple and inventory data equal to; statically sets itemName to "Apples" For organic: see setOrganic standards above o getters/setters - act as usual Potato O Potato() - standard empty constructor -- takes all items and sets them to their empty counterparts ("" for strings, O for numbers -- false for booleans); statically sets itemName to "Potatoes" Potato(with parameters) - takes in user parameters to set apple and inventory data equal to; statically sets itemName to "Potatoes" For organic: see setOrganic standards above o getPotato Total(float weight) - returns the weight parameter multiplied by the price of the potato o getters/setters - act as usual Inventory Apple Potato -type: string - orchard Location: string -type: string #itemID: string #itemName: string # stock: int #price: float #organic: bool + Apple + Apple(string type, string location, string itemID, int stock, float price, int flag) + getType(): string + getOrchardLocation(): string + setType(string type): void +setOrchardLocation(string location): void + getitemIDO: string + getitemName(): string + getStock(): int + getPrice(): float + isOrganico: bool + setitemID(string itemID): void + setStock(int stock): void + setPrice(float price): void + setOrganic(int flag): void + getitem Total(int quantity): float + Potato + Potato(string type, string itemID, int stock, float price, int flag) + getType(): string + setType(string type): void + getPotato Total(float weight): float Code Execution! Successful file opening. ***STOCK BEFORE CHANGES*** We have the following items in stock: Item: 0001 568 Idaho Potatoes at 0.33 per lb Organic: no Item: 0002 67 Sweet Potatoes at 0.97 per lb Organic: yes Item: 0003 32 Golden Potatoes at 0.57 per lb Organic: no Item: 0004 345 Fuji Apples at $0.66 per item Sourced from: Salt Lake City, UT Organic: yes Item: 0005 53 Pink Lady Apples at $0.92 per item Sourced from: Boise, ID Organic: yes Item: 0006 345 Red Delicious Apples at $0.4 per iten Sourced from: Detroit, MI Organic: no ***STOCK AFTER CHANGES*** lle have the following items in stock: Item: 0001 456 Idaho Potatoes at 0.33 per lb Organic: no Item: 0002 67 Sweet Potatoes at 1.99 per lb Organic: yes Item: 0003 32 Purple Potatoes at 0.57 per lb Organic: yes Item: 0004 345 Granny Smith Apples at $0.66 per iten Sourced from: New York, NY Organic: yes Item: 0005 53 Pink Lady Apples at $0.92 per item Sourced from: Boise, ID Organic: yes Item: 0006 345 Red Delicious Apples at $0.45 per item Sourced from: Detroit, MI Organic: no ***MISCELLANEOUS*** Buying 44 Pink Lady Apples will cost $40.48 Buying 134lbs worth of Idaho Potatoes will cost $44.22 Hints (UML Cheat Sheet): Each portion of a detailed UML Class diagram contains a different part of the class. There's a section for the name, variables, and functions belonging to the class. A break down of these is found below. Contains the class name Inventory #itemID: string #itemName: string #stock: int #price: float #organic: bool Contains all of the class data attributes Contains the class functions + getitemIDO: string + getitemName(): string + getStock(): int + getPrice(): float + isOrganic(): bool + setitemID(string itemID): void + setStock(int stock): void + setPrice(float price): void + setOrganic(int flag): void + getitem Total(int quantity): float Additionally, each variable and function will be preceded by a symbol: +, -, or #. These refer to where your data will be accessible. Each of those mean the following: - Private + Public #Protected For your variables, each will be followed by the data type they should be initialized as. So, "# itemID: string" should be a protected string variable named itemID. Finally, your functions contain any/all parameters (and their data types!) following the function name. They then are following by a colon and the data type they should return. So," + getStock(): int" should be a public function that has no parameters and retums an integer
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