Demonstrates use of class visibility labels. - C++ Class

C++ examples for Class:Member Access

Description

Demonstrates use of class visibility labels.

Demo Code

#include <iostream>
using namespace std;
#include  <math.h>
const float PI = 3.14159;  // Approximate value of pi.
// A sphere class.
class Sphere// ww  w  .  j  a v  a 2 s .co m
{
   private:
   float r;       // Radius of sphere
   float x, y, z;  // Coordinates of sphere
   float cube() {
      return (r * r * r);
   }
   float square() {
      return (r * r);
   }
   public:
   Sphere(float xcoord, float ycoord, float zcoord, float radius)
   {
      x = xcoord;
      y = ycoord;
      z = zcoord;
      r = radius;
   }
   ~Sphere()
   {
      cout << "Sphere (" << x << ", " << y << ", " << z << ", " << r << ") destroyed\n";
   }
   float volume()
   {
      return (cube() * 4 * PI / 3);
   }
   float surface_area()
   {
      return (square() * 4 * PI);
   }
};
void main()
{
   Sphere s(1.0, 2.0, 3.0, 4.0);
   cout << "The volume is " << s.volume() << "\n";
   cout << "The surface area is " << s.surface_area() << "\n";
   return;
}

Result


Related Tutorials