c++多态性主要表现在下面几个方面。
1.函数重载
2.运算符重载
3.虚函数
书上着重介绍了运算符的重载。
自己看了半天概念性的东西也没看懂多少,还是不明白运算符重载的意义。
于是就在网上搜索,看到了下面这个例子:
http://www.pconline.com.cn/pcedu/empolder/gj/c/0503/581281.html
class Test { //过程省略 } int main() { Test a,c; c=a+a; }
这样的代码显然是编译不能通过的。这个时候我们就要对+这个运算符进行重载,使得能执行c = a + a的操作。
于是恍然大悟,原来这就是运算符重载在c++当中的意义:可以通过运算符重载完成类的运算符操作。当然意义不止这个。。。
关于运算符重载的具体原则规律,参考了下面这个博客:
http://blog.csdn.net/zgl_dm/article/details/1767201
还是觉个书上面的例子好了。。。码农只会码代码- -|||
#include <iostream> using namespace std; class Complex { public: Complex() { real = imag = 0; } Complex(double r) { real = r; imag = 0; } Complex(double r, double i) { real = r; imag = i; } Complex operator +(const Complex &c); Complex operator -(const Complex &c); Complex operator *(const Complex &c); Complex operator /(const Complex &c); friend void Print(const Complex &c); private: double real, imag; }; //复数加法操作符重载 Complex Complex::operator +(const Complex &c) { return Complex(real + c.real, imag + c.imag); } //复数减法操作符重载 Complex Complex::operator -(const Complex &c) { return Complex(real - c.real, imag - c.imag); } //复数乘法操作符重载 Complex Complex::operator *(const Complex &c) { return Complex(real * c.real - imag * c.imag, real * c.imag + imag * c.real); } //复数除法操作符重载 Complex Complex::operator /(const Complex &c) { return Complex((real * c.real + imag * c.imag) / (c.real * c.real + c.imag * c.imag), (imag * c.real - real * c.imag) / (c.real * c.real + c.imag * c.imag)); } void Print(const Complex &c) { //判断虚部的正负 if (c.imag < 0) cout<<c.real<<c.imag<<'i'<<endl; else cout<<c.real<<'+'<<c.imag<<endl; } int main() { Complex c1(2.5), c2(5.5, -1.0), c3; c3 = c1 + c2; cout<<"c1 + c2 ="; Print(c3); c3 = c1 - c2; cout<<"c1 - c2 ="; Print(c3); c3 = c1 * c2; cout<<"c1 * c2 ="; Print(c3); c3 = c1 / c2; cout<<"c1 / c2 ="; Print(c3); return 0; }
通过这个例子,对运算符重载的作用有了个大概的了解。
运算符重载有两种方法:
1.成员函数的方法
2.友元函数的方法
下面学习一下友元函数的方法。。。。
然后发现自己忘记友元函数了。。。回过头来再次复习一下。。。顺便BS一下自己的记忆力。
看了继承性之后再回过头来看友元函数,觉得更加理解了友元函数存在的意义了。。。
友元函数可以访问类中私有成员~~
接着还是同样的复数运算,这次用友元函数的方法实现,(搞完这个吃饭去- -|||):
#include <iostream> using namespace std; class Complex { public: Complex() { real = imag = 0; } Complex(double r) { real = r; imag = 0; } Complex(double r, double i) { real = r; imag = i; } friend Complex operator +(const Complex &c1, const Complex &c2); friend Complex operator -(const Complex &c1, const Complex &c2); friend Complex operator *(const Complex &c1, const Complex &c2); friend Complex operator /(const Complex &c1, const Complex &c2); friend void Print(const Complex &c); private: double real, imag; }; //复数加法操作符重载 Complex operator +(const Complex &c1, const Complex &c2) { return Complex(c1.real + c2.real, c1.imag + c2.imag); } //复数减法操作符重载 Complex operator -(const Complex &c1, const Complex &c2) { return Complex(c1.real - c2.real, c1.imag - c2.imag); } //复数乘法操作符重载 Complex operator *(const Complex &c1, const Complex &c2) { return Complex(c1.real * c2.real - c1.imag * c2.imag, c1.real * c2.imag + c1.imag * c2.real); } //复数除法操作符重载 Complex operator /(const Complex &c1, const Complex &c2) { return Complex((c1.real * c2.real + c1.imag * c2.imag) / (c2.real * c2.real + c2.imag * c2.imag), (c1.imag * c2.real - c1.real * c2.imag) / (c2.real * c2.real + c2.imag * c2.imag)); } void Print(const Complex &c) { //判断虚部的正负 if (c.imag < 0) cout<<c.real<<c.imag<<'i'<<endl; else cout<<c.real<<'+'<<c.imag<<endl; } int main() { Complex c1(2.5), c2(5.5, -1.0), c3; c3 = c1 + c2; cout<<"c1 + c2 ="; Print(c3); c3 = c1 - c2; cout<<"c1 - c2 ="; Print(c3); c3 = c1 * c2; cout<<"c1 * c2 ="; Print(c3); c3 = c1 / c2; cout<<"c1 / c2 ="; Print(c3); return 0; }
比较这两种方法留到后面再讲。。。。
半天之后。。。。
现在来比较 这两种方法的异同。
先来了解下概念的东西:
单目运算符是指操作数只有一个的运算符, 比如!
双目运算符是值操作数有两个的运算符,比如 + - || &&
对于双目运算符来举例,使用成员函数的方法是将第一个第一个操作数当车功能调用重载运算符成员函数的对象,将第二个操作数作为运算符函数的实参。
而使用友元函数的方式之后,将两个操作数当作函数的实参。
区别就在这里。。。自己也先按照书上讲的理解到这里。