万门cpp课程作业:实现复数类

在这里插入图片描述
自己开始写的代码,想用模板来实现,但是发现模板使用还是有些问题。

#include<iostream>
#include<ostream>
using namespace std;

template<class T>
class Complex{
	T real;
	T imag;
public:
	Complex(T r, T ima) :real(r), imag(ima) {}
	ostream& operator << (ostream &os, const Complex &comp) {
		os << " "<<comp.real << "+" << comp.imag << "i ";
		return os;
	}

	Complex<T> operator + (Complex a, Complex b) {
		T new_real = a.real + b.real;
		T new_imag = a.imag + b.imag;
		return Complex(new_real, new_imag);
	}
};

int main() {
	Complex<int> input1 = Complex<int>(1, 2);
	Complex<int> input2 = Complex<int>(1, 2);
	cout << input1 + input2 << endl;
	return 0;
}

报错:
error C2804: 二进制“operator <<”的参数太多

解答:

因为在一般情况下,运算的左元就是你定义的类中的对象
如果你的重载函数在类内部定义,他就会默认这点,因为加法就有两个运算数啊,所以你传递的参数应该只有一个
如果楼主愿意传递两个函数
可以把重载函数定义为友函数
就可以用在外面
就可以传递两个参数了

return *this和return this有什么区别?

正确答案为:return *this返回的是当前对象的克隆或者本身(若返回类型为A, 则是克隆, 若返回类型为A&, 则是本身 )。return this返回当前对象的地址(指向当前对象的指针)。
如果返回类型是A&, 那么return *this返回的是当前对象本身(也就是其引用), 而非副本。

参考博客:https://blog.csdn.net/stpeace/article/details/22220777

vs 快速注释

注释/取消注释
1)注释:组合键“Ctrl+K+C”;
2)取消注释:组合键“Ctrl+K+U

改进的代码:
解决部分报错问题,但是最后还是出现重定义的问题。运行不起来

#include<iostream>

using namespace std;

class Complex {
	double real;
	double imag;
public:
	Complex() :real(0), imag(0) {}
	Complex(double r, double i) :real(r), imag(i) {}
	Complex(double r) :real(r), imag(0) {}

	double real() { return real; }
	void real(double d) { real = d; }

	double imag()const { return imag; }
	void imag(double i) { imag = i; }

	Complex& operator+=(Complex z) { real += z.real, imag += z.imag; return *this; }
	Complex& operator-=(Complex z) { real -= z.real, imag -= z.imag; return *this; }
	friend Complex operator+(Complex a, Complex b);
	friend Complex operator-(Complex a, Complex b);
	//friend operator-(Complex a);
	friend ostream& operator << (ostream &os, const Complex &comp);
	friend Complex operator + (Complex a, Complex b);
};

Complex operator+(Complex a, Complex b) { return a += b; }
Complex operator-(Complex a, Complex b) { return a -= b; }
//Complex operator-(Complex a) { return { -a.real(), -a.imag() }; } // unary minus

bool operator==(Complex a, Complex b) {
	return a.real() == b.real() && a.imag() == b.imag();
}

bool operator!=(Complex a, Complex b) {
	return !(a == b);
}

//complex operator+(complex a, complex b) { return a += b; }
//operator-(complex a, complex b)
//{
//}
//complex operator-(complex a, complex b) { return a -= b; }
//complex operator-(complex a) { return { -a.real(),-a.imag() }; }
//
//
ostream& operator << (ostream &os, const Complex &comp) {
	os << " " << comp.real << "+" << comp.imag << "i ";
	return os;
}
//
//Complex operator + (Complex a, Complex b) {
//	double new_real = a.real + b.real;
//	double new_imag = a.imag + b.imag;
//	return Complex(new_real, new_imag);
//}

int main() {
	Complex a{ 1,2 };
	Complex b{ 3 };
	cout << a + b << endl;
	return 0;
}

完整可以运行的代码:

#define _CRT_SECURE_NO_WARNINGS 1

#include<iostream>
using namespace std;

class Complex
{
public:
	Complex(double real = 8.0, double image = 6.0)       //构造函数
		:_real(real)
		, _image(image)
	{
		cout << " call construct Complex(double real, double image)" << endl;
	}
	Complex(const Complex& d)          //拷贝函数
	{
		cout << "call construct Complex(const Complex& d)" << endl;
		_real = d._real;
		_image = d._image;
	}
	~Complex()       //析构函数
	{
		cout << "call ~Complex() " << endl;
		_real = 0.0;
		_image = 0.0;
	}
	Complex& operator=(const Complex& d)         //赋值运算符重载
	{
		cout << "testing = func" << endl;
		if (this != &d)
		{
			_real = d._real;
			_image = d._image;
		}
		return *this;
	}
	void Display()const                //打印复数
	{
		cout << "_real:" << _real<<" _image:" << _image << endl;
	}

	bool operator==(const Complex& d)             //==
	{
		cout << "testing == func" << endl;
		return this->_real == d._real
			&& this->_image == d._image;
	}
	bool operator!=(const Complex& d)             //!=
	{
		cout << "testing != func" << endl;
		return this->_real != d._real
			|| this->_image == d._image;
	}
	//复数只有当虚部为0时,即_image=0时,才可以比较大小,这时比较的是实部即_real的大小
	bool operator>(const Complex& d)          //>
	{
		if (this->_image != 0 || d._image != 0)
		{
			cout << "无法比较       ";
			return false;
		}

		else
		{
			return this->_real > d._real;
		}
	}
	bool operator<(const Complex& d)           //<
	{
		if (this->_image != 0 || d._image != 0)
		{
			cout << "无法比较      ";
			return false;
		}
		else
		{
			return this->_real < d._real;
		}
	}
	bool operator<=(const Complex& d)           //<=
	{
		if (this->_image != 0 || d._image != 0)
		{
			cout << "无法比较      ";
			return false;
		}
		else
		{
			return this->_real <= d._real;
		}
	}
	bool operator>=(const Complex& d)           //>=
	{
		if (this->_image != 0 || d._image != 0)
		{
			cout << "无法比较        ";
			return false;
		}
		else
		{
			return this->_real >= d._real;
		}
	}
	Complex operator+ (const Complex& d)           //+
	{
		cout << "test + func" << endl;
		Complex ret;
		ret._real = (this->_real + d._real);
		ret._image = (this->_image + d._image);
		return ret;
	}
	Complex& operator+=(const Complex& d)          //+=
	{
		cout << "+=" << endl;
		this->_real += d._real;
		this->_image += d._image;
		return *this;
	}
	Complex& operator++()            //前置++
	{
		cout << "前置++" << endl;
		this->_real += 1;
		return *this;
	}
	Complex operator++(int)         //后置++
	{
		cout << "后置++" << endl;
		Complex tmp(*this);
		this->_real += 1;
		return tmp;
	}


protected:
	double _real;
	double _image;
};

void Test()
{
	Complex d1;
	Complex d2(4.0, 6.6);

	d1.Display();
	d2.Display();
	//d1 = d2;
	//d1.Display();
	Complex d3;
	d3 = d1 + d2;
	if (d1==d2) cout <<"yes,they are equal"  << endl;
	//d1.operator==(d2)
	if (d1.operator!=(d2)) cout << "oh,they are not equal" << endl;
	d3.Display();
}

void Test1()
{
	Complex d1;
	Complex d2(4.0, 9.6);
	d1.Display();
	d2.Display();
	d1 += d2;
	d1.Display();
	d2.Display();
}

void Test2()
{
	Complex d1;
	Complex d2(0, 0);
	d2 = ++d1;
	d2.Display();
	d1.Display();
	Complex d3(1.0, 2.0);
	d3 = d1++;
	d3.Display();
	d1.Display();

}
void Test3()
{
	Complex d1(7, 0);
	Complex d2(6, 0);
	Complex d3;
	Complex d4(6, 0);
	cout << d1.operator>(d2) << endl;
	cout << d1.operator<(d2) << endl;
	cout << d3.operator<=(d4) << endl;
	cout << d2.operator>=(d4) << endl;
}

int main() {
	Test();
	//Test1();
	//Test2();
	//Test3();
	system("pause");
	return 0;
}

学习知识点

  1. 构造函数,包含默认构造函数+拷贝构造函数。
  2. 定义好了构造函数,马上定义析构函数。
  3. 重载运算符,习惯使用this指针表示当前的值,this->_real表示当前的实部值。
    return *this;返回当前值。Complex tmp(*this);创建拷贝当前值的对象。
发布了46 篇原创文章 · 获赞 0 · 访问量 838

猜你喜欢

转载自blog.csdn.net/github_38148039/article/details/103817228