Practice

Named Planet


Planet

Design and code a class named Planet that holds information about a planet in our solar system.  Upon instantiation, a Planet object may receive a double-precision, floating-point value holding the mantissa part of the mass of the planet in kilograms and an integer holding the power of 10 by which the mantissa part must be multiplied to obtain the mass of the planet in kilograms (see the example below).  If these values are not provided or the mantissa part is not greater than of equal to 1.0 and less than 10.0, the object adopts a safe empty state. 

Define as const variables the mass of the sun, which is 1.989e30 kilograms, and the universal constant of gravitation, which is 6.674e-38 kilometers3 kilograms-1 seconds-2.  Include all of the prototypes necessary to ensure proper compilation and execution of your class declaration and implementation of your function definitions.

Your design includes the following member and helper functions:

  • double mass() const - a query that returns the mass of the planet in kilograms;
  • double gravity(double r) const - a query that returns the force of gravity between the planet and the sun, which is given by
     Force = G m msun / r2
    

    where G denotes the universal constant of gravitational attraction, m denotes the mass of the planet in kilograms, msun denotes the mass of the sun in kilograms, and r is the distance between the planet and the sun in kilometers;

  • an insertion operator that inserts into an output stream the mass of the planet in mantissa-exponent form to 2 decimal places as shown in the example below.  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 "Planet.h"

 int main ( ) {
     Planet earth(5.97, 24);

     cout << earth << endl;
     cout << "The force of gravity at the perihelion is " <<
      earth.gravity(1.5e8) << "N" << endl;

     return 0;
 }
 The mass of the planet is 5.97 x 10^24 kg
 The force of gravity at the perihelion is 35.22 N


NamedPlanet

Derive from your Planet class a class named NamedPlanet that holds information for a planet including its name. 

Upon instantiation, an NamedPlanet object may receive a double-precision, floating-point value holding the mantissa part of the mass of the planet in kilograms, an integer holding the power of 10 by which the mantissa part must be multiplied to obtain the mass of the planet in kilograms, and a null-terminated C-style string holding the name of the planet (see the example boelow).  The name of the planet is not 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 and helper functions:

  • const char* name() const - a query that returns the name of the planet;
  • an insertion operator that inserts into an output stream the name of the planet along with its mass in the format shown below.  Design your operator so that it allows cascading. 

For example, the following program uses your class and produces the output shown below

 #include <iostream>
 using namespace std;
 #include "NamedPlanet.h"

 int main ( ) {
     NamedPlanet earth(5.97, 24, "Earth");

     cout << earth << endl;
     cout << "The force of gravity at the perihelion is " <<
      earth.gravity(1.5e8) << " N" << endl;
     return 0;
 }
The mass of Earth is 5.97 x 10^24 kg
The force of gravity at the perihelion is 35.22 N






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