/* ************************************************************************** * Program name : 026_Inline_function (Version 1.00) * * Author : Duck Wong * * Language : C / C++ * * Compiler : Boodshed Dec-C++ compiler Ver 3.95 * * Computer : PII350 * * O/S : Windows 98 * ************************************************************************** * Version 1.00 : 2000/06/11 - first version * ************************************************************************** * Description : (a) Input the radius * * (b) Using an inline function to calculate * * the surface area and volumn of a sphere * * (c) Try again ? * ************************************************************************** */ #include #include const float PI=3.14159; // Note (1) inline float getarea(const float Radius) // Note (2) {return 4*PI*Radius*Radius*Radius;} // Note (3) inline float getvolumn (const float Radius) // Note (2) {return 4*PI*Radius*Radius*Radius/3;} // Note (4) int main() { // part 1 : declaration float Radius, SurfaceArea, Volumn; char Again; do { // part 2 : Repeat asking the size of the checkerboard pattern do { cout << "\nInput the radius of a sphere : "; cin >> Radius; if (Radius<=0) cout << "\nMust be bigger than zero!\n"; } while (Radius <= 0); // part 3 : Calculate the total surface area and volumn cout << "\nThe total surface area is " << getarea(Radius) << "\nThe volumn of this sphere is " << getvolumn(Radius) << "\n" << endl; // part 4 : try another number ? cout << "\aTry another number (Y/N) : "; cin >> Again; cout << "\n"; } while (Again=='Y' || Again=='y'); cout << "\n" << endl; system("PAUSE"); return 0; } /* Notes (1) The variable PI is defined as a "const" of data type float. This is a Global variable since it is defined before the function main. (2) The qualifier "inline" before the function's return type in the function definition "advises" the compiler to generate a copy of the function's code in place to avoid a function call. The keyword "const" on the parameter list of functions getarea and getvolumn tells the compiler that the functions does not modify the value of the variable Radius. (3) The total surface area of a sphere is : 4 X pi X Radius^3 (4) The volumn of a sphere is : 4/3 X pi X Radius^3 */