1.友元函数实现运算符重载(复数的加减法)
Complex.h:
#pragma once
#include <string>
using namespace std;
class Complex
{
public:
Complex();
Complex(int r, int i);
void show();
//运算符重载的实质就是函数重载
//友元函数实现运算符重载
friend Complex operator + (const Complex& x, const Complex& y);
friend bool operator == (const Complex & c1, const Complex & c2);
Complex Add(const Complex& x)//成员函数
{
Complex temp;
temp.real = this->real + x.real;
temp.image = this->image + x.image;
return temp;
}
private:
int real;
int image;
};
Complex.cpp:
#include "Complex.h"
#include <iostream>
using namespace std;
Complex::Complex()
{
real = 0;
image = 0;
}
Complex::Complex(int r, int i)
{
this->real = r;
this->image = i;
}
void Complex::show()
{
if (image > 0) {
if (image == 1) {
cout << real << "+i" << endl;
}
else
{
cout << real << "+" << image << "i" << endl;
}
}
else if (image < 0) {
if (image == -1) {
cout << real << "-i" << endl;
}
else
{
cout << real << image << "i" << endl;
}
}
else {
cout << real << endl;
}
}
Complex operator + (const Complex& x, const Complex& y) { //赋值运算符
Complex temp;
temp.real = x.real + y.real;
temp.image = x.image + y.image;
return temp;
}
bool operator == (const Complex& c1, const Complex& c2) {
if (c1.real == c2.real && c1.image == c2.image) {
return true;
}
else {
return false;
}
}
main.cpp:
#include <iostream>
#include "Complex.h"
using namespace std;
int main() {
Complex c1(1, -1), c2(1, -1);
c1.show();
c2.show();
//Complex c3 = c1.Add(c2);
//c3.show();
//Complex c3 = c1 + c2; //隐式的调用
//c3.show();
Complex c3 = operator+(c1,c2);//显式的调用
c3.show();
if (c1 == c2) {
cout << "两个复数相等!" << endl;
}
else {
cout << "两个复数不相等!" << endl;
}
system("pause");
return 0;
}
2.成员函数实现运算符重载
Complex.h:
#pragma once
#include <string>
using namespace std;
class Complex
{
public:
Complex();
Complex(int r, int i);
void show();
//运算符重载的实质就是函数重载
//友元函数实现运算符重载
//friend Complex operator + (const Complex& x, const Complex& y);
//friend bool operator == (const Complex & c1, const Complex & c2);
//成员函数实现运算符的重载
Complex operator + (const Complex& x);
bool operator ==(const Complex & x);
Complex Add(const Complex& x)//成员函数
{
Complex temp;
temp.real = this->real + x.real;
temp.image = this->image + x.image;
return temp;
}
private:
int real;
int image;
};
Complex.cpp:
#include "Complex.h"
#include <iostream>
using namespace std;
Complex::Complex()
{
real = 0;
image = 0;
}
Complex::Complex(int r, int i)
{
this->real = r;
this->image = i;
}
void Complex::show()
{
if (image > 0) {
if (image == 1) {
cout << real << "+i" << endl;
}
else
{
cout << real << "+" << image << "i" << endl;
}
}
else if (image < 0) {
if (image == -1) {
cout << real << "-i" << endl;
}
else
{
cout << real << image << "i" << endl;
}
}
else {
cout << real << endl;
}
}
Complex Complex::operator+(const Complex& x)
{
Complex temp;
temp.real = x.real + this->real;
temp.image = x.image + this->image;
return temp;
}
bool Complex::operator==(const Complex& x)
{
if (this->real == x.real && this->image == x.image) {
return true;
}
return false;
}
main.cpp:
#include <iostream>
#include "Complex.h"
using namespace std;
int main() {
Complex c1(1, -1), c2(1, -1);
c1.show();
c2.show();
//Complex c3 = c1.operator+(c2); //显式的调用
//c3.show();
Complex c3 = c1 + c2; //隐式的调用
c3.show();
if (c1 == c2) {
cout << "两个复数相等!" << endl;
}
else {
cout << "两个复数不相等!" << endl;
}
system("pause");
return 0;
}
3.输入与输出运算符的重载
Complex.h:
#pragma once
#include <string>
using namespace std;
class Complex
{
public:
Complex();
Complex(int r,int i);
void display();
//输入与输出运算符的重载只能使用友元函数
friend ostream& operator <<(ostream &myout,const Complex &c);
friend istream& operator >>(istream &myin,Complex &c);
private:
int real; //实部
int image; //虚部
};
Complex.cpp:
#include "Complex.h"
#include <iostream>
#include <string>
using namespace std;
Complex::Complex()
{
real = 0;
image = 0;
}
Complex::Complex(int r, int i)
{
this->real = r;
this->image = i;
}
void Complex::display()
{
if (image > 0) {
if (image == 1) {
cout << real << "+i" << endl;
}
else {
cout << real << "+"<< image << "i" << endl;
}
}
else if (image < 0) {
if (image == -1) {
cout << real << "-i" << endl;
}
else {
cout << real << image << endl;
}
}
else {
cout << real << endl;
}
}
ostream& operator<<(ostream& myout, const Complex& c)
{
if (c.image > 0) {
if (c.image == 1) {
myout << c.real << "+i" << endl;
}
else {
myout << c.real << "+" << c.image << "i" << endl;
}
}
else if (c.image < 0) {
if (c.image == -1) {
myout << c.real << "-i" << endl;
}
else {
myout << c.real << c.image << endl;
}
}
else {
myout << c.real << endl;
}
return myout;
}
istream& operator>>(istream& myin, Complex& c)
{
myin >> c.real >> c.image;
return myin;
}
main.cpp:
#include <iostream>
#include <string>
#include <iomanip>
#include "Complex.h"
using namespace std;
int main() {
Complex c1(3, 1), c2(4, -1);
cin >> c1 >> c2;
cout << c1 << c2;
system("pause");
return 0;
}
4.自增运算符++的重载
Complex.h:
#pragma once
#include <string>
using namespace std;
class Complex
{
public:
Complex();
Complex(int r,int i);
void display();
Complex operator ++ () {
this->real++;
this->image++;
return *this;
}
Complex operator ++(int) {
Complex temp = *this;
this->real++;
this->image++;
return temp;
}
private:
int real; //实部
int image; //虚部
};
Complex.cpp:
#include "Complex.h"
#include <iostream>
#include <string>
using namespace std;
Complex::Complex()
{
real = 0;
image = 0;
}
Complex::Complex(int r, int i)
{
this->real = r;
this->image = i;
}
void Complex::display()
{
if (image > 0) {
if (image == 1) {
cout << real << "+i" << endl;
}
else {
cout << real << "+"<< image << "i" << endl;
}
}
else if (image < 0) {
if (image == -1) {
cout << real << "-i" << endl;
}
else {
cout << real << image << endl;
}
}
else {
cout << real << endl;
}
}
ostream& operator<<(ostream& myout, const Complex& c)
{
if (c.image > 0) {
if (c.image == 1) {
myout << c.real << "+i" << endl;
}
else {
myout << c.real << "+" << c.image << "i" << endl;
}
}
else if (c.image < 0) {
if (c.image == -1) {
myout << c.real << "-i" << endl;
}
else {
myout << c.real << c.image << endl;
}
}
else {
myout << c.real << endl;
}
return myout;
}
istream& operator>>(istream& myin, Complex& c)
{
myin >> c.real >> c.image;
return myin;
}
main.cpp:
#include <iostream>
#include <string>
#include <iomanip>
#include "Complex.h"
using namespace std;
int main() {
Complex c1(3, 1), c2(4, -1);
//c1.display();
//c2.display();
//c1.operator++();
Complex c3;
c3 = ++c1;
c1.display();
c3.display();
string line(50,'-');
cout << line << endl;
Complex c4;
c4 = c2++;
c2.display();
c4.display();
system("pause");
return 0;
}