Classes for Furniture
Here is the GitHub classroom assignment invitation: link
Here are the teams: teams
Lab Objective
- Understand and implement basic class inheritance in C++.
- Create a hierarchy of classes representing different types of furniture.
- Use class inheritance to manage and track inventory details.
Lab Instructions
Step 1: Create a Base Class Furniture
Create a simple base class named Furniture with the following attributes and methods:
- Attributes:
- string name: The name of the furniture item.
- double price: The price of the furniture item.
- int quantity: The number of items available in the inventory.
- Methods:
- Constructor to initialize all attributes.
- Getter and Setter methods for all attributes.
- A display() method to print the furniture details.
#include <iostream>
#include <string>
class Furniture {
protected:
std::string name;
double price;
int quantity;
int inventoryID; // New attribute for unique inventory ID
private:
static int nextID; // Static class variable to generate unique IDs
public:
// Constructor to assign inventory ID and increment nextID
Furniture(std::string n, double p, int q)
: name(n), price(p), quantity(q), inventoryID(nextID++) {}
// Virtual destructor to ensure proper cleanup in derived classes
virtual ~Furniture() {}
// Getters
std::string getName() const { return name; }
double getPrice() const { return price; }
int getQuantity() const { return quantity; }
int getInventoryID() const { return inventoryID; } // Getter for inventoryID
// Setters
void setName(const std::string& n) { name = n; }
void setPrice(double p) { price = p; }
void setQuantity(int q) { quantity = q; }
// Virtual display method
virtual void display() const {
std::cout << "Inventory ID: " << inventoryID << std::endl;
std::cout << "Name: " << name << ", Price: $" << price
<< ", Quantity: " << quantity << std::endl;
}
};
// Initialize static variable
int Furniture::nextID = 1;
Step 2: Create Derived Classes
Create derived classes Chair, Table, and Sofa that inherit from Furniture. Each derived class should add an additional attribute specific to that furniture type.
- Chair: Add an attribute bool hasArmrest to indicate if the chair has armrests.
- Table: Add an attribute int numOfLegs to indicate the number of legs the table has.
- Sofa: Add an attribute int numOfSeats to indicate how many people can sit on the sofa.
For each derived class:
- Add appropriate constructors.
- Add getter and setter methods for the new attributes.
- Override the display() method to include the new attribute in the output.
#include "Furniture.h"
class Chair : public Furniture {
private:
bool hasArmrest;
public:
Chair(std::string n, double p, int q, bool armrest)
: Furniture(n, p, q), hasArmrest(armrest) {}
bool getHasArmrest() const { return hasArmrest; }
void setHasArmrest(bool armrest) { hasArmrest = armrest; }
void display() const override {
Furniture::display(); // Display inherited attributes
std::cout << "Armrest: " << (hasArmrest ? "Yes" : "No") << std::endl;
}
};
class Table : public Furniture {
private:
int numOfLegs;
public:
Table(std::string n, double p, int q, int legs)
: Furniture(n, p, q), numOfLegs(legs) {}
int getNumOfLegs() const { return numOfLegs; }
void setNumOfLegs(int legs) { numOfLegs = legs; }
void display() const override {
Furniture::display();
std::cout << "Number of Legs: " << numOfLegs << std::endl;
}
};
class Sofa : public Furniture {
private:
int numOfSeats;
public:
Sofa(std::string n, double p, int q, int seats)
: Furniture(n, p, q), numOfSeats(seats) {}
int getNumOfSeats() const { return numOfSeats; }
void setNumOfSeats(int seats) { numOfSeats = seats; }
void display() const override {
Furniture::display();
std::cout << "Number of Seats: " << numOfSeats << std::endl;
}
};
Below is a UML diagram of the classes define above:

Step 3: Enhance system
A. Consider some additional attributes for the Furniture class. Add 3 new attributes, choose 2 from below, and then create another that makes sense to be in the base class.
1. Material
- Type:
std::string - Description: Specifies the material from which the furniture is made (e.g., wood, metal, plastic, fabric).
- Example:
"Wood","Metal"
2. Dimensions
- Type: A struct or individual attributes for
length,width, andheight(e.g.,double length,double width,double height) - Description: Specifies the physical dimensions of the furniture item, which can be important for determining how much space it occupies.
- Example:
Dimensions {length = 2.0, width = 1.5, height = 0.8}
struct Dimensions {
double length;
double width;
double height;
};
3. Style
- Type:
std::string - Description: Describes the style of the furniture item (e.g., modern, traditional, rustic).
- Example:
"Modern","Victorian"
4. Category
- Type:
std::string - Description: Specifies the category or type of furniture (e.g., seating, storage, tables).
- Example:
"Seating","Storage"
B. Consider some possible attributes for the sofa class. Pick 2 and add 1 addition of your own that makes sense. Then add 3 that make sense for each of class Chair and class Table
Additional Attributes for the Sofa Class
- Type
- Type:
std::string - Description: Specifies the type of sofa, such as sectional, loveseat, recliner, sleeper, etc.
- Example:
"Sectional","Loveseat","Sleeper"
- Type:
- Upholstery Material
- Type:
std::string - Description: Specifies the material used for the upholstery of the sofa, such as leather, fabric, microfiber, etc.
- Example:
"Leather","Fabric","Microfiber"
- Type:
- Cushion Fill Material
- Type:
std::string - Description: Describes the material used to fill the cushions, such as foam, down, or a combination.
- Example:
"High-density foam","Down feathers"
- Type:
- Frame Material
- Type:
std::string - Description: Specifies the material used for the frame of the sofa, such as wood, metal, or engineered wood.
- Example:
"Solid wood","Metal","Plywood"
- Type:
- Seating Capacity
- Type:
int - Description: Specifies how many people the sofa is designed to seat comfortably.
- Example:
3,5
- Type:
- Reclining Mechanism
- Type:
bool - Description: Indicates whether the sofa has a reclining feature.
- Example:
true,false
- Type:
- Removable Covers
- Type:
bool - Description: Indicates whether the sofa’s cushion covers are removable and washable.
- Example:
true,false
- Type:
- Storage Compartment
- Type:
bool - Description: Indicates whether the sofa has built-in storage compartments, such as under the seats.
- Example:
true,false
- Type:
C. Come up with 2 additional types of furniture, and create a class for each. Make sure each has at least 5 attributes that makes sense for that tpe of furniture.
D. User drawio in google docs to make a UML class diagram of you complete system.
E. Build a test environment. Create a test function LoadFurniture that reads a datafile with information on furniture. For each it it creates and constructs the appropriate class, then calls the display on it, as well as each of the other functions. This will test the the classes. We will will be adding to this class in the next lab.
The file should be a CSV, comma separated values. Here is some sample a C++ code to parse a comma separated list (ParseCSVLineString).
Your LoadFurniture function will need to read and parse each line, then look at the furniture type and call the appropriate class constructor with parameters for each. It should then test the functions for that object type.
Your file will look something like this:
sofa, Luxury Lounger, 389.77, 8, 3, (other fields)
table, Scandinavian Side Table, 899.99, 6, 4, (other fields)
...
You will need to make a file of test data. Have at least 3 items of each category. Challenge: Can you use ChatGPT to generate test data? If so you can create a LOT of test data.
Step 5 Turn In
- Submit project to GitHub classrooms
- Include in the project a file “testruns.txt” with the output of your test run that tests everything.
- Upload the UML diagram to the github project.
Grading
| Requirements | Grading Comments | Points | Score |
|---|---|---|---|
| Good Code Design | 20 | ||
| Complete working code | 20 | ||
| Tests for all functions including common and boundry cases. | 30 | ||
| A complete set of test cases in a CSV (comma separated values) text file. | 10 | ||
| Class Diagrams | 10 | ||
| Comments | 10 | ||
| Total | 100 |
Remember the general requirements that are expected in every lab assignment in this course that are explained in Lab 1
