C++建立正四面体类,计算边长为a的正四面体的体积、表面积
2个回答
展开全部
//上面那位是正方体,不是正四面体
#include<iostream>
#include<cmath>
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<<RT.GetVolume()<<endl;
cout<<RT.GetSuperficialArea()<<endl;
}
#include<iostream>
#include<cmath>
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<<RT.GetVolume()<<endl;
cout<<RT.GetSuperficialArea()<<endl;
}
展开全部
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;
}
{
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;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询