宣告class成員為private之二//檔案名稱:d:\C++11\C1104.cpp #include <iostream> using namespace std; #include <math.h> #define PI 3.141593f class Cylinder { //宣告圓柱體類別 float radius; //Cylinder的資料成員1 float height; //Cylinder的資料成員2 public: void setCylinder(float r, float h) { //設定Cylinder資料成員 radius = r; height = h; } float getRadius() { //取得radius資料函數 return radius; } float getHeight() { //取得height資料函數 return height; } float area() { //計算圓柱體表面積函數 return 2 * PI * radius * height; } float volumn() { //計算圓柱體體積函數 return PI * float(pow(radius, 2)) * height; } }; int main() { Cylinder cl; //建立Cylinder物件 cl.setCylinder(5.0, 10.0); //起始cl物件資料 cout << "圓柱體:\n"; cout << "半徑 = " << cl.getRadius() << endl; //輸出圓柱體半徑 cout << "高 = " << cl.getHeight() << endl; //輸出長方體的高 cout << "表面積 = " << cl.area() << "平方公分\n"; //輸出圓柱體表面積 cout << "體積 = " << cl.volumn() << "立方公分\n"; //輸出圓柱體體積 } |