#include
#include
using namespace std;
const float Pi = 3.1415;
class C {
public:
C(float r, float h) :Radius(r), high(h) {}
protected:
float Radius;
float high;
};
class Round :public C {
public:
void supArea();
void volume();
};
void Round::supArea() {
float s;
s = 4 * Pi*pow(C::Radius, 2);
cout << "\n球的表面積為:" << s << endl;
}
void Round::volume() {
float v;
v = 4 * Pi*pow(C::Radius, 3) / 3;
cout << "\n球的體積為:" << v << endl;
}
class Cylinder :public C {
public:
void supArea();
void volume();
};
void Cylinder::supArea() {
float s;
s = 2 * Pi*pow(C::Radius, 2) + C::high * 2 * Pi*C::Radius;
cout << "\n圓柱的表面積為:" << s << endl;
}
void Cylinder::volume() {
float v;
v = Pi*pow(C::Radius, 2)*C::high;
cout << "\n圓柱的體積為:" << v << endl;
}
class Cone :public C {
public:
void supArea();
void volume();
};
void Cone::supArea() {
float s;
s = Pi*pow(C::Radius, 2) + Pi*C::Radius*C::high;
cout << "\n圓錐的表面積為:" << s << endl;
}
void Cone::volume() {
float v;
v = 1 / 3 * Pi*pow(C::Radius, 2)*sqrt(pow(C::high, 2) - pow(C::Radius, 2));
cout << "\n圓錐的體積為:" << v << endl;
}
int main() {
C q(1, 2);
Round r;
r.supArea();
r.volume();
Cylinder c1;
c1.supArea();
c1.volume();
Cone c2;
c2.supArea();
c2.volume();
return 0;
}
#include
using namespace std;
const float Pi = 3.1415;
class C {
public:
C(float r, float h) :Radius(r), high(h) {}
protected:
float Radius;
float high;
};
class Round :public C {
public:
Round(float r,float h):C(r,h){}
void supArea();
void volume();
};
void Round::supArea() {
float s;
s = 4 * Pi*pow(C::Radius, 2);
cout << "\n球的表面積為:" << s << endl;
}
void Round::volume() {
float v;
v = 4 * Pi*pow(C::Radius, 3) / 3;
cout << "\n球的體積為:" << v << endl;
}
class Cylinder :public C {
public:
Cylinder(float r,float h):C(r,h){}
void supArea();
void volume();
};
void Cylinder::supArea() {
float s;
s = 2 * Pi*pow(C::Radius, 2) + C::high * 2 * Pi*C::Radius;
cout << "\n圓柱的表面積為:" << s << endl;
}
void Cylinder::volume() {
float v;
v = Pi*pow(C::Radius, 2)*C::high;
cout << "\n圓柱的體積為:" << v << endl;
}
class Cone :public C {
public:
Cone(float r,float h):C(r,h){}
void supArea();
void volume();
};
void Cone::supArea() {
float s;
s = Pi*pow(C::Radius, 2) + Pi*C::Radius*C::high;
cout << "\n圓錐的表面積為:" << s << endl;
}
void Cone::volume() {
float v;
v = 1 / 3 * Pi*pow(C::Radius, 2)*sqrt(pow(C::high, 2) - pow(C::Radius, 2));
cout << "\n圓錐的體積為:" << v << endl;
}
int main() {
Round r(1,2);
r.supArea();
r.volume();
Cylinder c1(1,2);
c1.supArea();
c1.volume();
Cone c2(1,2);
c2.supArea();
c2.volume();
return 0;
}