复数类 重载后缀“--”运算符

描述

创建一个复数类Complex,并重载后缀“--”运算符,将其重载为友元函数,实现复数的实部减1和虚部减1。

Complex类如下,请补充重载后缀“--”运算符,并用main函数测试

class Complex

{

protected:

 double r;//实部

 double i;//虚部

public:

 Complex(double x = 0.0, double y = 0.0)

{

         r = x;

         i = y;

 }

 void show()

{

      cout<< "(" << r << "," << i <<")";

}

};

int main()

{

   Complex c1(10.0, 20.0), c2;

   c2 = c1--;

   c1.show();

   cout<< endl;

   c2.show();

   cout<< endl;

   return 0;

}

输入

输出

见样例

输入样例 1 

输出样例 1

(9,19)
(10,20)
#include <iostream> 
using namespace std;
class Complex
{
	protected:
		double r;//实部
 		double i;//虚部
	public:
 		Complex(double x = 0.0, double y = 0.0)
		{
         r = x;
         i = y;
 		}
 		friend Complex operator--(Complex& a,int)
 		{
 			Complex t;
 			t=a;
 			a.r=a.r-1;
 			a.i=a.i-1;
 			return t;
		 }
 		void show()
		{
      		cout<< "(" << r << "," << i <<")";
		}
};
int main()
{
   Complex c1(10.0, 20.0), c2;

   c2 = c1--;//先使用c1,再使c1-1; 

   c1.show();

   cout<< endl;

   c2.show();

   cout<< endl;

   return 0;
}
发布了110 篇原创文章 · 获赞 4 · 访问量 5186

猜你喜欢

转载自blog.csdn.net/weixin_43673589/article/details/104453495