溫瑞烘老師的教學歷程檔案(Teaching ePortfolio) - C++ - struct與class
 

資訊管理系
副教授
温瑞烘


歷程檔案 Portfolio


關於我 About Me

struct與class

struct Cuboid {                                                 class Cuboid {
                                                                                public:
  int length;                                                            int length;
  int width;                                                              int width;
  int height;                                                            int height;
};                                                                         
int area(Cuboid rt)  {                                         int area() {
  return 2*(rt.length*rt.width+                              return 2*(length*width+
   rt.width*rt.height+                                                width*leight+
   rt.height*rt.length);                                               height*length));
};                                                                           }

int volumn(Cuboid rt) {                                    int volumn() {
  return rt.length*rt.width*rt.height);                   return length*width*height;
}                                                                            }   
                                                                           };
main() {                                                               main() {
  Cuboid rt={10,8,6};                                             Cuboid rt={10,8,6};
  cout << "表面積=" << area(rt) << endl;            cout << "表面積=" << rt.area() << endl;
  cout << "體積=" << volumn(rt) << endl;           cout << "體積=" << rt.volumn() << endl;
}                                                                             }


訪前述的範例,寫兩個程式, 一個使用struct,一個使用class,宣告長方形的長與寬,
並有兩個函數,一為計算周長,一為計算面積,由鍵盤輸入長與寬

 


 

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


文章分類 Labels

 


最新文章 Top10

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