C++上机7

类外定义运算符重载函数

#include <iostream>
using namespace std;
class Complex {
public:
	double real;
	double imag;
	Complex(double r = 0, double i = 0)
	{
		real = r; imag = i;
	}
};
Complex operator+(Complex co1, Complex co2)
{
	Complex temp;
	temp.real = co1.real + co2.real;
	temp.imag = co1.imag + co1.imag;
	return temp;
}
int main()
{
	Complex com1(1.1, 2.2), com2(3.3, 4.4),total1,total2;
	total1 = operator+(com1, com2);
	cout << "real1=" << total1.real << " " << "imag1=" << total1.imag << endl;
	total2 = com1 + com2;
	cout << "real2=" << total2.real << " " << "imag2=" << total2.imag << endl;
	return 0;
}

友元运算符重载函数

#include <iostream>
using namespace std;
class Complex {
double real;
	double imag;
public:
	Complex(double r = 0, double i = 0)
	{
		real = r; imag = i;
	}
	void print();
	friend Complex operator+(Complex co1, Complex co2);
};
Complex operator+(Complex co1, Complex co2)
{
	Complex temp;
	temp.real = co1.real + co2.real;
	temp.imag = co1.imag + co1.imag;
	return temp;
}
void Complex::print()
{
	cout << "total real=" << real << " " << " total imag=" << imag << endl;
}
int main()
{
	Complex com1(1.1, 2.2), com2(3.3, 4.4),total1;
	total1 = com1 + com2;
	total1.print();
	return 0;
}

成员运算符重载函数

#include <iostream>
using namespace std;
class Complex {
double real;
	double imag;
public:
	Complex(double r = 0, double i = 0);
	void print();
	Complex operator+(Complex c);
};
Complex::Complex(double r, double i)
{
	real = r; imag = i;
}

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

void Complex::print()
{
	cout << "total real=" << real << " " << " total imag=" << imag << endl;
}
int main()
{
	Complex com1(2.3, 4.6), com2(3.6, 2.8),total1;
	total1 = com1 + com2;
	total1.print();
	return 0;
}


 

 

1 .成员运算符重载函数

#include <iostream>
using namespace std;
class Complex {
public:
	
	Complex(double r = 0.0, double i = 0.0);
	void print();
	Complex operator*(Complex c);
private:
	double real;
	double imag;
};
Complex::Complex(double r, double i)
{
	real = r; imag = i;
}

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

void Complex::print()
{
	cout << "total real=" << real << " " << " total imag=" << imag << endl;
}
int main()
{
	Complex com1(2.3, 4.6), com2(3.6, 2.8),total1;
	total1 = com1 * com2;
	total1.print();
	return 0;
}


2 .友元运算符重载函数

#include <iostream>
using namespace std;
class Complex {
public:
	
	Complex(double r = 0.0, double i = 0.0);
	void print();
	friend Complex operator*(Complex a, Complex b);
private:
	double real;
	double imag;
};

Complex::Complex(double r, double i)
{
	real = r; imag = i;
}

Complex operator*(Complex a, Complex b)
{
	Complex temp;
	temp.real = a.real * b.real-a.imag * b.imag;
	temp.imag = a.real * b.imag+a.imag * b.real;
	return temp;
}

void Complex::print()
{
	cout << "total real=" << real << " " << " total imag=" << imag << endl;
}
int main()
{
	Complex com1(2.3, 4.6), com2(3.6, 2.8),total;
	total = com1 * com2;
	total.print();
	return 0;
}

 

        其中,X是友元运算符重载函数所在类的类名;函数类型指定了友元运算符函数的返回值类型;operator是定义运算符重载函数的关键字;运算符即是要重载的运算符名称,必须是C++中可重载的运算符;形参表中给出重载运算符所需要的参数和类型;关键字friend表明这是一个友元运算符重载函数。由于友元运算符重载函数不是该类的成员丽数,所以在类外定义时不需要缀上类名。
        若友元运算符重载函数重载的是双目运算符,则参数表中有两个操作数;若重载的是单目运算符.则参数表中只有一个操作数。

 

       其中,X是成员运算符重载函数所在类的类名;函数类型指定了成员运算符函数的返回值类型;operator是定义运算符重载函数的关键字;运算符即是要重载的运算符名称,必须是C++中可重载的运算符;形参表中给出重载运算符所需要的参数和类型。由于成员运算符重载函数是该类的成员函数,所以在类外定义时需要缀上类名。
        在成员运算符重载函数的形参表中,若运算符是单目的,则参数表为空;若运算符是双目的,则参数表中有一个操作数。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值