?? 例9.2.txt
字號:
例9.2 有兩個長方柱,其長、寬、高分別為: (1)12,20,25;(2)10,14,20。求它們的體積。編一個基于對象的程序,在類中用帶參數的構造函數。
#include <iostream>
using namespace std;
class Box
{public:
Box(int,int,int); //聲明帶參數的構造函數
int volume( ); //聲明計算體積的函數
private:
int height;
int width;
int length;
};
Box∷Box(int h,int w,int len) //在類外定義帶參數的構造函數
{height=h;
width=w;
length=len;
}
int Box∷volume( ) //定義計算體積的函數
{return(height*width*length);
}
int main( )
{Box box1(12,25,30); //建立對象box1,并指定box1長、寬、高的值
cout<<″The volume of box1 is ″<<box1.volume( )<<endl;
Box box2(15,30,21); //建立對象box2,并指定box2長、寬、高的值
cout<<″The volume of box2 is ″<<box2.volume( )<<endl;
return 0;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -