当前位置:编程学习 > C/C++ >>

高分求助 C++编程题 谢谢

设计类并在main()中进行测试

设计一个圆柱类Cylinder。数据成员包括radius(底面半径)、height(圆柱体的高)。成员函数包括:

(1)构造函数(要求对构造函数进行重载);

(2)求圆柱体的表面积(表面积:surface area);

(3)求圆柱体的体积(体积Volume);

  (4)输出圆柱体信息(包括圆柱体的底面半径、高、表面积、体积)。

谢谢

追问:你能把我整理一下吗? 按题目的4个小题分好 等我给你追加分 谢谢了
答案://====================================================
//名称:C++文件示例
//作者:Tao
//时间:2010-11-02
//简介:
//设计类并在main()中进行测试
//设计一个圆柱类Cylinder。数据成员包括radius(底面半径)、height(圆柱体的高)。成员函数包括:
//
//(1)构造函数(要求对构造函数进行重载);
//
//(2)求圆柱体的表面积(表面积:surface area);
//
//(3)求圆柱体的体积(体积Volume);
//
//(4)输出圆柱体信息(包括圆柱体的底面半径、高、表面积、体积)。
//====================================================

#include <iostream>
#include <conio.h>

#define PI 3.1415926

using namespace std;

//圆柱体类
class Cylinder
{
public:
 float radius;
 float height;
 
 Cylinder()
 {
  //构造函数:使用默认值
  radius = 5;
  height = 10;
 }

 Cylinder(float rad, float hei)
 {
  //构造函数重载:使用参数初始化
  radius = rad;
  height = hei;

 }

 float GetSurfaceArea()
 {
  //计算圆柱体的表面积
  //圆柱体的表面积=2个底面积+1个侧面积

  float baseArea = PI * radius * radius; //圆的面积 = π × r × r
  float sideArea = 2 * PI  * height;  //圆柱体侧面积 = 底面周长 × 高

  float surfaceArea = 2 * baseArea + sideArea;

  return surfaceArea;

 }

 float GetVolume()
 {
  //计算圆柱体的体积
  //圆柱体的体积 = 底面积×高
  float baseArea = PI * radius * radius; //圆的面积 = π × r × r
  float vol = baseArea * height;

  return vol;
 }

 void ShowCylinderInfo()
 {
  //输出圆柱体信息(包括圆柱体的底面半径、高、表面积、体积)。
  cout << "圆柱体的底面半径:"<< radius << endl;
  cout << "圆柱体的高:"<< height << endl;

  cout << "圆柱体的表面积:"<< GetSurfaceArea() << endl;
  cout << "圆柱体的体积:"<< GetVolume() << endl;
 }

};


int main(int argc, char *argv[])
{
 //在main中测试Cylinder类

 Cylinder cylinder;

 cylinder.ShowCylinderInfo();

 getch();
 return 0;
}

我是初学者,看不是很懂,好像很乱。
写代码啊??

上一个:Visual C++ 6.0是什么啊
下一个:%5.1f 在C++里面是什么意思

CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,