【题解】【运算符重载】Complex

题目描述

1.定义一个复数类Complex,重载运算符"+",使之能用于复数的加法运算。将运算符函数重载为非成员,非友元的普通函数。

#include<iostream>
#include<math.h>
#include<iomanip>
using namespace std;
class Complex{
	private:
		double real,image;
	public:
		Complex(double real=0,double image=0);
		double get_real();
		double get_image();
		void display();
};

int main(){
	Complex operator+(Complex &c1,Complex &c2);
	Complex c1(3,4),c2(5,-10),c3;
	c3 = c1 + c2;
	c3.display();
	
	return 0;
}
Complex::Complex(double real,double image){
	this->real = real;
	this->image = image;
}
double Complex::get_real(){
	return real;
}
double Complex::get_image(){
	return image;
}
void Complex::display(){
	if(image > 0)
		cout<<real<<"+"<<image<<"i"<<"\n";
	else if(image < 0)
		cout<<real<<image<<"i"<<"\n";
	else
		cout<<real<<"\n";
}
Complex operator+(Complex &c1,Complex &c2){
	return Complex(c1.get_real()+c2.get_real(),c1.get_image()+c2.get_image());
}

2.定义一个复数类Complex,重载运算符+、-、*、/

#include<iostream>
using namespace std;
class Complex{
	public:
		Complex(){real = 0;image = 0;}
		Complex(double r,double i){real = r;image = i;}
		Complex operator+(Complex &c2);
		Complex operator-(Complex &c2);
		Complex operator*(Complex &c2);
		Complex operator/(Complex &c2);
		void display();
	private:
		double real;
		double image;
}; 
int main(){
	Complex c1(3,4),c2(5,-10),c3;
	c3 = c1 + c2;
	c3.display();
	c3 = c1 - c2;
	c3.display();
	c3 = c1 * c2;
	c3.display();
	c3 = c1 / c2;
	c3.display();
return 0;	
} 
void Complex::display(){
	if(image > 0)
		cout<<real <<"+"<<image<<"i"<<"\n";
	else if(image < 0)
		cout<<real<<image<<"i"<<"\n";
	else 
		cout<<real;
}
Complex Complex::operator+(Complex &c2){
	Complex temp;
	temp.real = real + c2.real;
	temp.image = image + c2.image;
	return temp;
}
Complex Complex::operator-(Complex &c2){
	Complex temp;
	temp.real = real - c2.real;
	temp.image = image - c2.image;
	return temp;
}
Complex Complex::operator*(Complex &c2){
	Complex temp;
	temp.real = real * c2.real - image*c2.image;
	temp.image = image*c2.real + real*c2.image;
	return temp;
}
Complex Complex::operator/(Complex &c2){
	Complex temp;
	temp.real = (real * c2.real + image*c2.image) / (c2.real*c2.real + c2.image*c2.image);
	temp.image = (image*c2.real - real*c2.image) /	(c2.real*c2.real + c2.image*c2.image);
	return temp;
}

3.定义一个复数类Complex,重载运算符 “+” ,使之能用于复数的加法运算。参加运算的两个运算量都可以是类对象,也可以其中一个是整数,顺序任意。例如c1 + c2 , 3 + c2 , c2 + 3。编写以上程序。

#include<iostream>
using namespace std;
class Complex{
	public:
		Complex(){real = 0;image = 0;}
		Complex(double r,double i){real = r;image = i;}
		Complex operator+(Complex &c2);
		Complex operator-(Complex &c2);
		Complex operator*(Complex &c2);
		Complex operator/(Complex &c2);
		Complex operator+(int &i);
		friend Complex operator+(int &,Complex &);
		void display();
	private:
		double real;
		double image;
}; 
int main(){
	Complex c1(3,4),c2(5,-10),c3;
	c3 = c1 + c2;
	c3.display();
	int i=5;
	c3 = i + c1;
	c3.display();
	c3 = c1 + i;
	c3.display(); 
return 0;	
} 
void Complex::display(){
	if(image > 0)
		cout<<real <<"+"<<image<<"i"<<"\n";
	else if(image < 0)
		cout<<real<<image<<"i"<<"\n";
	else 
		cout<<real;
}
Complex Complex::operator+(Complex &c2){
	Complex temp;
	temp.real = real + c2.real;
	temp.image = image + c2.image;
	return temp;
}
Complex Complex::operator-(Complex &c2){
	Complex temp;
	temp.real = real - c2.real;
	temp.image = image - c2.image;
	return temp;
}
Complex Complex::operator*(Complex &c2){
	Complex temp;
	temp.real = real * c2.real - image*c2.image;
	temp.image = image*c2.real + real*c2.image;
	return temp;
}
Complex Complex::operator/(Complex &c2){
	Complex temp;
	temp.real = (real * c2.real + image*c2.image) / (c2.real*c2.real + c2.image*c2.image);
	temp.image = (image*c2.real - real*c2.image) /	(c2.real*c2.real + c2.image*c2.image);
	return temp;
}
Complex Complex::operator+(int &i){
	Complex temp;
	temp.real = real + i;
	temp.image = image;
	return temp;	
}
Complex operator+(int &i,Complex &c2){
	Complex temp;
	temp.real = i + c2.real;
	temp.image = c2.image;
	return temp;
}

如果是类的成员函数进行运算符重载,那么可以将函数的形参个数 = 操作数 - 1(参照例二)。
如果是类的友元函数,函数的形参个数 = 操作数,注意函数定义时不需要类的限定符,还有使用时不需要对象名.函数名。友元函数的好处是可以访问到类的私有成员变量(参照例三)。
如果是非成员,非友元的普通函数,函数的形参个数 = 操作数,由于不能访问类的私有成员变量,首先要保证类要有get()函数,然后在普通函数体内,可以通过get()函数访问到私有成员变量(参照例三)。

已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 游动-白 设计师:上身试试 返回首页