In: Computer Science
rite the code for a class named Funnel. A funnel is a cone-shaped object that is used to pour liquids into small openings. For the purpose of this question, you may assume that a Funnel is an inverted cone (see image at left) and has a flap (a lid) at the bottom that can be opened and closed. A Funnel has the following PUBLIC features: a) A Funnel created without any data has a radius of 12, a height of 20, units measured in "millilitres", and . b) Alternatively, a Funnel can be created by accepting 2 whole numbers representing the radius and height, and has units in "ounces" . All Funnels start out in a closed position and are empty (i.e. have a volume of 0.00).The formula for calculating the volume of a cone is: V = 1/3 * Pi * R^2 * H where V = Volume, Pi = 3.14159, R^2 = radius squared, and H = height ($0.00) c) A Funnel contains a function changeFlap( ) that opens a closed flap and closes an open flap and returns nothing (1 mark). d) A Funnel also contains a function pour(int seconds) that empties the Funnel according to the formula: f = 2.5/R per second (where f = amount poured, R = radius), but ONLY FOR Funnel's that have OPEN FLAPS! The function also displays the amount poured for each second, and finally returns the total amount poured. So, for a default Funnel with a volume of 3015.9264, the function pour(2) would display: after 1 secs, poured 0.0075 millilitres, volume now at 3015.9189 after 2 secs, poured 0.0075 millilitres, volume now at 3015.9114 The function would return 0.015 for THIS EXAMPLE ONLY! If the Funnel's flap is closed, then only the line: "Sorry Funnel flap is closed!" is displayed, and 0.00 would be returned. This function permanently reduces the Funnel's volume . e) A Funnel contains another function pour( ) that computes and returns the exact number of seconds (including fractional portions) required to completely empty the Funnel . f) The postfix ++ operator increases the Funnel's radius by 1 and returns the Funnel's new volume . g) The prefix ++ operator increases the Funnel's height by 1 and returns the Funnel's new volume . h) A function called calcVolume( ) that computes and returns the Funnel's volume . i) A function called getVolume( ) that computes and returns the Funnel's current volume . For example, the following program: int main( ) { Funnel f1, f2(6, 15); float amount, volumeF1, volumeF2, seconds; volumeF1 = f1++; // sets Funnel f1's radius to 13 volumeF2 = ++f2; // sets Funnel f2's height to 16 cout << "f1's volume is: " << volumeF1 << endl; cout << "f2's volume is: " << volumeF2 << endl; amount = f1.pour(5); // flap is closed, so does not pour anything f1.changeFlap( ); amount = f1.pour(5); cout << "amount poured from Funnel f1: " << amount << endl; cout << "------------------------------------------------" << endl; seconds = f2.pour( ); cout << "it takes " << seconds << " seconds to empty Funnel f2" << endl; f2.changeFlap( ); f2.pour(3); cout << "f1's volume is: " << f1.getVolume( ) << endl; cout << "f2's volume is: " << f2.getVolume( ) << endl; return 0; } would display: f1's volume is: 3539.52 f2's volume is: 603.185 Sorry Funnel flap is closed! after 1 secs, poured 0.192308 millilitres, volume now at 3539.33 after 2 secs, poured 0.192308 millilitres, volume now at 3539.14 after 3 secs, poured 0.192308 millilitres, volume now at 3538.95 after 4 secs, poured 0.192308 millilitres, volume now at 3538.76 after 5 secs, poured 0.192308 millilitres, volume now at 3538.56 amount poured from Funnel f1: 0.961538 ------------------------------------------------ it takes 1447.64 seconds to empty Funnel f2 after 1 secs, poured 0.416667 ounces, volume now at 602.769 after 2 secs, poured 0.416667 ounces, volume now at 602.352 after 3 secs, poured 0.416667 ounces, volume now at 601.935 f1's volume is: 3538.56 f2's volume is: 601.935
#include <iostream>
using namespace std;
int flap=0;
class Funnel {
public:
Funnel(){ //default constructor
radius=12;
height=20;
volume= ((3.14*radius*radius*height)/3)/29.5735; //unit in millilitres
}
Funnel(int rad, int hght){ //parametrized constructor
radius=rad;
height=hght;
volume= (3.14*radius*radius*height)/3; //unit in ounces
}
//function to open and close flap
void changeFlap(){
if(flap==0)
flap=1;
else
flap=0;
}
//calculates the volume after every second, the liquid is poured out
float pour(int seconds){
float f= 0.00;
int i=1;
if(flap==0){
cout<<"Sorry, Flap is closed!";
return f;
}
else {
while(i <= seconds){
f= (2.5/ radius)/29.5735;
volume= volume-f;
cout<<"after "<<i<<" seconds, poured"<<f<<" millilitres, volume now at"<<volume;
return (f*seconds);
}
}
//calculates the time required to empty the funnel
float pour(){
float time;
time= (radius * volume) /2.5;
return time;
}
// Overload postfix ++ operator
friend Funnel operator++(Funnel &ob)
{
ob.radius++;
return ( (3.14*radius*radius*height)/3);
}
// Overload postfix ++ operator
friend Funnel operator++(Funnel &ob, int h)
{
++ob.height;
return ( (3.14*radius*radius*height)/3);
}
//calculates the volume
float calcVolume(){
return ( (3.14*radius*radius*height)/3);
}
//returns the current volume
float getVolume(){
return volume;
}
private:
int radius; // radius of the funnel
int height; // Height of the funnel
float volume; // volume of the funnel
};