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

Design, implement, and test a class that represents a trash can I C++. Your clas

ID: 3916867 • Letter: D

Question

Design, implement, and test a class that represents a trash can I C++. Your class must provide members for maximum trash items and current number of trash items (for example, a trash can having a maximum trash items of 5 can hold only 5 trash items -- when the can has 5 items and an attempt is made to add an item then the trash can must empty itself before adding the new item). The class must have a default constructor--the default constructor will use a NAMED CONSTANT to set the maximum trash items member to the default value of 5. You must have a second constructor that allows the user to choose a maximum item count other than the default (this maximum item count should be a parameter to the constructor). Your class must also include the following member methods: add a trash item, get current trash item count, and empty trash can. Use your new class in a small program that creates at least two trash can objects (one with the default constructor and one with the second constructor) and calls their member methods such that the methods are demonstrated properly (for example, create a trash can object with maximum size of 4, then call your add item method 5 times, then call the get item count method to ensure that the class is working properly.) Turn in output showing that your program methods do what they're supposed to do (I recommend putting output statements in your main program interspersed with the calls to your object methods. By tracing those statements, and comparing the value returned by your get current trash item count method, you can ensure that things are working properly. Be sure to use only named constants for all constants in the program. Be sure to comment your code. please include the output with text! Thank you!

Explanation / Answer

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts. Thanks

//Code

#include <iostream>

using namespace std;

//TrashCan class

class TrashCan {

    //attributes

    int max_trash_items;

    int current_trash_count;

    //named constant for default capacity

    const int DEFAULT_MAX_SIZE = 5;

public:

    //default constructor

    TrashCan()

    {

        //using default capacity

        max_trash_items = DEFAULT_MAX_SIZE;

        current_trash_count = 0;

    }

    //constructor which accepts custom maximum capacity

    TrashCan(int MAX_SIZE)

    {

        max_trash_items = MAX_SIZE;

        current_trash_count = 0;

    }

    //method to add a trash item to the trash can, return true if successful,

    //return false if trash can is full

    bool addTrashItem()

    {

        if (current_trash_count < max_trash_items) {

            //added

            current_trash_count++;

            return true;

        }

        //full

        return false;

    }

    //method to return current trash count

    int getCurrentTrashCount()

    {

        return current_trash_count;

    }

    //method to empty the trash can

    void emptyTrash()

    {

        current_trash_count = 0;

    }

};

int main()

{

    //creating a trash can using default constructor

    TrashCan trash1;

    //creating a trash can using parameterized constructor

    TrashCan trash2(10);

    //demonstrating the class methods

    cout << "Current trash count for first trash can: " << trash1.getCurrentTrashCount() << endl; //should be 0

    cout << "Adding trash items to first trash can until it is full" << endl;

    while (true) {

        if (!trash1.addTrashItem()) {

            break;

        }

    }

    cout << "Current trash count for first trash can: " << trash1.getCurrentTrashCount() << endl; //should be 5

    cout << "Current trash count for second trash can: " << trash2.getCurrentTrashCount() << endl; //should be 0

    cout << "Adding trash items to second trash can until it is full" << endl;

    while (true) {

        if (!trash2.addTrashItem()) {

          break;

        }

    }

    cout << "Current trash count for second trash can: " << trash2.getCurrentTrashCount() << endl; //should be 10

    cout << "Trying to add one more trash item to second can" << endl;

    trash2.addTrashItem();

    cout << "Current trash count for second trash can: " << trash2.getCurrentTrashCount() << endl; //should be 10

    return 0;

}

/*OUTPUT*/

Current trash count for first trash can: 0

Adding trash items to first trash can until it is full

Current trash count for first trash can: 5

Current trash count for second trash can: 0

Adding trash items to second trash can until it is full

Current trash count for second trash can: 10

Trying to add one more trash item to second can

Current trash count for second trash can: 10

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote