Here is the OOP Assignment No 02 using Classes
Question:
(a) Build a class CoffeeShots (representing a cup of coffee) having the following attributes
- type(string)
- price(double)
- volume(float)
- size(char)
(b) Provide a parameterized constructor with arguments for price and volume. For the sake of simplicity, assume that there is just one type of coffee named “Cappuccino” and therefore provide a default value for the type parameter in constructor so that the user may omit providing this value when creating an instance. Initialize all data to the values provided in the argument list. However, since there is no argument for size, you will have to assign it a value by yourself. The size attribute will get a char value based on the following condition:
- The size is ‘s’ if the volume of the coffee is between 0 and 50ml
- The size is ‘m’ if the volume is between 51 – 75ml
- The size is ‘l’ if the volume is greater than 75ml
(c) Provide getters for each of these attributes, however, setter will be provided only for price.
(d) Build a method upSize(). This method increases the volume of the coffee by 5ml and then resets the size accordingly so that the above conditions are still met. It also adds Rs. 5 to the price of the coffee.
(e) Write a non-member function (not belonging to this class) named createMyCofee().This method prompts the user for all the details required to create the coffee instance and creates a dynamic instance of the coffee with the provided data. It then returns a pointer to this coffee instance. Finally, use this returned pointer in main( ) function to invoke print( ) function that should print the order details i.e., coffee type, price, volume, and size information using appropriate getters function.
(f) Prototypes of the various functions are mentioned as follows:
Coffeeshots(double p, float v)
void setPrice(double price)
double getPrice( )
float getVolume( )
string getType( )
char getSize( )
void print( )
CoffeeShots* createMyCofee( )
Code:
The below is the code for OOP Assignment No 02 using Classes
using namespace std; class CoffeeShots { // member variables of a function string type; double price; float volume; char size; //member function prototype public: CoffeeShots(){type="Cappuccino";} CoffeeShots(double , float ); void setprice(double ); double getprice(); char getsize(); float getvolume(); string gettype(); void upsize(); void print(); }; // constructor used to initialize value CoffeeShots::CoffeeShots(double p, float v) { volume = v; price = p; type="Cappuccino"; } //setter function for price void CoffeeShots::setprice(double p) { price=p; } // getter function double CoffeeShots::getprice() { return price; } // getter function char CoffeeShots::getsize() { if (volume>0 && volume <=50) { size='s'; return size; } else if (volume>50 && volume <=75 ) { size='m'; return size; } else if (volume>75) { size = 'l'; return size; } } // getter function float CoffeeShots::getvolume() { return volume; } // getter function string CoffeeShots::gettype() { return type; } // getter function void CoffeeShots::upsize() { volume+=5; price+=5; } // print function void CoffeeShots::print() { cout<<"\nTHE TYPE OF COFFEE IS: "<<gettype(); cout<<"\nTHE PRICE OF COFFEE IS: "<<getprice(); cout<<"\nTHE VOLUME OF COFFEE IS: "<<getvolume(); cout<<"\nTHE SIZE OF COFFEE IS: "<<getsize(); } // this one is independent function CoffeeShots* createmyCoffee(){ double price; float volume; cout<<"Enter the price : "; cin>>price; cout<<"Enter the volume : "; cin>>volume; CoffeeShots* c= new CoffeeShots(price,volume); return c; } // from here the main starts int main() { char more; CoffeeShots* c = createmyCoffee(); c->print(); while(true) { cout<<"\n\nENTER Y TO ADD 5ml TO COFFEE OR N TO ABORT: "; cin>>more; if (more=='Y' || more=='y') { c->upsize(); c->print(); } else if (more=='N' || more =='n') return 0; else { cout<<"YOU ENTERED INVALID CHARACTER"; return 0; } } delete c; c=NULL; }
For other assignments and quizzes click here
For more details about Huffman coding click here
Output: