鱼C论坛

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

[已解决]求解一道重载运算符的题目

[复制链接]
发表于 2020-4-3 19:07:26 | 显示全部楼层 |阅读模式

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

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

x
建立点类Point,包含两个成员变量x,y,分别表示横坐标和纵坐标。要求该类可以显示点的坐标值,重载前置运算符++、--;重载后置运算符++、--。
主函数需包含的内容:创建点对象,进行前置和后置运算并显示点值。
最佳答案
2020-4-3 19:22:29
  1. #include<iostream>
  2. using namespace std;
  3. class AB
  4. {
  5. public:
  6.     AB(int xx, int yy);
  7.     void ShowAB();
  8.     AB& operator ++();
  9.     AB operator ++(int);
  10.     AB& operator --();
  11.     AB operator --(int);
  12. private:
  13.     int x1,x2;
  14. };
  15. AB::AB(int xx, int yy)
  16. {
  17.     x1=xx;
  18.     x2=yy;
  19. }
  20. void AB::ShowAB()
  21. {
  22.     cout<<x1<<" , "<<x2<<endl;
  23. }
  24. AB& AB::operator ++()
  25. {
  26.     x1++;
  27.     x2++;
  28.     return *this;
  29. }
  30. AB AB::operator ++(int)
  31. {
  32.     AB old=*this;
  33.     ++(*this);
  34.     return old;
  35. }
  36. AB& AB::operator --()
  37. {
  38.     x1--;
  39.     x2--;
  40.     return *this;
  41. }
  42. AB AB::operator --(int)
  43. {
  44.     AB old=*this;
  45.     --(*this);
  46.     return old;
  47. }

  48. int main(void)
  49. {
  50.     AB AA(0,0);
  51.     AB BB(0,0);
  52.     cout<<"A的值为:";
  53.     AA.ShowAB();
  54.     cout<<"B的值为:";
  55.     BB.ShowAB();
  56.     cout<<"B=A++运算后,A的值为:";
  57.     (++AA).ShowAB();
  58.     cout<<"             B的值为:";
  59.     (BB++).ShowAB();
  60.     cout<<"B=++A运算后,A的值为:";
  61.     (++AA).ShowAB();
  62.     cout<<"             B的值为:";
  63.     (++BB).ShowAB();

  64.     cout<<"B=A--运算后,A的值为:";
  65.     (--AA).ShowAB();
  66.     cout<<"             B的值为:";
  67.     (BB--).ShowAB();
  68.     cout<<"B=--A运算后,A的值为:";
  69.     (--AA).ShowAB();
  70.     cout<<"             B的值为:";
  71.     (--BB).ShowAB();

  72.     return 0;
  73. }
复制代码


你把类名改一下就符合了~
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-4-3 19:22:29 | 显示全部楼层    本楼为最佳答案   

回帖奖励 +1 鱼币

  1. #include<iostream>
  2. using namespace std;
  3. class AB
  4. {
  5. public:
  6.     AB(int xx, int yy);
  7.     void ShowAB();
  8.     AB& operator ++();
  9.     AB operator ++(int);
  10.     AB& operator --();
  11.     AB operator --(int);
  12. private:
  13.     int x1,x2;
  14. };
  15. AB::AB(int xx, int yy)
  16. {
  17.     x1=xx;
  18.     x2=yy;
  19. }
  20. void AB::ShowAB()
  21. {
  22.     cout<<x1<<" , "<<x2<<endl;
  23. }
  24. AB& AB::operator ++()
  25. {
  26.     x1++;
  27.     x2++;
  28.     return *this;
  29. }
  30. AB AB::operator ++(int)
  31. {
  32.     AB old=*this;
  33.     ++(*this);
  34.     return old;
  35. }
  36. AB& AB::operator --()
  37. {
  38.     x1--;
  39.     x2--;
  40.     return *this;
  41. }
  42. AB AB::operator --(int)
  43. {
  44.     AB old=*this;
  45.     --(*this);
  46.     return old;
  47. }

  48. int main(void)
  49. {
  50.     AB AA(0,0);
  51.     AB BB(0,0);
  52.     cout<<"A的值为:";
  53.     AA.ShowAB();
  54.     cout<<"B的值为:";
  55.     BB.ShowAB();
  56.     cout<<"B=A++运算后,A的值为:";
  57.     (++AA).ShowAB();
  58.     cout<<"             B的值为:";
  59.     (BB++).ShowAB();
  60.     cout<<"B=++A运算后,A的值为:";
  61.     (++AA).ShowAB();
  62.     cout<<"             B的值为:";
  63.     (++BB).ShowAB();

  64.     cout<<"B=A--运算后,A的值为:";
  65.     (--AA).ShowAB();
  66.     cout<<"             B的值为:";
  67.     (BB--).ShowAB();
  68.     cout<<"B=--A运算后,A的值为:";
  69.     (--AA).ShowAB();
  70.     cout<<"             B的值为:";
  71.     (--BB).ShowAB();

  72.     return 0;
  73. }
复制代码


你把类名改一下就符合了~
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-7-4 14:53

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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