溫瑞烘老師的教學歷程檔案(Teaching ePortfolio) - C++ - 宣告class成員為private之一
 

資訊管理系
副教授
温瑞烘


歷程檔案 Portfolio


關於我 About Me

宣告class成員為private之一

//檔案名稱:d:\C++11\C1104.cpp
#include <iostream>
  using namespace std;
#include <math.h>
class Cuboid {  //宣告長方體類別
  private:      // 資料成員是私有的, 外界無法存取, 須透過成員函數
 int length; //Cuboid的資料成員1
 int width; //Cuboid的資料成員2
 int height; //Cuboid的資料成員3
  public:       // 宣告成員函數為公用, 外解可以存取
 void setCuboid(int l, int w, int h) { //設定Cuboid資料成員
     length = l;
  width = w;
  height = h;
 }
 int getLength() { //取得length資料函數
  return length;
 }
 int getWidth() { //取得width資料函數
  return width;
 }
 int getHeight() { //取得height資料函數
  return height;
 }
 int area() { // 計算長方體表面積函數
     return 2 * (length * width
   + width * height
   + height * length);
 }
 int volumn() { // 計算長方體體積函數
  return length * width * height;
 }
};


int main() {
 Cuboid rt; //建立Cuboid物件
 rt.setCuboid(6, 8, 10); //起始rt物件資料
 cout << "長方體:\n";
 cout << "長 = " << rt.getLength() << endl; //輸出長方體的長
 cout << "寬 = " << rt.getWidth() << endl; //輸出長方體的寬
 cout << "高 = " << rt.getHeight() << endl; //輸出長方體的高
 cout << "表面積 = " << rt.area() << "平方公分\n"; //輸出長方體表面積
 cout << "體積 = " << rt.volumn() << "立方公分\n\n"; //輸出長方體體積
}
 

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


文章分類 Labels

 


最新文章 Top10

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