c语言自行设计矩形类构造函数

首先,感谢你的阅读,本人观点:学习编程是个漫长的过程并不是一时就会很厉害的,关键是坚持,快乐地学习,在这里分享我的一些笔记给你;
  请自行设计一个矩形类,可以计算矩形的面积、周长、对象线,判断是否是正方形。请用上类似的构造函数,自己设计main()函数,对设计的类进行测试。

  [cpp]

  #include <iostream>

  #include <cmath>

  using namespace std;

  class Rectangle

  {

  private:

  double length;

  double width;

  public:

  Rectangle():length(1),width(1){}

  Rectangle(double len,double wid):length(len),width(wid){}

  // Rectangle(double len=1,double wid =1):length(len),width(wid){}

  double area(void);

  double perimeter(void){ return 2*(length+width); }

  double diagonal(void) { return sqrt(length*length+width*width); }

  bool square_or_not(void) { return length==width?true:false; }

  void show_message(void);

  };

  //Rectangle::Rectangle(double len,double wid){length = len;width = wid;}

  double Rectangle::area(void)

  {

  return length*width;

  }

  void Rectangle::show_message(void)

  {

  cout << "矩形的长宽分别为: " << length << '\t' << width <<endl;

  cout << "周长: " << perimeter() << "面积: " << area() << "对角线长度: "<< diagonal() << endl;

  cout << "是否为正方形? " << square_or_not() << endl;

  }

  int main()

  {

  Rectangle rect1;

  rect1.show_message();

  Rectangle rect2(3,4);

  rect2.show_message();

  return 0;

  }

  #include <iostream>

  #include <cmath>

  using namespace std;

  class Rectangle

  {

  private:

  double length;

  double width;

  public:

  Rectangle():length(1),width(1){}

  Rectangle(double len,double wid):length(len),width(wid){}

  // Rectangle(double len=1,double wid =1):length(len),width(wid){}

  double area(void);

  double perimeter(void){ return 2*(length+width); }

  double diagonal(void) { return sqrt(length*length+width*width); }

  bool square_or_not(void) { return length==width?true:false; }

  void show_message(void);

  };

  //Rectangle::Rectangle(double len,double wid){length = len;width = wid;}

  double Rectangle::area(void)

  {

  return length*width;

  }

  void Rectangle::show_message(void)

  {

  cout << "矩形的长宽分别为: " << length << '\t' << width <<endl;

  cout << "周长: " << perimeter() << "面积: " << area() << "对角线长度: "<< diagonal() << endl;

  cout << "是否为正方形? " << square_or_not() << endl;

  }

  int main()

  {

  Rectangle rect1;

  rect1.show_message();

  Rectangle rect2(3,4);

  rect2.show_message();

  return 0;

  }
从基础的学习到后面应用提高都非常的系统,无论你是本专业的还是跨专业的,都能有所收获,参加4个月的达内时光转眼已逝,伴随着喜悦,达内给你一个新的平台,一个崭新的开始。C++培训
(本文由广州达内为你分享;)

转载于:https://www.cnblogs.com/javaitpx/archive/2013/04/08/3007353.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值