You were recently hired by GroceriesRUs company. They want you to develop
code to move their grocery operations online.
As a part of this effort, you are required to develop a GroceryCart class
that can hold groceryitems (Item class).
The Item class has the following data members item description (string),
cost (double).
The Items class should have mutators (setters) and accessors (getters).
Also overload the << oprator for the Item class
The GroceryCart should have the following public methods-
void insertItem(Item) - Insert a grocery Item into the cart
void deleteItem(Item) - Delete a grocery Item from the cart
int getItemCount() - Return how many items are there are in the cart
bool isCartEmpty() - Returns true if cart is empty; false otherwise
double calcTotalCost() - Returns the total cost of all items in the cart
Also, overload the == operator to see if two carts are identical. Two
carts are identical if all the item descriptions are identical.
Hint: Use a vector container in the GroceryCart to hold the items.
Your code should pass the test in main() for full credit. The code should
be organized as the following files
item.h, item.cpp, cart.h, cart.cpp, assignment1_test.cpp (which has the
main() test function)
*/
#include <iostream>
# include "item.h" // You need to code this
# include "cart.h" // You need to code this
using namespace std;
int main()
{
Item it1{"Salad", 5.50}, it2{"Milk", 2.50}, it3{"Bread", 1.75}, it4;
//Tests the Item constructors
GroceryCart cart1; // Test GroceryCart constructor
cart1.insertItem(it1);
cart1.insertItem(it2);
cart1.insertItem(it3);
if (!cart1.isCartEmpty()) { // Test isCartEmpty() method
cout << "Number of items in cart1 " << cart1.getItemCount() <<
endl; // Tests getItemCount() method
cout << "Total cost of items in cart1 $" << cart1.calcTotalCost()
<< endl; // Tests calcTotalCost() method
}
//To test the delete function
cart1.deleteItem(it1);
cart1.deleteItem(it3);
cart1.deleteItem(it2);
cout << "Number of items in cart1 " << cart1.getItemCount() << endl;
GroceryCart cart2;
cart2.insertItem(it1);
it4.setDescription("Coffee"); // Tests setDescription() method
it4.setCost(7.5); //Test setCost method
cout << it4; //Should print Coffee:$7.5. Tests << operator
cart2.insertItem(it4);
//To test the items inserted in cart2
cout << cart2.getItemCount() << endl;
// To check the == operator overloading
GroceryCart cart3;
cart3.insertItem(it1);
cart3.insertItem(it2);
GroceryCart cart4;
cart4.insertItem(it1);
cart4.insertItem(it2);
if (cart3 == cart4) //Tests == operator
cout << "Possibly duplicate carts" << endl;
return 0;
}
Code in c++::
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <cstddef>
#include <cstring>
using namespace std;
class Company
{
public:
Company(string name, int size);
//Initializes the company object so it can accept "size" employee
ids.
// Sets the name of the company object to name.
Company( );
//Initializes the company object so it can accept up to 15 employee
id's of
//type int, and sets the name of the course to the empty
string.
Company(const Company& acompany);
//Copy Constructor - you define this
~Company( );
//Destructor - you define this
//Returns all the dynamic memory used by the company object to the
freestore.
void operator =(const Company& rightSide);
//Overloads the == operator, you define this
friend bool operator > (Company& cone, Company&
ctwo);
//the overloaded > operator as a friend function, you define
this
// methods to set and get the company name, and to set and get
employee id numbers and salaries
void setName(string aname);
string getName();
void setEid(int location, int idValue);
int getEid(int location);
float getSalary(int id);
void setSalary (int id, float salary);
int CompanySize; //Number of employees that work for the company
private:
string Cname; // Name of the Company
int *Eid; //pointer to dynamic array that holds the values of the
Employee ids
float *Esalary; // dynamic array that holds the salaries for each
employess
float getPayroll(); // returns the company payroll (sum of the
salaries in the Esalary array).
// Useful for implementing the > operator
}; // end definition of Company interface
// function to print out company information
void pCompanyInfo(Company acompany);
// function to sort companies into ascending order based on the
total company payroll
void sortCompanies(Company CompanyList[], int num);
//Program to demonstrate (and test) the use of the class
Company.
int main( ) {
Company *Clist[10]; // Array of pointers to Company
Objects
ifstream infile; // The file pointer to the data file
char filename[30]; // Name of the file with the Company Data
string cname; // read the name of the company into cname
int csize; //read the number of employees into csize
int eid; // read an employee id into eid
float esalary; // read a salary into esalary
cout << "Enter input file name: ";
cin >> filename;
infile.open(filename);
if (infile.fail()) {
cout << "Can not open file: "<< filename <<
endl;
exit(1);
}
int companyCnt;
infile >> companyCnt; // get the number of companies stored
in the file
for (int i = 0; i < companyCnt; i++) {
// read in a company name
infile >> cname;
// read in a company size
infile >> csize;
Clist[i] = new Company(cname, csize); // instantiate a company and put it on the list of companies
for (int j=0; j < csize; j++){ // get and store the employee
ids for the company
infile >> eid;
Clist[i]->setEid(j, eid);
}
for (int k=0; k < csize; k++){ //get and store the employee
salaries for each employee in the company
infile >> esalary;
Clist[i]->setSalary(Clist[i]->getEid(k), esalary);
}
} // end reading data from input file
cout << endl << "Company information" << endl;
cout.setf(ios::fixed);
cout.setf(ios::showpoint); // set decimal places
cout.precision(2);
//Print out the information the program just obtained from the
file
for (int i = 0; i < companyCnt; i++)
pCompanyInfo(*Clist[i]);
cout << endl << endl;
//Create an array of Company Objects
Company CompObjects[10];
for (int i = 0; i < companyCnt; i++)
CompObjects[i] = *Clist[i];
// now, sort the company objects into ascending order
sortCompanies(CompObjects, companyCnt);
cout << "Company Information in Ascending Order by Total Payroll" << endl;
for (int i = 0; i < companyCnt; i++)
pCompanyInfo(CompObjects[i]);
return 0;
} // end main
// function to print out company information - you define
this
void pCompanyInfo(Company acompany) {
cout << "\nCompany Name: " << acompany.getName()
<< endl;
for (int i = 0; i < acompany.CompanySize; i++)
cout << "Employee: " << i << " id number: "
<< acompany.getEid(i)<< " Salary: " <<
acompany.getSalary(acompany.getEid(i)) << endl;
}
//function to sort the companies stored in the CompanyList
into
//order of total payroll (that is, lowest total payroll to
highest)
//you define this
void sortCompanies(Company CompanyList[], int num){
for (int i = num - 1; i > 0; i--)
for (int j = 0; j < i; j++) {
if (CompanyList[j] > CompanyList[j + 1]) {
Company temp;
temp = CompanyList[j]; // overloaded assignment operator is used
here
CompanyList[j] = CompanyList[j + 1];
CompanyList[j + 1] = temp;
}
}
}
//Method Definitons for Company
Company::Company()
{
CompanySize = 15;
Cname="";
Eid = new int [CompanySize];
Esalary = new float [CompanySize];
}
Company::Company(string name, int size)
{
CompanySize = size;
Cname = name;
Eid = new int [CompanySize];
Esalary = new float [CompanySize];
}
//Copy Constructor - you define this
Company:: Company(const Company & acompany)
{
CompanySize = acompany.CompanySize;
Cname = acompany.Cname;
Eid = new int [CompanySize];
Esalary = new float [CompanySize];
for (int i = 0; i < CompanySize; i++) {
Eid[i] = acompany.Eid[i];
Esalary[i] = acompany.Esalary[i];
}
}
// destructor - you implement this
Company::~Company()
{
delete [] Eid;
delete [] Esalary;
}
void Company::setName(string aname)
{
Cname = aname;
}
string Company::getName()
{
return Cname;
}
void Company::setEid(int location, int idValue)
{
if (location >= 0 && location < CompanySize)
Eid[location] = idValue;
else
cout << "Error: Max Employese: " << CompanySize
<< " Exceeded" << endl << endl;
}
int Company::getEid(int location)
{
return Eid[location];
}
void Company::setSalary(int id, float Value)
{
bool found = false;
for (int i = 0; i < CompanySize; i++)
if (Eid[i] == id) {
Esalary[i] = Value;
found = true;
break;
}
if (!found)
cout << "Error: could not set salary for employee: " <<
id << endl;
return;
}
float Company::getSalary(int id) {
for (int i = 0; i < CompanySize; i++)
if (Eid[i] == id)
return (Esalary[i]);
cout << "Error: could not find salary for employee: " << id << endl;
return 0.0;
}
//Overloaded assignment operator - you implement this
void Company::operator =(const Company& right_side)
{
if (right_side.Eid != Eid || right_side.Esalary != Esalary) {
if (Eid != 0)
delete [] Eid;
if (Esalary != 0)
delete [] Esalary;
CompanySize = right_side.CompanySize;
Cname = right_side.Cname;
Eid = new int [CompanySize];
Esalary = new float [CompanySize];
for (int i = 0; i < CompanySize; i++) {
Eid[i] = right_side.Eid[i];
Esalary[i] = right_side.Esalary[i];
}
}
}
// This might be useful for overloading the > operator for
the Company Class
float Company::getPayroll() {
float total = 0;
for (int i = 0; i < CompanySize; i++)
total += Esalary[i];
return total;
}
// The overloaded > operator for the course class, you are to
implement this.
bool operator > (Company& cone, Company& ctwo) {
return (cone.getPayroll() > ctwo.getPayroll()); // return true
if this condition is satisfied
}
You were recently hired by GroceriesRUs company. They want you to develop code to move their...
I've posted 3 classes after the instruction that were given at start You will implement and test a PriorityQueue class, where the items of the priority queue are stored on a linked list. The material from Ch1 ~ 8 of the textbook can help you tremendously. You can get a lot of good information about implementing this assignment from chapter 8. There are couple notes about this assignment. 1. Using structure Node with a pointer point to Node structure to...
You are to implement a MyString class which is our own limited implementation of the std::string Header file and test (main) file are given in below, code for mystring.cpp. Here is header file mystring.h /* MyString class */ #ifndef MyString_H #define MyString_H #include <iostream> using namespace std; class MyString { private: char* str; int len; public: MyString(); MyString(const char* s); MyString(MyString& s); ~MyString(); friend ostream& operator <<(ostream& os, MyString& s); // Prints string MyString& operator=(MyString& s); //Copy assignment MyString& operator+(MyString&...
C++ programming language: In this program you will create a simplified bag that acts like a stack meaning that the Last item inserted is the First Item that comes out. Your backend implementation must use a linked list. The code should pass the test (there's only 1) and there should be no memory leaks. Note that passing the test does not ensure full credit! The functions are listed in the suggested order of implementation but you may implement them in...
In an effort to develop in-depth knowledge of base class, derived class, and operator overloading, we will expand on our in-class exercise of developing the rectangleType class and overload the following operators: +, -, *, ++, --, ==, !=, >>, and <<. Your program should contain the following features: a. Create a base class named rectangleType with following private data members: • length with double type • width with double type b. rectangleType class should contain the following functions: •...
This Program should run in C++ In an effort to develop in-depth knowledge of base class, derived class, and operator overloading, we will expand on our in-class exercise of developing the rectangleType class and overload the following operators: +, -, *, ++, --, ==, !=, >>, and <<. Your program should contain the following features: a. Create a base class named rectangleType with following private data members: • length with double type • width with double type b. rectangleType class...
Language = C++ How to complete this code? C++ Objects, Structs and Linked Lists. Program Design: You will create a class and then use the provided test program to make sure it works. This means that your class and methods must match the names used in the test program. Also, you need to break your class implementation into multiple files. You should have a car.h that defines the car class, a list.h, and a list.cpp. The link is a struct...
Write a function, swapSubtrees, that swaps all of the left and right subtrees of a binary tree. Add this function to the class binaryTreeType and create a program to test this function. #include <iostream> using namespace std; //Definition of the Node template <class elemType> struct nodeType { elemType info; nodeType<elemType> *lLink; nodeType<elemType> *rLink; }; //Definition of the class template <class elemType> class binaryTreeType { public: //Overload the assignment operator. const binaryTreeType<elemType>& operator=(const binaryTreeType<elemType>&) { if (this != &otherTree) //avoid self-copy...
Task:
Tasks to complete:
------------------------------------------------------------------------------------------------------------------------------------------
given code:
------------------------------------------------------------------------------------------------------------------------------------------
main.cpp
#include
#include "rectangleType.h"
using namespace std;
// part e
int main()
{
rectangleType rectangle1(10, 5);
rectangleType rectangle2(8, 7);
rectangleType rectangle3;
rectangleType rectangle4;
cout << "rectangle1: " << rectangle1 <<
endl;
cout << "rectangle2: " << rectangle2 <<
endl;
rectangle3 = rectangle1 + rectangle2;
cout << "rectangle3: " << rectangle3 << endl;
rectangle4 = rectangle1 * rectangle2;
cout << "rectangle4: " << rectangle4 << endl;
if (rectangle1 > rectangle2)
cout << "Area...
Write a code in C++ by considering the following conditions :- Tasks :- 1. Create a class Employee (with member variables: char * name, int id, and double age). 2. Create a constructor that should be able to take arguments and initialize the member variables. 3. Create a copy constructor for employee class to ensure deep copy. 4. Create a destructor and de allocate the dynamic memory. 5. Overload the assignment operator to prevent shallow copy and ensure deep copy....
CSCI 2010 Lab11 Link-Lists
Lab 11A Linked-Lists
Preparation
Create a Visual Studio C++ Project C2010Lab11A
Add the following to the project.
//LinkedList.cpp
#include <cstdlib>
#include "LinkedList.h"
using namespace std;
//---------------------------------------------------
//List Element Members
//---------------------------------------------------
ListElement::ListElement(int d, ListElement * n)
{
datum=d;
next=n;
}
int ListElement::getDatum () const
{
return datum;
}
ListElement const* ListElement::getNext () const
{
return next;
}
//---------------------------------------------------
//LinkedList Members
//---------------------------------------------------
LinkedList::LinkedList ()
{
head=NULL;
}
void LinkedList::insertItem(int item)
{
ListElement *currPtr = head;
ListElement *prevPtr =...