/*--------------------- Planet.cc: Planet class ---------------------- class Planet models a few features of planets. (Clearly this is an extreme simplification of astronomy; the idea is to illustrate how C++ can model a class of objects.) Class declaration is in planet.h, test driver in planetst.cc. Programmer: John Chapman For chapter 16 of "The Universal Machine" (multimedia) Copyright (C) McGraw-Hill/Primus, 2003 --------------------------------------------------------------------*/ #include #include #include #include //#include "Planet.h" /*--------------------- Planet.h: Planet class ---------------------- class Planet models a few features of planets. (Clearly this is an extreme simplification of astronomy; the idea is to illustrate how C++ can model a class of objects.) Implementation of constructor and member functions is in planet.cc. Test driver for this class is in planetst.cc. Programmer: John Chapman For chapter 14 of "The Universal Machine" (multimedia) Copyright (C) WCB/McGraw-Hill, 1998 --------------------------------------------------------------------*/ class Planet{ public: Planet(float rad); //constructor float volume(); //public member functions float surface_area(); private: float radius; //private data member }; //Class::Constructor(parameters) Planet::Planet(float rad) //constructor { radius = rad; //initialize data member radius } //returnType Class::functionName() float Planet::volume() { float vol; vol = 4/3*PI*pow(radius,3); //compute the volume return vol; } float Planet::surface_area() { float area; area = 4*PI*pow(radius,2); //compute the surface area. return area; } /*------------------ planetst.cc: test Planet class ------------------ Test driver for the Planet class Class declaration is in planet.h, implementation in planet.cc. Programmer: John Chapman For chapter 14 of "The Universal Machine" (multimedia) Copyright (C) WCB/McGraw-Hill, 1998 --------------------------------------------------------------------*/ void main() { Planet Knobby(45.0); //Suppose Knobby were a Planet cout << "The volume of this planet is: " << Knobby.volume() << endl; cout << "The surface area of this planet is: " << Knobby.surface_area() << endl; cout << "Enter a number to quit:"; int in; cin >> in; //wait for input }