一.操作符重载
Q:下面的复数解决方案是否可行?
class Complex
{
public:
int a;
int b;
};
int main()
{
Complex c1={1,2};
Complex c2={3,4};
Complex c3=c1+c2;
return 0;
}
该段代码想要实现的是将两个复数类进行相加得出第三个类
代码实现的运行结果
由上面的结果图可以得知,出现的错误是无法匹配+号操作符的操作,同时出现 的潜在问题是a与b是public成员,在实际的操作中应将a与b设置为private成员
改正的代码示例
#include <iostream>
using namespace std;
class Complex
{
int a;
int b;
public:
Complex(int a = 0, int b = 0)
{
this->a = a;
this->b = b;
}
int getA()
{
return a;
}
int getB()
{
return b;
}
friend Complex Add(const Complex& p1, const Complex& p2);
};
Complex Add(const Complex& p1, const Complex& p2)
{
Complex ret;
ret.a = p1.a + p2.a;
ret.b = p1.b + p2.b;
return ret;
}
int main()
{
Complex c1(1, 2);
Complex c2(3, 4);
Complex c3 = Add(c1, c2); // c1 + c2
cout<<"c3.a ="<<c3.getA()<<endl;
cout<<"c3.b ="<<c3.getB()<<endl;
return 0;
}
该代码运行了友元函数friend,同时定义了全局函数Add,将a与b设置为私有成员
运行结果
出现的疑问:Add函数可以解决Complex对象相加的问题,但是Complex是现实世界中确实存在的复数,并且复数在数学中的地位和普通的实数相同---为什么不能让+操作符也支持复数相加?
操作符重载
1.C++中的重载能够扩展操作符的功能
2.操作符的重载以函数的方式进行
本质--用特殊形式的函数扩展操作符的功能
通过operator关键字可以定义特殊的函数
operator的本质是通过函数重载操作符
语法
操作符重载示例
#nclude <isotream>
using namespace std;
class Complex
{
int a;
int b;
public:
Complex(int a = 0, int b = 0)
{
this->a = a;
this->b = b;
}
int getA()
{
return a;
}
int getB()
{
return b;
}
friend Complex operator + (const Complex& p1, const Complex& p2);
};
Complex operator + (const Complex& p1, const Complex& p2)
{
Complex ret;
ret.a = p1.a + p2.a;
ret.b = p1.b + p2.b;
return ret;
}
int main()
{
Complex c1(1, 2);
Complex c2(3, 4);
Complex c3 = c1 + c2; // operator + (c1, c2)
cout<<"c3.a ="<<c3.getA()<<endl;
cout<<"c3.b ="<<c3.getB()<<endl;
return 0;
}
运行结果如图所示
可以将操作符重载定义为类的成员函数
1.比全局操作符重载函数少一个参数
2.不需要依赖友元就可以完成操作符重载
3.编译器优先在成员函数中寻找操作符重载函数
小结
1.操作符重载是C++的强大特性之一
2.操作符重载的本质是通过函数扩展操作符的功能
3.operator关键字是实现操作符重载的关键
4.操作符重载遵循相同的函数重载规则
5.全局函数和成员函数都可以实现对操作符的重载
二.完整复数类
复数类应该具有的操作
利用操作符重载
1.统一复数与实数的运算方式
2.统一复数与实数的比较方式
double getModulus();
Complex operator + (const Complex& c);
Complex operator - (const Complex& c);
Complex operator * (const Complex& c);
Complex operator / (const Complex& c);
bool operator == (const Complex& c);
bool operator != (const Complex& c);
Complex& operator = (const Complex& c);
复数类的实现
Complex.cpp
#include "Complex.h"
#include "math.h"
Complex::Complex(double a, double b)
{
this->a = a;
this->b = b;
}
double Complex::getA()
{
return a;
}
double Complex::getB()
{
return b;
}
double Complex::getModulus()
{
return sqrt(a * a + b * b);
}
Complex Complex::operator + (const Complex& c)
{
double na = a + c.a;
double nb = b + c.b;
Complex ret(na, nb);
return ret;
}
Complex Complex::operator - (const Complex& c)
{
double na = a - c.a;
double nb = b - c.b;
Complex ret(na, nb);
return ret;
}
Complex Complex::operator * (const Complex& c)
{
double na = a * c.a - b * c.b;
double nb = a * c.b + b * c.a;
Complex ret(na, nb);
return ret;
}
Complex Complex::operator / (const Complex& c)
{
double cm = c.a * c.a + c.b * c.b;
double na = (a * c.a + b * c.b) / cm;
double nb = (b * c.a - a * c.b) / cm;
Complex ret(na, nb);
return ret;
}
bool Complex::operator == (const Complex& c)
{
return (a == c.a) && (b == c.b);
}
bool Complex::operator != (const Complex& c)
{
return !(*this == c);
}
Complex& Complex::operator = (const Complex& c)//返回值是个引用
{
if( this != &c )
{
a = c.a;
b = c.b;
}
return *this;
}
Complex.h
#ifndef _COMPLEX_H_
#define _COMPLEX_H_
class Complex
{
double a;
double b;
public:
Complex(double a = 0, double b = 0);
double getA();
double getB();
double getModulus();
Complex operator + (const Complex& c);
Complex operator - (const Complex& c);
Complex operator * (const Complex& c);
Complex operator / (const Complex& c);
bool operator == (const Complex& c);
bool operator != (const Complex& c);
Complex& operator = (const Complex& c);
};
#endif
test.cpp
#include <stdio.h>
#include "Complex.h"
int main()
{
Complex c1(1, 2);
Complex c2(3, 6);
Complex c3 = c2 - c1;
Complex c4 = c1 * c3;
Complex c5 = c2 / c1;
printf("c3.a = %f, c3.b = %f\n", c3.getA(), c3.getB());
printf("c4.a = %f, c4.b = %f\n", c4.getA(), c4.getB());
printf("c5.a = %f, c5.b = %f\n", c5.getA(), c5.getB());
Complex c6(2, 4);
printf("c3 == c6 : %d\n", c3 == c6);
printf("c3 != c4 : %d\n", c3 != c4);
(c3 = c2) = c1;
printf("c1.a = %f, c1.b = %f\n", c1.getA(), c1.getB());
printf("c2.a = %f, c2.b = %f\n", c2.getA(), c2.getB());
printf("c3.a = %f, c3.b = %f\n", c3.getA(), c3.getB());
return 0;
}
输出结果
注意事项
1.C++规定赋值操作符(=)只能重载为成员函数
2.操作符重载不能改变原操作符的优先级
3.操作符重载不能改变操作的个数
4.操作符重载不应该改变操作符的原有语义
小结
1.复数的概念可以通过自定义类实现
2.复数中的运算操作可以通过操作符重载来实现
3.赋值操作符只能通过成员函数实现
4.操作符重载的本质为函数定义
原文地址:https://blog.51cto.com/13475106/2410891