C++建立正四面体类,计算边长为a的正四面体的体积、表面积

2025-06-24 03:43:50
推荐回答(2个)
回答1:

//上面那位是正方体,不是正四面体
#include
#include
using namespace std;
class CRegulartetrahedron
{
public:
CRegulartetrahedron():edge(0),height(0){}
CRegulartetrahedron(double x):edge(x)
{
height=sqrt(6.0)/3.0*edge;
}

double GetVolume()//体积
{
return 1.0/2.0*edge*sqrt(3.0)/2.0*edge*height*1.0/3.0;//底面积乘以高除以三分之一
}
double GetSuperficialArea()//表面积
{
return 4.0*1.0/2.0*edge*sqrt(3.0)/2.0*edge;//四个面积之和
}
private:
double edge;//边长
double height;
};
void main()
{
CRegulartetrahedron RT(2);
cout< cout<}

回答2:

class fang4
{
public:
int tj();
int mj();
int init();
private:
int a;
};
int fang4::init()
{
cin >> a;
return a;
}
int fang4::mj()
{
return a*a*6;
}
int fang4::tj()
{
return a*a*a;
}

int main()
{
fang4 m;
m.init();
printf("mj:%d tj:%d\n", m.mj(), m.tj());
system("pause");
return 0;
}