类外定义运算符重载函数
#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++中可重载的运算符;形参表中给出重载运算符所需要的参数和类型。由于成员运算符重载函数是该类的成员函数,所以在类外定义时需要缀上类名。
在成员运算符重载函数的形参表中,若运算符是单目的,则参数表为空;若运算符是双目的,则参数表中有一个操作数。