Answer to Question #184538 in C++ for Thandi

Question #184538

Implement a class named Bottle, which has two int data members; one to represent the maximum capacity of a bottle and the other to represent the quantity of liquid the bottle contains at a given time. Include the following member functions in the class Bottle:

• to set the maximum capacity of a bottle (void setCapacity(int c))

• to set the current quantity of liquid in the bottle (void setQuantity(int q))

• to access the maximum capacity of the bottle (int getCapacity())

• to access the quantity of liquid currently in the bottle (int getQuantity())

• to fill a bottle completely (void fill())

• to empty the bottle (void empty())

Write a main function to test the Bottle class. It should create at least two Bottle objects and include statements to test all the member functions of Bottle using the created Bottle objects.


1
Expert's answer
2021-04-23T07:59:28-0400
#include <iostream>
using namespace std;
class Bottle{
    int max_capacity, level;
    public:
    Bottle(){
        max_capacity = 0;
        level = 0;
    }
    void setCapacity(int c){
        this->max_capacity = c;
    }
    void setQuantity(int q){
        this->level = q;
    }
    int getCapacity(){
        return this->max_capacity;
    }
    int getQuantity(){
        return this->level;
    }
    void fill(){
        this->level = max_capacity;
    }
    void empty(){
        this->level = 0;
    }
};
int main(){
    Bottle A, B;
    A.setCapacity(100);
    B.setCapacity(50);
    cout<<"The capacity of bottle A is "<<A.getCapacity();
    cout<<"\nThe capacity of bottle B is "<<B.getCapacity();
    A.setQuantity(6);
    B.setQuantity(45);
    cout<<"\nThe current quantity of liquid in A is "<<A.getQuantity();
    cout<<"\nThe current quantity of liquid in B is "<<B.getQuantity();
    cout<<"\n\nFilling up both bottles...";
    A.fill();
    B.fill();
    cout<<"\n\nThe current quantity of liquid in A is "<<A.getQuantity();
    cout<<"\nThe current quantity of liquid in B is "<<B.getQuantity();
    cout<<"\n\nEmptying both bottles...";
    A.empty();
    B.empty();
    cout<<"\n\nThe current quantity of liquid in A is "<<A.getQuantity();
    cout<<"\nThe current quantity of liquid in B is "<<B.getQuantity();
    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS