【C++面向对象程序设计】实验四 运算符重载

目录

一、实验目的

1.进一步了解运算符重载的概念和使用方法

2.掌握几种常用的运算符重载的方法

二、实验要求

1.定义一个复数类Complex,重载运算法“+”,使之能用于复数的加法运算。将运算符重载为普通函数(非成员、非友元)、成员函数、友元函数。根据要求修改通过函数来实现复数相加的示例,分别编写程序,求两个复数之和

2.定义一个复数类Complex,重载运算法“/”,使之能用于复数的除法运算。将运算符重载为普通函数(非成员、非友元)、成员函数、友元函数。根据要求修改通过函数来实现复数相除的示例,分别编写程序,求两个复数相除。


一、实验目的

1.进一步了解运算符重载的概念和使用方法

2.掌握几种常用的运算符重载的方法

二、实验要求

1.定义一个复数类Complex,重载运算法“+”,使之能用于复数的加法运算。将运算符重载为普通函数(非成员、非友元)、成员函数、友元函数。根据要求修改通过函数来实现复数相加的示例,分别编写程序,求两个复数之和

【普通函数】

#include 
using namespace std;

class Complex 
{
	public:
		Complex(float x = 0, float y = 0) 
		{
			real = x;
			imag = y;
		}
		Complex complex_add(Complex &c2);
		void display() 
		{
			cout << real << '+' << imag << 'i' << endl;
		}
	private:
		float real;
		float imag;
};

Complex Complex::complex_add(Complex &c2) 
{
	Complex c;
	c.real = real + c2.real;
	c.imag = imag + c2.imag;
	return c;
}

int main() 
{
	Complex complex1(3.34f, 4.8f), complex2(12.8f, 5.2f);
	Complex complex;
	complex = complex1.complex_add(complex2);
	complex.display();
	return 0;
}

【成员函数】

#include 
using namespace std;

class Complex 
{
	public:
		Complex(float x = 0, float y = 0) 
		{
			real = x;
			imag = y;
		}
		Complex operator + (Complex &c2);
		void display() 
		{
			cout << real << '+' << imag << 'i' << endl;
		}
	private:
		float real;
		float imag;
};

Complex Complex::operator +(Complex &c2) 
{
	Complex c;
	c.real = real + c2.real;
	c.imag = imag + c2.imag;
	return c;
}

int main() 
{
	Complex c1(3.34f, 4.8f), c2(12.8f, 5.2f);
	Complex c3 = c1 + c2;
	c3.display();
	return 0;
}

【友元函数】 

#include 
using namespace std;

class Complex 
{
	public:
		Complex(float x = 0, float y = 0) 
		{
			real = x;
			imag = y;
		}
		friend Complex operator + (Complex &c1, Complex &c2);
		void display() 
		{
			cout << real << '+' << imag << 'i' << endl;
		}
	private:
		float real;
		float imag;
};

Complex operator +(Complex &c1, Complex &c2) 
{
	return Complex(c1.real + c2.real, c1.imag + c2.imag);
}

int main() 
{
	Complex c1(3.34f, 4.8f), c2(12.8f, 5.2f);
	Complex c3 = c1 + c2;
	c3.display();
	return 0;
}

2.定义一个复数类Complex,重载运算法“/”,使之能用于复数的除法运算。将运算符重载为普通函数(非成员、非友元)、成员函数、友元函数。根据要求修改通过函数来实现复数相除的示例,分别编写程序,求两个复数相除。

【普通函数】

#include 
using namespace std;

class Complex 
{
	public:
		Complex(float x = 0, float y = 0) 
		{
			real = x;
			imag = y;
		}
		Complex complex_division(Complex &c2);
		void display() 
		{
			cout << real << '+' << imag << 'i' << endl;
		}
	private:
		float real;
		float imag;
};

Complex Complex::complex_division(Complex &c2) 
{
	Complex c;
	c.real = (real * c2.real + imag * c2.imag) / ((c2.real) * (c2.real) + (c2.imag) * (c2.imag));
	c.imag = (c2.real * imag - real * c2.imag) / ((c2.real) * (c2.real) + (c2.imag) * (c2.imag));
	return c;
}

int main() 
{
	Complex c1(3.34f, 4.8f), c2(12.8f, 5.2f);
	Complex c3 = c1.complex_division(c2);
	c3.display();
	return 0;
}

【成员函数】

#include 
using namespace std;

class Complex 
{
	public:
		Complex(float x = 0, float y = 0) 
		{
			real = x;
			imag = y;
		}
		Complex operator / (Complex &c2);
		void display() 
		{
			cout << real << '+' << imag << 'i' << endl;
		}
	private:
		float real;
		float imag;
};

Complex Complex::operator / (Complex &c2) 
{
	Complex c;
	c.real = (real * c2.real + imag * c2.imag) / ((c2.real) * (c2.real) + (c2.imag) * (c2.imag));
	c.imag = (c2.real * imag - real * c2.imag) / ((c2.real) * (c2.real) + (c2.imag) * (c2.imag));
	return c;
}

int main() 
{
	Complex c1(3.34f, 4.8f), c2(12.8f, 5.2f);
	Complex c3 = c1 / c2;
	c3.display();
	return 0;
}

【友元函数】

#include 
using namespace std;

class Complex 
{
	public:
		Complex(float x = 0, float y = 0) 
		{
			real = x;
			imag = y;
		}
		friend Complex operator / (Complex &c1, Complex &c2);
		void display() 
		{
			cout << real << '+' << imag << 'i' << endl;
		}
	private:
		float real;
		float imag;
};

Complex operator / (Complex &c1, Complex &c2) 
{
	Complex c;
	c.real = (c1.real * c2.real + c1.imag * c2.imag) / ((c2.real) * (c2.real) + (c2.imag) * (c2.imag));
	c.imag = (c2.real * c1.imag - c1.real * c2.imag) / ((c2.real) * (c2.real) + (c2.imag) * (c2.imag));
	return c;
}

int main() 
{
	Complex c1(3.34f, 4.8f), c2(12.8f, 5.2f);
	Complex c3 = c1 / c2;
	c3.display();
	return 0;
}


你可能感兴趣的:(C++面向对象程序设计,c++,开发语言,算法)