复数类

记录复数类:

#include <iostream>
using namespace std;

template <typename T>
class CComplex
{
public:
	CComplex(T r=T(), T i=T()):mreal(r),mimage(i){}

private:
	T mreal;
	T mimage;
     //声明友元时,生成一对一的友元
	friend CComplex<T> operator+<T>(const CComplex<T> &lhs,
					  const CComplex<T> &rhs);
	friend ostream& operator<<<T>(ostream &out, 
					const CComplex<T> &src);

};


 
template<typename T>
ostream& operator<<(ostream &out, 
					const CComplex<T> &src)

{
	out<<src.mreal<<" "<<src.mimage;
	return out;
}
template<typename T>
 CComplex<T> operator+ (const CComplex<T> &lhs,
					  const CComplex<T> &rhs)
{
	return CComplex<T>(lhs.mreal+rhs.mreal,lhs.mimage+rhs.mimage);
}

int main()
{
	CComplex<int> comp1(10, 10);
	CComplex<int> comp2(20, 20);
	CComplex<int> comp3 = comp1+comp2;
	comp3 = comp1 + 10;
	comp3 = 10 + comp1;
	cout<<comp3<<endl;

	return 0;
}

main 函数中第四行和第五行是错误的,编译通不过,那是因为我们写的函数模板operator+只能接受两个实例化的对象,因为模板可以进行实参推演。而第四行和第五行的10,是推演不出来,所以会报错。

那怎样解决呢?可以提供一个operator+模板特例化int型的函数。

以下是完整代码:

加注释就是修改过的

 

template <typename T>
class CComplex
{
public:
	CComplex(T r=T(), T i=T()):mreal(r),mimage(i){}

private:
	T mreal;
	T mimage;
     //声明友元时,生成一对一的友元
	friend CComplex<T> operator+<T>(const CComplex<T> &lhs,
					  const CComplex<T> &rhs);
	friend ostream& operator<<<T>(ostream &out, 
					const CComplex<T> &src);
	friend CComplex<int>operator+ (const CComplex<int> &lhs, 
					  const CComplex<int> &rhs); //因为函数访问了类的私有成员,所以要声明为友元函数

};

template<typename T>
ostream& operator<<(ostream &out, const CComplex<T> &src)
{
	out<<src.mreal<<" "<<src.mimage;
	return out;
}
template<typename T>
CComplex<T> operator+ (const CComplex<T> &lhs,const CComplex<T> &rhs)					  
{
	return CComplex<T>(lhs.mreal+rhs.mreal,lhs.mimage+rhs.mimage);
}

CComplex<int> operator+ (const CComplex<int> &lhs,const CComplex<int> &rhs)//模板的特例化
{
	return CComplex<int>(lhs.mreal+rhs.mreal,lhs.mimage+rhs.mimage);
}
int main()
{
	CComplex<int> comp1(10, 10);
	CComplex<int> comp2(20, 20);
	CComplex<int> comp3 = comp1+comp2;
	comp3 = comp1 + 10;
	comp3 = 10 + comp1;
	cout<<comp3<<endl;

	return 0;
}

提供一个特例化就可以解决,main函数第四行和第五行就会调用特例化的那个operator+函数,会隐式构造一个实部是10的对象,然后相加。最后结果comp3的结果就是实部为20,虚部为10.



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java中的复数类可以用来进行复数运算和处理。以下是Java中复数类的总结: 1. Java自带的复数类:Java中自带了Complex类,可以通过导入java.util.Complex包来使用。 2. 复数类的定义:复数类通常由实部和虚部组成,可以定义一个复数类来表示这两个部分。例如: ```java public class Complex { private double real; private double imag; // 构造方法 public Complex(double real, double imag) { this.real = real; this.imag = imag; } // getter和setter方法 public double getReal() { return real; } public void setReal(double real) { this.real = real; } public double getImag() { return imag; } public void setImag(double imag) { this.imag = imag; } } ``` 3. 复数类的运算:复数类可以进行加、减、乘、除等运算,例如: ```java public Complex add(Complex other) { double real = this.real + other.real; double imag = this.imag + other.imag; return new Complex(real, imag); } public Complex subtract(Complex other) { double real = this.real - other.real; double imag = this.imag - other.imag; return new Complex(real, imag); } public Complex multiply(Complex other) { double real = this.real * other.real - this.imag * other.imag; double imag = this.real * other.imag + this.imag * other.real; return new Complex(real, imag); } public Complex divide(Complex other) { double real = (this.real * other.real + this.imag * other.imag) / (Math.pow(other.real, 2) + Math.pow(other.imag, 2)); double imag = (this.imag * other.real - this.real * other.imag) / (Math.pow(other.real, 2) + Math.pow(other.imag, 2)); return new Complex(real, imag); } ``` 4. 复数类的常用方法:复数类还可以实现一些常用方法,例如获取模长和相角,例如: ```java public double getModulus() { return Math.sqrt(Math.pow(this.real, 2) + Math.pow(this.imag, 2)); } public double getArgument() { return Math.atan2(this.imag, this.real); } ``` 5. 复数类的使用:定义好复数类后,就可以通过实例化对象来进行复数运算,例如: ```java Complex c1 = new Complex(2, 3); Complex c2 = new Complex(4, -2); Complex sum = c1.add(c2); Complex difference = c1.subtract(c2); Complex product = c1.multiply(c2); Complex quotient = c1.divide(c2); double modulus = c1.getModulus(); double argument = c1.getArgument(); ``` 以上就是Java复数类的总结。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值