0% found this document useful (0 votes)
62 views5 pages

Shopping List Management System Using Inheritance in C++

The document outlines a project for creating a Shopping List Management System in C++ using object-oriented programming principles, focusing on inheritance, composition, and encapsulation. It details the design of a base Product class and derived classes for different product categories, along with a ShoppingList class to manage these products. Additionally, it includes functional requirements for adding, removing, displaying items, calculating total costs, and optional enhancements like a menu-driven interface and persistent data storage.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
62 views5 pages

Shopping List Management System Using Inheritance in C++

The document outlines a project for creating a Shopping List Management System in C++ using object-oriented programming principles, focusing on inheritance, composition, and encapsulation. It details the design of a base Product class and derived classes for different product categories, along with a ShoppingList class to manage these products. Additionally, it includes functional requirements for adding, removing, displaying items, calculating total costs, and optional enhancements like a menu-driven interface and persistent data storage.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

CENG 241 2024-2025 : Composition

Shopping List Management System Using Inheritance in C++

This project involves designing and implementing a Shopping List Management System using object-oriented
programming (OOP) principles in C++. The system manages a shopping list composed of multiple product
categories (e.g., Food, Electronics, and Clothing), each represented by specialized classes that inherit from a
common base class, Product.

The primary focus is on practicing key OOP concepts such as:

 Inheritance: Designing a class hierarchy with shared functionality in a base class.


 Composition: Using objects of other classes to build a more complex system (ShoppingList contains
arrays or vectors of Product-derived objects).
 Encapsulation: Protecting and controlling access to class data using private attributes and public getter
and setter methods.
 Constructors and Destructors: Managing object initialization and cleanup.
 Operator Overloading: Extending C++ operators to simplify common operations such as printing or
merging lists.
 Dynamic Memory Management: Using new and delete for efficient handling of object storage
(optional).

Implement a ShoppingList class that represents a shopping list containing different product types. Each product type (e.g.,
Food, Electronics, Clothing, Household Items) will be implemented as a separate class. The ShoppingList class will
compose these product types and provide functionality to manage them.
Key Requirements
Base Class: Product
The Product class serves as the foundation for all product types.
Attributes (shared among all products):
name (string): The name of the product.
quantity (int): The number of units of the product.
price (double): The price of the product or unit price.
Methods:
Constructor and Destructor: For initializing and cleaning up resources.
Getter and Setter Methods: Provide controlled access to private attributes (e.g., getName(),
setPrice(double)).
Virtual Method: calculateCost() to compute the total cost (quantity × price). This allows derived classes
to override specific behavior.
Virtual Method: display() to output product details.
Derived Classes
1. Food Class
Attributes:
name: Name of the food item.
quantity: Number of units.
pricePerUnit: Cost per unit of the item.
Methods:
Constructor and Destructor.
Getter and Setter methods for all attributes.
calculateCost(): Returns quantity × pricePerUnit.
display(): Outputs details such as “Food: Apples, Quantity: 5, Price per Unit: 1.50, Total Cost:
7.50”.
2. Electronics Class
Attributes:
name: Name of the electronic item.
quantity: Number of units.
price: Price of the item.
brand: Brand of the electronic item.
Methods:
Constructor and Destructor.
Getter and Setter methods for all attributes.
calculateCost(): Returns quantity × price.
display(): Outputs details such as “Electronics: Smartphone (Samsung), Quantity: 1, Price:
800.00, Total Cost: 800.00”.
3. Clothing Class
Attributes:
name: Name of the clothing item.
quantity: Number of units.
price: Price of the item.
size: Size of the clothing (e.g., S, M, L).
Methods:
Constructor and Destructor.
Getter and Setter methods for all attributes.
calculateCost(): Returns quantity × price.
display(): Outputs details such as “Clothing: T-Shirt (Size: M), Quantity: 4, Price: 10.00, Total
Cost: 40.00”.
Composition: ShoppingList Class
The ShoppingList class uses composition to manage multiple product categories.
Attributes:
Arrays or vectors for each product type:
Example: std::vector<Food> foods;, std::vector<Electronics> electronics;.
Counters to track the number of items in each category.
Methods:
addItem(): Add a product to the appropriate category (e.g., foods.push_back(newFood)).
removeItem(): Remove a product by its name or position in a category.
displayAll(): Print all items grouped by category.
calculateTotalCost(): Compute the sum of the total costs for all categories.
clearList(): Clear all categories and reset counters.
Optional: Overload << operator to print the shopping list in a user-friendly format.
Optional: Overload + operator to merge two shopping lists.
Functional Requirements
Core Functionalities
1. Adding Items:
Dynamically add items to the list for a specific category (foods, electronics, or clothing).
Use appropriate constructors for creating items.
2. Removing Items:
Remove an item by name or index from a specific category.
3. Displaying Items:
Print all items grouped by product type.
Display attributes like name, quantity, price, and total cost for each product.
4. Calculating Total Cost:
Compute and display the total cost of the shopping list.
5. Clearing the Shopping List:
Reset the shopping list by clearing all categories.
Optional tasks
Once your design is complete, test your implementation using the provided main.cpp (provides on next page) file. The main
function includes sample data for each product category and demonstrates the functionality of adding items, printing the
shopping list, and clearing it.

#include <iostream>
#include "Food.h"
#include "Electronics.h"
#include "Clothing.h"
#include "ShoppingList.h"

using namespace std;


int main() {
ShoppingList myList;

// Add Food items


myList.add(Food("Apples", 5, 1.50));
myList.add(Food("Milk", 2, 2.00));
myList.add(Food("Bread", 3, 1.20));
myList.add(Food("Cheese", 1, 5.50));
myList.add(Food("Tomatoes", 10, 0.80));

// Add Electronics items


myList.add(Electronics("Smartphone", 1, 800.00, "Samsung"));
myList.add(Electronics("Laptop", 1, 1200.00, "Dell"));
myList.add(Electronics("Headphones", 2, 50.00, "Sony"));
myList.add(Electronics("Bluetooth Speaker", 1, 100.00, "JBL"));
myList.add(Electronics("USB Charger", 3, 15.00, "Anker"));

// Add Clothing items


myList.add(Clothing("T-Shirt", 4, 10.00, "M"));
myList.add(Clothing("Jeans", 2, 30.00, "L"));
myList.add(Clothing("Jacket", 1, 80.00, "XL"));
myList.add(Clothing("Socks", 6, 2.00, "Free Size"));
myList.add(Clothing("Hat", 1, 15.00, "M"));

// Print the shopping list


myList.printList();
cout<<"Total Cost: "<<myList.totalCost()<<endl;

// Clear the shopping list


myList.clearList();

return 0;
}
Post-Lab Activity: Enhancing the Shopping List Program:

Enhance the Shopping List program by adding:

1. A Menu-Driven Interface: Allow users to interact with the program dynamically.


2. Persistent Data Storage: Save the shopping list data to a file so it can be loaded when the program is executed
later.

Task Description:

1. Menu System:
o Implement a user-friendly menu that provides options to:
 Add items to the shopping list.
 View the shopping list.
 Clear the shopping list.
 Save the shopping list to a file.
 Exit the program.

2. Persistent Storage:
o When the program starts, it should:
 Check for an existing data file (e.g., shopping_list.txt).
 If the file exists, read its contents and reconstruct the shopping list.
 If the file does not exist, start with an empty shopping list.
o When the program exits, it should save the current shopping list to the file, ensuring data persistence
for the next execution.

3. File Format:
o Use a simple text format for storing data, with clear separation between items and categories.

You might also like