鱼C论坛

 找回密码
 立即注册
查看: 1414|回复: 1

小女子请教大家一题,先谢过大家!

[复制链接]
发表于 2015-3-24 13:31:32 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
按要求编写程序:
定义一个矩形类Rectangle,要求如下:
(1)包含四个double型私有数据成员x,y,width和height分别存放矩形的横坐标,纵坐标,宽和高。
(2)定义构造函数,对四个数据成员进行初始化,该函数的所有参数的默认值均为0.
(3)用成员函数重载前置“++”运算符,实现矩形对象长和宽的加1运算。
(4)用友元函数重载“==”运算符,实现对两个矩形对象面积是否相等的判断。
(5)定义getArea()函数,返回矩形对象的面积。
(6)定义showInfo()函数,显示矩形对象的坐标、宽和高。
(7)在主函数中对矩形类的各个功能进行测试。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2015-3-24 16:08:14 | 显示全部楼层
  1. #include <IOSTREAM>
  2. #include <STDIO.H>

  3. class Rectangle
  4. {
  5.         int x;
  6.         int y;
  7.         int width;
  8.         int height;
  9. public:
  10.         Rectangle()
  11.         {
  12.                 x = 0;
  13.                 y = 0;
  14.                 width = 0;
  15.                 height = 0;
  16.         }
  17.        
  18.         Rectangle operator=(const Rectangle& rect)
  19.         {
  20.                 Rectangle r;
  21.                 r.x = rect.x;
  22.                 r.y = rect.y;
  23.                 r.width = rect.width;
  24.                 r.height = rect.height;
  25.                 return r;
  26.         }

  27.         const Rectangle operator++()  
  28.         {  
  29.                 ++width;
  30.                 ++height;
  31.                 return *this;
  32.         }

  33.         friend inline bool operator==(const Rectangle &rect, const Rectangle &rect1)
  34.         {
  35.                 return ((rect1.x==rect.x) && (rect1.y==rect.y) && (rect1.width==rect.width) && (rect.height==rect.height));
  36.         }

  37.         int getArea()
  38.         {
  39.                 return (width * height);
  40.         }

  41.         void showInfo()
  42.         {
  43.                 printf("x=%d\ty=%d\twidth=%d    height=%d\r\n", x, y, width, height);
  44.         }
  45. };

  46. int main()
  47. {
  48.         Rectangle rect;
  49.         Rectangle rect1;

  50.         rect1 = ++rect;
  51.         rect.showInfo();

  52.         bool b = (rect==rect1);
  53.         int area = rect.getArea();
  54.         printf("b=%s\tarea=%d\r\n", b?"TRUE":"FALSE", area);

  55.         getchar();

  56.         return 0;
  57. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-19 11:00

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表