(程序头部注释开始)
* 程序的版权和版本声明部分
* Copyright (c) 2011, 烟台大学计算机学院学生
* All rights reserved.
* 文件名称: 实现复数类中的运算符重载
* 作 者: 冯珍珍
* 完成日期: 2012 年 4 月 7 日
* 版 本 号:
* 对任务及求解方法的描述部分
* 输入描述:
* 问题描述:定义一个复数类重载运算符+、-、*、/,使之能用于复数的加减乘除。
* 程序输出:
* 程序头部的注释结束
/*【任务1】实现复数类中的运算符重载
定义一个复数类重载运算符+、-、*、/,使之能用于复数的加减乘除。
(1)方案一:请用类的成员函数完成运算符的重载;*/
#include <iostream>
using namespace std;
class Complex
{
public:
Complex(){real=0;imag=0;}
Complex(double r,double i){real=r;imag=i;}
Complex operator+(Complex &c2);
Complex operator-(Complex &c2);
Complex operator*(Complex &c2);
Complex operator/(Complex &c2);
void display();
private:
double real;
double imag;
};
//下面定义成员函数
Complex Complex::operator+(Complex &c2)//定义重载运算符”+“的函数
{
Complex c;
c.real=real+c2.real; //real+c2.real 此处意思为”this->real+c2.real,this->real就是c1.real
c.imag=imag+c2.imag;
return c;
}
Complex Complex::operator-(Complex &c2)//定义重载运算符”-“的函数
{
Complex c;
c.real=real-c2.real;
c.imag=imag-c2.imag;
return c;
}
Complex Complex::operator*(Complex &c2)
{
Complex c;
c.real = real*c2.real - imag*c2.imag;
c.imag = real*c2.imag + imag*c2.real;
return c;
}
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 = (-real*c2.imag + imag*c2.real)/(c2.real*c2.real+c2.imag*c2.imag);
return c;
}
void Complex::display()
{
cout<<"("<<real<<","<<imag<<"i)"<<endl;
}
int main()
{
Complex c1(3,4),c2(5,-10),c3;
cout<<"c1=";
c1.display();
cout<<"c2=";
c2.display();
c3=c1+c2; // 此处相当于非重载运算符函数中的“c3=c1.Complex_add(c2)
//将运算符+重载为类的成员函数后,编译系统会将程序中的表达式c1+c2解释为:c1.operator+(c2)即以c2为实参调用c1的运算符重载函数operator+(Complex&c2),operator+是类的一个函数名
cout<<"c1+c2=";
c3.display();
c3=c1-c2;
cout<<"c1-c2=";
c3.display();
c3=c1*c2;
cout<<"c1*c2=";
c3.display();
c3=c1/c2;
cout<<"c1/c2=";
c3.display();
system("pause");
return 0;
}
//(2)方案二:请用类的友元函数,而不是成员函数,完成上面提及的运算符的重载;
#include <iostream>
using namespace std;
class Complex
{
public:
Complex(){real=0;imag=0;}
Complex(double r,double i){real=r;imag=i;}
friend Complex operator+(Complex &c1,Complex &c2);
friend Complex operator-(Complex &c1,Complex &c2);
friend Complex operator*(Complex &c1,Complex &c2);
friend Complex operator/(Complex &c1,Complex &c2);
void display();
private:
double real;
double imag;
};
//下面定义友员函数
Complex operator+(Complex &c1,Complex &c2)//定义重载运算符”+“的函数
{
return Complex (c1.real+c2.real,c1.imag+c2.imag);
}
Complex operator-(Complex &c1,Complex &c2)//定义重载运算符”-“的函数
{
return Complex(c1.real-c2.real,c1.imag-c2.imag);
}
Complex operator*(Complex &c1,Complex &c2)//定义重载运算符”*“的函数
{
return Complex (c1.real*c2.real,c1.imag*c2.imag);
}
Complex operator/(Complex &c1,Complex &c2)//定义重载运算符”/“的函数
{
return Complex(c1.real/c2.real,c1.imag/c2.imag);
}
void Complex::display()
{
cout<<"("<<real<<","<<imag<<"i)"<<endl;
}
int main()
{
cout<<"用友员函数输出的结果是"<<endl;
Complex c1(3,4),c2(5,-10),c3;
cout<<"c1=";
c1.display();
cout<<"c2=";
c2.display();
c3=c1+c2;
cout<<"c1+c2=";
c3.display();
c3=c1-c2;
cout<<"c1-c2=";
c3.display();
c3=c1*c2;
cout<<"c1*c2=";
c3.display();
c3=c1/c2;
cout<<"c1/c2=";
c3.display();
system("pause");
return 0;
}
运算符函数作为有员函数,将参数改为两个,c3=c1-c2相当于调用函数Complex operator-(Complex &c1,Complex &c2)//定义重载运算符”-“的函数{return Complex(c1.real-c2.real,c1.imag-c2.imag)}
方案三:在方案二的基础上,扩展+、-、*、/运算符的功能,使之能与double型数据进行运算。设Complex c; double d; c?d和d?c的结果为将d视为实部为d的复数同c运算的结果(其中?为+、-、*、/之一)。另外,定义一目运算符-,-c相当于0-c
#include<iostream>
using namespace std;
class Complex
{
public:
Complex(){real=0;imag=0;}
Complex(double r,double i){real=r;imag=i;}
friend Complex operator+(Complex &c1,Complex &c2);//声明重载函数加作为友元函数
friend Complex operator-(Complex &c1,Complex &c2);//声明重载函数减作为友元函数
friend Complex operator*(Complex &c1,Complex &c2);//声明重载函数加乘作为友元函数
friend Complex operator/(Complex &c1,Complex &c2);//声明重载函数除作为友元函数
Complex operator+(double &d); // 声明一个重载运算符的复数与double型数据相加成员函数
friend Complex operator+( double &d,Complex &c1);//声明复数与double型数据相加友员重载运算符函数
friend Complex operator-(Complex &c2);//声明一目运算符形如"-c"
void display();
private:
double real;
double imag;
};
//定义友元函数加法
Complex operator+(Complex &c1,Complex &c2)
{
return Complex(c1.real + c2.real,c1.imag + c2.imag);
}
//定义复数加double型的数
Complex Complex::operator+( double &d)
{
return Complex(real + d,imag);
}
Complex operator+(double &d,Complex &c1)
{
return Complex(c1.real + d,c1.imag);
}
//定义友元函数减法
Complex operator-(Complex &c1,Complex &c2)
{
return Complex(c1.real - c2.real,c1.imag - c2.imag);
}
//定义一目运算符形如"-c"
Complex operator-(Complex &c2)
{
return Complex(-c2.real, -c2.imag);
}
//定义友元函数乘法
Complex operator*(Complex &c1,Complex &c2)
{
Complex c;
c.real = c1.real*c2.real - c1.imag*c2.imag;
c.imag = c1.real*c2.imag + c1.imag*c2.real;
return c;
}
//定义友元函数除法
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 = (-c1.real*c2.imag + c1.imag*c2.real)/(c2.real*c2.real+c2.imag*c2.imag);
return c;
}
void Complex::display()
{
cout<<"("<<real<<","<<imag<<"i)"<<endl;
}
int main()
{
double m=3.2;
Complex c1(3,4),c2(5,-10),c3;
cout<<"c1=";
c1.display();
cout<<"c2=";
c2.display();
c3=c1+c2;
cout<<"c1+c2=";
c3.display();
c3=c1-c2;
cout<<"c1-c2=";
c3.display();
c3=c1*c2;
cout<<"c1*c2=";
c3.display();
c3=c1/c2;
cout<<"c1/c2=";
c3.display();
cout<<"与double型数据相加的虚数为:"<<endl;
c3=m+c1;
cout<<"m+c1="; //此处调用的是友员函数
c3.display();
c3=c1+m;
cout<<"c1+m="; //此处调用的是成员函数
c3.display();
cout<<"一目运算符的实现:"<<endl;
c3=-c2;
cout<<"-c2=";
c3.display();
system("pause");
return 0;
}
小结:虚数与其他类型的数据相加,可以用成员重载函数也可以用友员重载函数,但是当第一个参数不是类对象时,不能用成员重载函数只能用友员重载函数,在vc++6.0环境下不能做友元函数因为其提供的头文件不支持运载符重载为友元函数,此时可将程序前两行改为#include<iostream.h)