C++ Constructor function

Description

C++ Constructor function

#include  <iostream.h>
const float PI = 3.14159;  // Approximate value of pi.
// A sphere class.
class Sphere// w w w .j  ava2s .c o  m
{
   public:
   float r;          // Radius of sphere
   float x, y, z;  // Coordinates of sphere
   Sphere(float xcoord, float ycoord, float zcoord, float radius)
   {
      x = xcoord; y = ycoord; z = zcoord; r = radius;
   }
   ~Sphere() { }
   float volume()
   {
      return (r * r * r * 4 * PI / 3);
   }
   float surface_area()
   {
      return (r * r * 4 * PI);
   }
};
void main()
{
   Sphere s(1.0, 2.0, 3.0, 4.0);
   cout << "X = " << s.x << ", Y = " << s.y << ", Z = " << s.z << ", R = " << s.r << "\n";
   return;
}



PreviousNext

Related