Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I have the following code that compiles fine as one file, but need to seperate t

ID: 666544 • Letter: I

Question

I have the following code that compiles fine as one file, but need to seperate the .cpp files (Item and ShoppingCart) into .cpp and .hpp files. I should end up with 5 files. Item.hpp, Item.cpp, ShoppingCart.hpp, ShoppingCart.cpp, and of course my Main.cpp. I use g++ compiler if that makes a difference. Which #include do i need for each file as well. MUST USE HEADER FILES (.h)

//Shopping cart with items

#include <iostream>

using namespace std;

//Item.cpp file
class Item
{
   string name;
   double price;
   int quantity;
  
   public:
   //get and set methods
   void setName(string nm)
   {name=nm;}
  
   string getName()
   {return name;}
  
   void setPrice(double pr)
   {price=pr;}
  
   double getPrice()
   {return price;}
  
   void setQuantity(int qty)
   {quantity=qty;}
  
   int getQuantity()
   {return quantity;}
   //constructor with parameters
   Item(string nm,double pr,int qty)
   {
       name=nm;price=pr;quantity=qty;
   }
  
   Item() //default constructor
   {
       name="";
       price=0;
       quantity=0;
   }
};

//ShoppingCart.cpp
class ShoppingCart
{
   Item *items[100]; //declare 100 items of pointers
   int arrayEnd;//for end of array
  
   public:
   ShoppingCart()
   {
       for(int i=0;i<100;i++) items[i]=NULL; // assign each array element to NULL
       arrayEnd=0;
   }
  
   void addItem(Item *tItem)
   {
       items[arrayEnd++]=tItem; //adding each item to array
   }
  
   double totalPrice()
   {
       double total=0;
       for(int i=0;i<arrayEnd;i++) //repeat until end of items array
       total+=(items[i]->getQuantity()*items[i]->getPrice()); //calling price and quantity by get methods
   return total; //return total
   }
};

//Main program to for testing your input
int main()
{
   Item a("affadavit", 179.99, 12);
      Item b("Bildungsroman", 0.7, 20);
      Item c("capybara", 4.5, 6);
      Item d("dirigible", 0.05, 16);
      ShoppingCart sc1;
      sc1.addItem(&a);
      sc1.addItem(&b);
      sc1.addItem(&c);
      sc1.addItem(&d);
      double diff = sc1.totalPrice();
      cout<<"Total price is : "<<diff;
      return 0;
}

Explanation / Answer

// ShoppingCart.h file


//Shopping cart with items
#include <iostream>
#include "Item.cpp"
#include "ShoppingCart.cpp"
using namespace std;

//ShoppingCart.h

class ShoppingCart
{
Item *items[100]; //declare 100 items of pointers
int arrayEnd;//for end of array
  
public:
ShoppingCart()
{
for(int i=0;i<100;i++)
   items[i]=NULL; // assign each array element to NULL
arrayEnd=0;
}
  
void addItem(Item *tItem)
{
items[arrayEnd++]=tItem; //adding each item to array
}

void display()
{
for(int i=0;i<arrayEnd;i++) //repeat until end of items array
{
   cout<<items[i]->getName()<<items[i]->getQuantity()<<items[i]->getPrice();
   }
}

  
double totalPrice()
{
double total=0;
for(int i=0;i<arrayEnd;i++) //repeat until end of items array
{
   total+=(items[i]->getQuantity()*items[i]->getPrice());
   }
//calling price and quantity by get methods
return total; //return total
}
};

//Main.cpp

#include <iostream>
#include "ShoppingCart.h"

using namespace std;

//Main program to for testing your input

int main()
{
   ShoppingCart sc1;
   ShoppingCartClass cc;
   cc.setn();
   int n=cc.getn();  
   ItemClass It1;
   Item It[20];
   for(int k=0;k<n;k++)
   {
       It[k]=It1.get();
   sc1.addItem(&It[k]);
   }
  
   sc1.display();
   double diff = sc1.totalPrice();
cout<<"Total price is : "<<diff <<" Rupees";
return 0;
}

//Item.h

//Item.h
#include <iostream>
using namespace std;

class Item
{
string name;
double price;
int quantity;
  
public:
//get and set methods
  
void setName(string nm)
{name=nm;}
  
string getName()
{return name;}

void setPrice(double pr)
{price=pr;}
  
double getPrice()
{return price;}
  
void setQuantity(int qty)
{quantity=qty;}
  
int getQuantity()
{return quantity;}
//constructor with parameters
Item(string nm,double pr,int qty)
{
name=nm;price=pr;quantity=qty;
}
  
Item() //default constructor
{
name="";
price=0;
quantity=0;
}
};

//Item.cpp

#include <iostream>
#include "Item.h"

class ItemClass
{
   public:      
       Item get(void);      
};

   Item ItemClass::get()
       {
           string n;
           double p;
           int q;
   cout<<"Enter Item Name:";
   cin>>n;
   cout<<"Enter Item Price:";
   cin>>p;
   cout<<"Enter Item Quantity:";
   cin>>q;
Item i;
i.setName(n);
i.setPrice(p);
i.setQuantity(q);
       return (i);
   }

//ShoppingCart.cpp

#include <iostream>

class ShoppingCartClass
{
   int n;
   public:
       int getn()
       {
           return n;
       }
       void setn()
       {
           cout<<"Enter the number of items purchased:";
           cin>>n;
       }
};