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.
#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;
}
Comments
Leave a comment