C++ 完善求三维空间内的表面积和体积

//加入了对长方体,立方体,球体,圆柱及三棱柱的体积与表面积计算
#include"iostream"
#include"cmath"
using namespace std;

class line//线
{
public:
	line(float len);
	virtual float area() = 0;
	virtual float volume() = 0;
protected:
	float m_len;
};
line::line(float len):m_len(len)
{

}//类作为纯虚函数,无法被实例化

class rec :public line//方形
{
public:
	rec(float len, float width);
	float area();
protected:
	float m_width;
};
rec::rec(float len, float width):line(len), m_width(width)
{

}
float rec::area() 
{
	return m_len * m_width;
}

class cuboid :public rec//长方体
{
public:
	cuboid(float len, float width, float height);
	float area();
	float volume();
protected:
	float m_height;
};
cuboid::cuboid(float len, float width, float height) :rec(len, width), m_height(height)
{

}
float cuboid::area()
{
	return 2 * (m_len * m_width + m_len * m_height + m_height * m_width);

}
float cuboid::volume()
{
	return m_len * m_height * m_width;
}

class cube :public cuboid//立方体
{
public :
	cube(float len);
};
cube::cube(float len) :cuboid(len, len, len)
{

}

class cir :public line//圆
{
public:
	cir(float len);
	float area();
};
cir::cir(float len) :line(len)
{

}
float cir::area()
{
	float q = acos(-1);
	return m_len * m_len * q;
}

class glo :public cir//球体
{
public:
	glo(float len);
	float area();
	float volume();
};
glo::glo(float len) :cir(len)
{

}
float glo::area()
{
	float q = acos(-1);
	return 4 * m_len * m_len * q;
}
float glo::volume()
{
	float q = acos(-1);
	return 4 * q * m_len * m_len * m_len/3;
}

class cyl :public cir//圆柱
{
public:
	cyl(float len,float height);
	float area();
	float volume();
protected:
	float m_height;
};
cyl::cyl(float len, float height) :cir(len), m_height(height)
{

}
float cyl::area()
{
	float q = acos(-1);
	return m_len * m_len * q * 2 + 2 * q * m_len * m_height;
}
float cyl::volume()
{
	float q = acos(-1);
	return m_len * m_len * q * m_height;
}



class tri :public line//三角形
{
public:
	tri(float len, float len2, float len3);
	float area();
protected:
	float m_len2;
	float m_len3;
};
tri::tri(float len, float len2, float len3) : line(len), m_len2(len2),m_len3(len3)
{

}
float tri::area()
{
	float q = (m_len + m_len2 + m_len3) * (m_len + m_len2 - m_len3) * (m_len - m_len2 + m_len3) * (-m_len + m_len2 + m_len3);
	return sqrt(q)/4;
}

class tria :public tri//直三棱柱
{
public:
	tria(float len, float len2, float len3,float height);
	float area();
	float volume();
protected:
	float m_height;
};
tria::tria(float len, float len2, float len3, float height) :tri(len, len2, len3), m_height(height)
{

}
float tria::area()
{
	float q = (m_len + m_len2 + m_len3) * (m_len + m_len2 - m_len3) * (m_len - m_len2 + m_len3) * (-m_len + m_len2 + m_len3);
	return sqrt(q)/2+ (m_len + m_len2 + m_len3) * m_height;
}
float tria::volume()
{
	float q = (m_len + m_len2 + m_len3) * (m_len + m_len2 - m_len3) * (m_len - m_len2 + m_len3) * (-m_len + m_len2 + m_len3);
	return sqrt(q) * m_height/4;
}

int main()
{

	line* p = new cuboid(10, 20 ,30);
	cout << "长方体表面积: " << p->area() << endl;
	cout << "长方体体积: " << p->volume() << endl;
	cout << endl;
	p = new cube(15);
	cout << "正方体表面积: " << p->area() << endl;
	cout << "正方体体积: " << p->volume() << endl;
	cout << endl;
	p = new glo(10);
	cout << "球表面积: " << p->area() << endl;
	cout << "球体积: " << p->volume() << endl;
	cout << endl;
	p = new cyl(10, 10);
	cout << "圆柱表面积: " << p->area() << endl;
	cout << "圆柱体积: " << p->volume() << endl;
	cout << endl;
	p = new tria(3,4,5,100);
	cout << "三棱柱表面积: " << p->area() << endl;
	cout << "三棱柱体积: " << p->volume() << endl;
	cout << endl;
	return 0;
}

运行结果:

长方体表面积: 2200
长方体体积: 6000

正方体表面积: 1350
正方体体积: 3375

球表面积: 1256.64
球体积: 4188.79

圆柱表面积: 1256.64
圆柱体积: 3141.59

三棱柱表面积: 1212
三棱柱体积: 600

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值