C++一天一个程序(五)
(1)確定所求長方形的長和寬。
(2)確定計算長方形的周長和面積的公式并計算。
(3)輸出計算結(jié)果。
(1)以面向過程程序設(shè)計思想編碼。
#include
using namespace std;
void main(){
int perimeter,area;
int length=20,width=10;
perimeter=2*(length+width);
area=length* width;
cout<<“perimeter=”<<perimeter<<endl;
cout<<“area=”<<area<<endl;
}
2.以面向?qū)ο蟮某绦蛟O(shè)計方式思考
#include
using namespace std;
class Rectangle
{
public:
Rectangle(float w=0,float 1=0)
{ width=w;length=l; }
void GetArea()
{cout<<’ area="<<widthlength <<endl;}
void GetPerim()
{cout<<“perimeter=”< <2(width+length)<<endl;} private:float width,length;
}
void main()
{
Rectangle a( 10,20);/定義長方形類的個變量 a, 即實例化-個特殊的長方形對象a,它的長是20,寬是
10/
a.GetPerim();//調(diào)用a對象的兩個方法
a.GetArea();
}
面向過程注重算法,面向?qū)ο笞⒅毓餐瑢傩院托袨椤?/p>
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅持創(chuàng)作打卡瓜分現(xiàn)金大獎
總結(jié)
以上是生活随笔為你收集整理的C++一天一个程序(五)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C++一天一个程序(四)
- 下一篇: 一天一个C++程序(六)