Problem 1 Implement a program that tracks customers\' purchases. Create the foll
ID: 3915088 • Letter: P
Question
Problem 1 Implement a program that tracks customers' purchases. Create the following classes. Use appropriate access modifiers (member variables should be privatel) and data types for each. Don't forget to add getter and setter functions. Shopping Cart 27 points Item: This class has the attributes (member variables) called title, description, and price. It has a pure virtual function called print that prints the type and description of the current object to the console. This function is NOT implemented in this class but must be implemented/overridden in the three subclasses below. Book: This class inherits from Item. It has an instance variable called pageCount. Movie: This class inherits from Item. It has an instance variable called length. CD: This class inherits from item. It has an instance variable called trackCount. o o ShoppingCart: This class keeps track of items that were bought. it has a single constructor which expects the maximum number of items that can be placed in the cart. It must have a dynamically allocated array of pointer to an item object (item*array- new Item [size]) which is initialized in the constructor. The cart must have functions to add an item and print the items currently in the cart to the console by calling each object's print() function. Customer: The customer class stores an id, the first name and the last name and a pointer to a shopping cart object which gets initialized in the constructor. Finally, implement a main function that creates a customer. Then add one item of each type to the customer's shopping cart and list the items in the cart on the console. *Note: fields, attributes, properties, member variables, instance variables all refer to the same thing: variables defined inside a class as part of an object, not inside fiunctionsExplanation / Answer
#include<iostream>
using namespace std;
//class item
//parent class
class Item
{
//data memebers
public:
string title,description;
double price;
//virtual print method
virtual void print()
{
cout<<" ------------------- ";
cout<<"Title:"<<title<<endl;
cout<<"Decription:"<<description<<endl;
cout<<"Price:"<<price<<endl;
cout<<" ------------------- ";
}
};
//child class
//inherits from Item
class Book :public Item
{
public:
int pageCount;
void print()
{
cout<<" ------------------- ";
cout<<"Title:"<<title<<endl;
cout<<"Decription:"<<description<<endl;
cout<<"Price:"<<price<<endl;
cout<<"Page Count:"<<pageCount<<endl;
cout<<" ------------------- ";
}
};
class Movie:public Item
{
public:
int length;
void print()
{
cout<<" ------------------- ";
cout<<"Title:"<<title<<endl;
cout<<"Decription:"<<description<<endl;
cout<<"Price:"<<price<<endl;
cout<<"length:"<<length<<endl;
cout<<" ------------------- ";
}
};
class CD:public Item
{
public:
int trackCount;
void print()
{
cout<<" ------------------- ";
cout<<"Title:"<<title<<endl;
cout<<"Decription:"<<description<<endl;
cout<<"Price:"<<price<<endl;
cout<<"Track Count:"<<trackCount<<endl;
cout<<" ------------------- ";
}
};
//shoppingCart class
class shoppingCart
{
public:
Item *array;
int current,size;
//constructor
shoppingCart(int max)
{
size=max;
current =0;
array = new Item[size];
}
//method to add element
void add()
{
string t,d;
double p;
//reading input
cout<<"Enter item details: ";
cout<<"Enter title:";
cin>>t;
cout<<"Enter description:";
cin>>d;
cout<<"Enter price:";
cin>>p;
array[current].title=t;
array[current].description=d;
array[current].price=p;
current++;
}
//method to print cart items..
void print_cart()
{
int i=0;
cout<<"Cart items: ";
while(i<current)
{
array[i].print();
i++;
}
}
};
//customer class
class Customer
{
public:
string id,first,last;
shoppingCart *s;
int size;
//constructor
Customer()
{
cout<<"Enter size of the cart";
cin>>size;
s = new shoppingCart(size);
cout<<"Enter id:";
cin>>id;
cout<<"Enter first name:";
cin>>first;
cout<<"Enter last name:";
cin>>last;
}
};
//main method
int main()
{
Customer c;
c.s->add();//adding item to cart
c.s->add();//adding item to cart
c.s->print_cart();
return 0;
}
output:
Enter size of the cart2
Enter id:str2
Enter first name:surya
Enter last name:s
Enter item details:
Enter title:hrx
Enter description:hrithhik
Enter price:1020
Enter item details:
Enter title:honda
Enter description:hyd
Enter price:12312
Cart items:
-------------------
Title:hrx
Decription:hrithhik
Price:1020
-------------------
-------------------
Title:honda
Decription:hyd
Price:12312
-------------------
Process exited normally.
Press any key to continue . . .
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.