Please help in C++ programming

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.

//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;
}
It's quite straight forward, just put your class declarations in a separate header for each one:

Item.hpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <string>
using namespace std;

class Item
{
public:
   Item(); //default constructor

   //constructor with parameters
   Item(string nm,double pr,int qty);

   //get and set methods
   void setName(string nm);
   string getName();
   void setPrice(double pr);
   double getPrice();
   void setQuantity(int qty);
   int getQuantity();

private:
   string name;
   double price;
   int quantity;
};


ShoppingCart.hpp:
1
2
3
4
5
6
7
8
9
10
11
12
class ShoppingCart
{
public:
   ShoppingCart();

void addItem(Item *tItem);
double totalPrice();

private:
   Item *items[100]; //declare 100 items of pointers
   int arrayEnd;//for end of array
};


Item.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include "Item.hpp"

Item::Item()
   : name(“”)
   , price(0)
   , quantity(0)
{
}

Item::Item(string nm,double pr,int qty)
   : name(mm)
   , price(pr)
   , quantity(qty)
  {
  }

void Item::setName(string nm)
{
   name=nm;
}

string Item::getName()
{
   return name;
}

void Item::setPrice(double pr)
{
   price=pr;
}

double Item::getPrice()
{
   return price;
}

void Item::setQuantity(int qty)
{
    quantity=qty;
}

int Item::getQuantity()
{
   return quantity;
}


ShoppingCart.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include "ShoppingCart.hpp"

ShoppingCart::ShoppingCart()
{
   for(int i=0;i<100;i++) i
      tems[i]=NULL; // assign each array element to NULL
   arrayEnd=0;
}

void ShoppingCart::addItem(Item *tItem)
{
   items[arrayEnd++]=tItem; //adding each item to array
}

double ShoppingCart::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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include "Item.hpp"
#include "ShoppingCart.hpp"

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;
}


HTH
Last edited on
Topic archived. No new replies allowed.