多态之运算符重载
来源:互联网 发布:知床热气球 编辑:程序博客网 时间:2024/06/11 16:07
1.双目运算符重载
双目运算符重载规则:若要实现表达式oprd1 B oprd2,运算符为B,则需要将B重载为oprd1所属类对象的成员函数,形参为oprd2所属类型
示例代码:复数的加减运算
#include <iostream>using namespace std;class Complex {public: Complex(double r = 0.0, double i = 0.0) : real(r), imag(i) {} Complex operator + (const Complex &c) const; Complex operator - (const Complex &c) const; void display() const;private: double real; double imag;};Complex Complex::operator+(const Complex &c1) const { return Complex(real+c1.real, imag+c1.imag);}Complex Complex::operator-(const Complex &c1) const { return Complex(real-c1.real, imag-c1.imag);}void Complex::display() const { cout << "(" << real << ", " << imag << ")" << endl;}int main() { Complex c1(4, 5), c2(10, 9), c3; cout << "c1 = "; c1.display(); cout << "c2 = "; c2.display(); c3 = c1 - c2; cout << "c3 = c1 - c2 = "; c3.display(); c3 = c1 + c2; cout << "c3 = c1 + c2 = "; c3.display(); return 0;}
运行结果:
2.单目运算符重载为类成员函数
(1)前置单目运算符:oprd是A类对象,U是前置运算符,要实现 U oprd,需要将U重载为A类成员函数,没有形参。
(2)后置单目运算符++和--重载规则:若要实现oprd++或者oprd--,则++或者--要被重载为A类成员函数,有一个int形参。
示例代码:时钟的前置++和后置++
#include <iostream>using namespace std;class Clock {public: Clock(int hours = 0, int minutes = 0, int seconds = 0); Clock &operator ++ (); Clock operator ++ (int); void showTime() const;private: int hours, minutes, seconds;};Clock::Clock(int hours, int minutes, int seconds) { if (0 <= seconds && seconds < 60 && 0 <= minutes && minutes < 60 && 0 <= hours && hours < 24) { this->hours = hours; this->minutes = minutes; this->seconds = seconds; } else cout << "Time error!!" << endl;}Clock& Clock::operator ++ () { seconds++; if (seconds >= 60) { seconds -= 60; minutes++; if (minutes >= 60) { minutes -= 60; hours = (hours+1)%24; } } return *this;}Clock Clock::operator ++ (int) { Clock old = *this; ++(*this); return old;}void Clock::showTime() const { cout << hours << ":" << minutes << ":" << seconds << endl;}int main() { Clock myClock(12, 59, 59); cout << "First showTime: "; myClock.showTime(); cout << "Show myClock++: "; (myClock++).showTime(); cout << "Show ++myClock: "; (++myClock).showTime(); return 0;}
运行结果:
3.运算符重载为非成员函数
当双目运算符左操作数不是类对象或者不是可以重载该运算符的对象。
(1)重载规则如下:
至少有一个自定义类型参数
后置++和后置--的重载参数,形参列表中要增加一个int,不必写参数名称
当重载运算符需要操作类对象的私有成员时,应当声明为该类对象的友元函数
(2)运算符B重载为非成员函数的形式
双目运算符:原表达式oprd1 B oprd 2, 重载后operator B (oprd1, oprd 2)
前置单目运算符:原表达式B oprd, 重载后operator B (oprd)
后置单目运算符, 原表达式oprd B, 重载后operator B (oprd, 0)
示例代码:复数的+,-和<<,均重载为非成员函数
#include <iostream>using namespace std;class Complex {public: Complex(double r = 0.0, double i = 0.0) : real(r), imag(i) {} friend Complex operator + (const Complex &c1, const Complex &c2); friend Complex operator - (const Complex &c1, const Complex &c2); friend ostream &operator << (ostream &out, const Complex &c);private: double real; double 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);}ostream &operator << (ostream &out, const Complex &c) { out << "(" << c.real << ", " << c.imag << ")"; return out;}int main() { Complex c1(5, 4), c2(10, 9), c3; cout << "c1 = " << c1 << endl; cout << "c2 = " << c2 << endl; c3 = c1-c2; cout << "c3 = c1 - c2 = " << c3 << endl; c3 = c1 + c2; cout << "c3 = c1 + c2 = " << c3 << endl; return 0;}运行结果:
0 0
- 多态之运算符重载
- 重载之运算符重载
- 运算符重载之(),++
- 运算符重载之->
- 运算符重载之 * ->
- 运算符重载之* ->
- 运算符重载之 []
- 重载运算符之->
- 多态之运算符重载(一)
- C++重载技术之运算符重载
- 运算符重载之等号
- 重载运算符之二
- 重载运算符之三
- Delphi 之运算符重载
- C#之运算符重载
- C++之运算符重载
- C++之运算符重载
- C++之 运算符重载
- 欢迎使用CSDN-markdown编辑器
- 应届生程序员求职书籍推荐
- linux环境下,配置eclipse nodejs开发环境
- 4种必须知道的Android屏幕自适应解决方案
- python闭包学习笔记四
- 多态之运算符重载
- Android学习笔记之java01
- 2015年第六届蓝桥杯本科B组C++省赛个人题解
- mysql主从复制
- EL表达式杂谈
- GCD实现
- tslib移植笔记(1)
- Android 初探OpenGL ES
- Remove Duplicates from Sorted Array II