C++一天一个程序(五)

(1)确定所求长方形的长和宽。
(2)确定计算长方形的周长和面积的公式并计算。
(3)输出计算结果。
(1)以面向过程程序设计思想编码。
#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.以面向对象的程序设计方式思考
#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();//调用a对象的两个方法
a.GetArea();
}
面向过程注重算法,面向对象注重共同属性和行为。

发布了14 篇原创文章 · 获赞 0 · 访问量 217

猜你喜欢

转载自blog.csdn.net/weixin_41210618/article/details/104741147
今日推荐