Practice

Easter Egg


Egg

Design and code a class named Egg that holds information about an egg.  Upon instantiation, an Egg object may receive a double-precision, floating-point value holding the mass of the egg in grams, a double-precision, floating-point value holding the short radius of the egg in centimetres and a double-precision, floating-point value holding the long radius of the egg in centimetres.  If no information is provided or any value is non-positive, the object assumes a safe empty state. 

Include all of the statements necessary to ensure the proper compilation of your class declaration and the implementation of its functions.

Your design includes the following member functions:

  • double volume() const - a query that returns the volume of the egg, which is given by
     Volume = (4/3) pi a2 b
    
    where pi denotes one half the circumference of a circle of unit radius (3.1415926...), a denotes the short radius of the egg and b denotes the long radius of the egg;
  • double density() const - a query that returns the average density of the egg, which is given by
     Density = m / V
    
    where m denotes the mass of the egg and V denotes the volume of the egg;
Your design includes an insertion operator that inserts into an ostream a line with the following information:
  • the volume of the egg in a field of 6 with 2 decimal points;
  • the density of the egg in a field of 5 with 2 decimal points;
Design your operator so that it handles cascading.  For example, the following program uses your class and produces the output shown below
 #include <iostream>
 using namespace std;
 #include "Egg.h"

 int main ( ) {
     Egg e(100.00, 2.00, 3.00), f(50.00, 1.00, 1.00);

     cout << e << f << endl;

     return 0;
 }
 The volume of this egg is  50.27 cm3, its density is  1.99 gm/cm3
 The volume of this egg is   4.19 cm3, its density is 11.94 gm/cm3

EasterEgg

Derive from your Egg class a class named EasterEgg that holds information for an Easter egg.  Design your derived class in such a way that it does not require

  • any friendship relations,
  • protected or public access for any data member of the base class, or
  • any function other than one listed in the specification here and for the base class.
Upon instantiation, an EasterEgg object may receive a null-terminated C-style string holding a description of the artwork on the surface of the egg, a double-precision, floating-point value holding the original mass of the egg in grams, a double-precision, floating-point value holding the short radius of the egg in centimetres, a double-precision, floating-point value holding the long radius of the egg in centimetres.  If no information is provided or any numerical argument is non-positive, the object assumes a safe empty state.  Do not assume that the description is limited to any number of characters. 

Include all of the functions necessary to ensure the proper copying and assignment of the data from one object to another and to avoid memory leaks.

Your design includes the following member functions:

  • bool remove(double discard) - a modifier that removes a mass of discard value from the egg.  If discard is positive-valued and your object contains at least this mass, your function removes that mass and returns true.  If discard is non-positive or your object contains less than this mass, your function does nothing and returns false;
  • const char* description() const - a query that returns the description of the artwork;
Your design also includes an insertion operator that inserts into an ostream the following information:
  • the volume of the egg in a field of 6 with 2 decimal points;
  • the density of the egg in a field of 5 with 2 decimal points;
  • the description of the artwork on the egg on a separate line.
Design your operator so that it handles cascading.  For example, the following program uses your class and produces the output shown below
 #include <iostream>
 using namespace std;
 #include "EasterEgg.h"

 int main ( ) {
     EasterEgg a("Floral Design", 100.00, 2.0, 3.0);

     a.remove(80.0);
     a.remove(25.0);
     cout << a << endl;
     return 0;
 }
 The volume of this egg is  50.27 cm3, its density is  0.40 gm/cm3
 This Easter egg has a Floral Design






  Designed by Chris Szalwinski   Copying From This Site   
Logo
Creative Commons License