Skip to content

Commit 87a93d2

Browse files
committed
vault backup: 2025-04-14 18:09:31
1 parent b4dac15 commit 87a93d2

File tree

1 file changed

+95
-0
lines changed

1 file changed

+95
-0
lines changed

src/develop/CPP/3_Class&object.md

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -853,5 +853,100 @@ int main() {
853853
## 8 作业
854854
855855
按需求设计一个圆类输入圆的半径和圆柱的高,依次输出圆周长、圆面积、圆球表面积、圆柱体积(以空格分隔,π取 3.14)。
856+
```cpp
857+
#include <iostream>
858+
859+
class circle
860+
{
861+
private:
862+
/* data */
863+
float radius; //圆的半径
864+
float high; //圆的高
865+
float area; //圆的面积
866+
float circumference; //圆的周长
867+
float volume; //圆的体积
868+
float surface; //圆的表面积
869+
static const float PI = 3.14; //圆周率
870+
871+
public:
872+
circle(float r = 0, float h = 0); //构造函数
873+
~circle();
874+
875+
// 计算圆周长
876+
float calculateCircumference();
877+
878+
// 计算圆面积
879+
float calculateArea();
880+
881+
// 计算圆球表面积
882+
float calculateSphereArea();
883+
884+
// 计算圆柱体积
885+
float calculateCylinderVolume();
886+
887+
// 设置半径和高
888+
void setValues(float r, float h);
889+
};
890+
891+
circle::circle(float r, float h)
892+
{
893+
radius = r;
894+
high = h;
895+
}
896+
897+
circle::~circle()
898+
{
899+
}
900+
901+
float circle::calculateCircumference()
902+
{
903+
circumference = 2 * PI * radius;
904+
return circumference;
905+
}
906+
907+
float circle::calculateArea()
908+
{
909+
area = PI * radius * radius;
910+
return area;
911+
}
912+
913+
float circle::calculateSphereArea()
914+
{
915+
surface = 4 * PI * radius * radius;
916+
return surface;
917+
}
918+
919+
float circle::calculateCylinderVolume()
920+
{
921+
volume = PI * radius * radius * high;
922+
return volume;
923+
}
924+
925+
void circle::setValues(float r, float h)
926+
{
927+
radius = r;
928+
high = h;
929+
}
930+
931+
int main()
932+
{
933+
float radius, high;
934+
935+
// 输入半径和高
936+
std::cin >> radius >> high;
937+
938+
// 创建circle对象并设置值
939+
circle c(radius, high);
940+
941+
// 计算并输出结果,以空格分隔
942+
std::cout << c.calculateCircumference() << " "
943+
<< c.calculateArea() << " "
944+
<< c.calculateSphereArea() << " "
945+
<< c.calculateCylinderVolume() << std::endl;
946+
947+
return 0;
948+
}
949+
950+
```
856951

857952
编写C++程序完成以下功能: 1)定义一个 Point 类,其属性包括点的坐标,提供计算两点之间距离的方法;2)定义一个圆形类, a.其属性包括圆心和半径; b.创建两个圆形对象,提示用户输入圆心坐标和半径,判断两个圆是否相交,并输出结果。

0 commit comments

Comments
 (0)