![]() |
Data Abstraction and Problem Solving with C++Walls and Mirrorsby Frank M. Carrano |
![]() |
Sphere.cppGo to the documentation of this file.00001 00018 #include <iostream> 00019 #include "Sphere.h" // header file 00020 00021 using namespace std; 00022 00023 Sphere::Sphere() : theRadius(1.0) 00024 { 00025 } // end default constructor 00026 00027 Sphere::Sphere(double initialRadius) 00028 { 00029 if (initialRadius > 0) 00030 theRadius = initialRadius; 00031 else 00032 theRadius = 1.0; 00033 } // end constructor 00034 00035 void Sphere::setRadius(double newRadius) 00036 { 00037 if (newRadius> 0) 00038 theRadius = newRadius; 00039 else 00040 theRadius = 1.0; 00041 } // end setRadius 00042 00043 double Sphere::getRadius() const 00044 { 00045 return theRadius; 00046 } // end getRadius 00047 00048 double Sphere::getDiameter() const 00049 { 00050 return 2.0 * theRadius; 00051 } // end getDiameter 00052 00053 double Sphere::getCircumference() const 00054 { 00055 return PI * getDiameter(); 00056 } // end getCircumference 00057 00058 double Sphere::getArea() const 00059 { 00060 return 4.0 * PI * theRadius * theRadius; 00061 } // end getArea 00062 00063 double Sphere::getVolume() const 00064 { 00065 double radiusCubed = theRadius * theRadius * theRadius; 00066 return (4.0 * PI * radiusCubed)/3.0; 00067 } // end getVolume 00068 00069 void Sphere::displayStatistics() const 00070 { 00071 cout << "\nRadius = " << getRadius() 00072 << "\nDiameter = " << getDiameter() 00073 << "\nCircumference = " << getCircumference() 00074 << "\nArea = " << getArea() 00075 << "\nVolume = " << getVolume() << endl; 00076 } // end displayStatistics 00077 // End of implementation file. |