溫瑞烘老師的教學歷程檔案(Teaching ePortfolio) - C++ - 類別與成員函數
 

資訊管理系
副教授
温瑞烘


歷程檔案 Portfolio


關於我 About Me

類別與成員函數

//檔案名稱:d:\C++11\C1103.cpp p. 11-7
#include <iostream>
using namespace std;
#include <math.h>
#define PI 3.141593f

class Cuboid //宣告長方體類別
{
public:
 int length; //Cuboid的資料成員1
 int width; //Cuboid的資料成員2
 int height; //Cuboid的資料成員3
 int area() //計算長方體表面積函數
 {
  return 2 * (length * width
   + width * height
   + height * length);
 }
 int volumn() //計算長方體體積函數
 {
  return length * width * height;
 }
};

class Cylinder //宣告圓柱體類別
{
public:
 float radius; //Cylinder的資料成員1
 float height; //Cylinder的資料成員2
 float area() //計算圓柱體表面積函數
 {
  return 2 * PI * radius * height;
 }
 float volumn() //計算圓柱體體積函數
 {
  return PI * float(pow(radius, 2)) * height;
 }
};

int main()
{
 Cuboid rt = {6, 8, 10}; //建立Cuboid結構資料
 cout << "長方體:\n";
 cout << "長 = " << rt.length << endl; //輸出長方體的長
 cout << "寬 = " << rt.width << endl; //輸出長方體的寬
 cout << "高 = " << rt.height << endl; //輸出長方體的高
 cout << "表面積 = " << rt.area() << "平方公分\n"; //輸出長方體表面積
 cout << "體積 = " << rt.volumn() << "立方公分\n\n"; //輸出長方體體積

 Cylinder cl = {5.0, 10.0}; //建立Cylinder結構資料
 cout << "圓柱體:\n";
 cout << "半徑 = " << cl.radius << endl; //輸出圓柱體半徑
 cout << "高 = " << cl.height << endl; //輸出長方體的高
 cout << "表面積 = " << cl.area() << "平方公分\n"; //輸出圓柱體表面積
 cout << "體積 = " << cl.volumn() << "立方公分\n"; //輸出圓柱體體積
 return 0; //成功結束程式
}
 

全部共 0則留言
登入帳號密碼代表遵守學術網路規範
 


文章分類 Labels

 


最新文章 Top10

中華科技大學數位化學習歷程 - 意見反應