In: Computer Science
(C++ program)
Write and Design a Star Class definition.
Attribute of a Star are : - radius - age - coordinates in space: x,y.z Behaviors of a Star are: - calculateVolume() - calculateSurfaceArea() - calculateCircumference() - DisplayCoordinates() - DisplayStar()
be sure to supply constructor() method(s) and all your set() and get() methods. The above is only a generic description of the class Star. You will need to design the datatypes, method return values, method parameters, etc...
Sphere Formulas in terms of radius r:
Program Code Screenshot :


Sample Output :

Program Code to Copy
#include <iostream>
#include <cmath>
using namespace std;
class Star{
private:
double radius;
int age;
public:
Star(double r, int a){
radius = r;
age = a;
}
public double getRadius(){
return radius;
}
public void setRadius(double r){
radius = r;
}
public double getAge(){
return age;
}
public void setAge(int a){
age = a;
}
//calculate the volume
double calculateVolume(){
return (4.0/3)*M_PI*radius*radius*radius;
}
//Calculate the surface area
double calculateSurfaceArea(){
return 4*M_PI*radius*radius;
}
//Calculate the Circumference
double calculateCircumference(){
return 2*M_PI*radius;
}
void DisplayCoordinates(){
//Description not given
}
//Display the star attributes
void DisplayStar(){
cout<<"Star, radius : "<<radius<<", Age :
"<<age<<endl;
}
};
int main()
{
Star s(5.4,1000000);
s.DisplayStar();
cout<<"Circumference
"<<s.calculateCircumference()<<endl;
cout<<"Area
"<<s.calculateSurfaceArea()<<endl;
cout<<"Volume "<<s.calculateVolume()<<endl;
return 0;
}