首页 > 编程知识 正文

单目运算符作为类成员函数重载时,双目运算符重载为类的成员函数,其左操作数为

时间:2023-05-04 18:04:33 阅读:226501 作者:2821

1.单目运算符只能重载为类的成员函数
Clock& operator ++ ();//前置单目运算符重载
  没有形参,返回的是clock类的引用,是一个左值。
Clock operator ++ (int); //后置单目运算符重载
  后置++要有一个int作形参,返回的是一个clock类的旧值,是一个右值

2.双目运算符
重载为成员函数时:
  Complex operator + (const Complex &c2) const;
  只有一个形参。
重载为非成员函数时:
  friend Complex operator+(const Complex &c1, const Complex &c2);
  有两个复数类参数,且函数要设为友元函数。
注意:基础数据类型之间的操作符无法重载
例如:MyClock operator+(long, long);这个声明是错误的。

//单目运算符重载#include <iostream>using namespace std;class Clock {//时钟类定义public: Clock(int hour = 0, int minute = 0, int second = 0); void showTime() const; //前置单目运算符重载 Clock& operator ++ (); //后置单目运算符重载 Clock operator ++ (int); private: int hour, minute, second;};Clock::Clock(int hour, int minute, int second) { if (0 <= hour && hour < 24 && 0 <= minute && minute < 60 && 0 <= second && second < 60) { this->hour = hour; this->minute = minute; this->second = second; } else cout << "Time error!" << endl;}void Clock::showTime() const { //显示时间 cout << hour << ":" << minute << ":" << second << endl;}Clock & Clock::operator ++ () { second++; if (second >= 60) { second -= 60; minute++; if (minute >= 60) { minute -= 60; hour = (hour + 1) % 24; } } return *this;//返回的是clock对象的引用,是一个左值}Clock Clock::operator ++ (int) { //注意形参表中的整型参数 Clock old = *this; ++(*this); //调用前置“++”运算符 return old;//返回的是旧的对象,是一个右值}int main() { Clock myClock(23, 59, 59); cout << "First time output: "; myClock.showTime(); cout << "Show myClock++: "; (myClock++).showTime(); cout << "Show ++myClock: "; (++myClock).showTime(); return 0;} //双目运算符重载为类的成员函数#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 &c2) const; //运算符-重载成员函数 Complex operator - (const Complex &c2) const; void display() const; //输出复数private: double real; //复数实部 double imag; //复数虚部};Complex Complex::operator+(const Complex &c2) const{ //创建一个临时无名对象作为返回值 return Complex(real+c2.real, imag+c2.imag); }Complex Complex::operator-(const Complex &c2) const{ //创建一个临时无名对象作为返回值 return Complex(real-c2.real, imag-c2.imag); }void Complex::display() const { cout<<"("<<real<<", "<<imag<<")"<<endl;}int main() { Complex c1(5, 4), c2(2, 10), 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;} //双目运算符重载为非类成员函数#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(2, 10), 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;}

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。