运算符重载

Posted on 2019-07-15 17:41  金色的省略号  阅读(111)  评论(0编辑  收藏  举报
  1、Clock类,运算符 >、运算符 < 、友元函数运算符>、运算符 < 重载
#include<iostream>
#include<iomanip>
using namespace std;
class Clock{
    int hour;
    int minute;
    int second;
public:
    Clock(int hour=0, int minute=0, int second=0);

    void setTime(int hour, int minute, int second);
    void showTime();

    bool operator > (Clock& c);
    bool operator < (Clock& c);
};
//构造
Clock::Clock(int hour, int minute, int second)
{
    this->hour = hour;
    this->minute = minute;
    this->second = second;
}
//设置
void Clock::setTime(int hour, int minute, int second)
{
    this->hour = hour;
    this->minute = minute;
    this->second = second;
}
//显示
void Clock::showTime(){
    cout<<setw(2)<<setfill('0')<<hour<<":";
    cout<<setw(2)<<setfill('0')<<minute<<":";
    cout<<setw(2)<<setfill('0')<<second<<":";
    cout<<endl;
}
//重载>
bool Clock::operator > (Clock& c){
    return (hour*60*60 + minute*60 + second)>
         (c.hour*60*60 + c.minute*60 + c.second);
}
//重载<
bool Clock::operator < (Clock& c){
    return (hour*60*60 + minute*60 + second)<
         (c.hour*60*60 + c.minute*60 + c.second);
}
    /* //友元函数,运算符重载
    friend bool operator > (Clock& c1,Clock& c2){
        return (hour*60*60 + minute*60 + second)>
         (c.hour*60*60 + c.minute*60 + c.second);
    }
    friend bool operator < (Clock& c1,Clock& c2){
        return (hour*60*60 + minute*60 + second)<
         (c.hour*60*60 + c.minute*60 + c.second);
    }
    */
int main()
{
    Clock c1,c2;
    c1.showTime();
    c2.showTime();

    c1.setTime(1,55,30);
    c2.setTime(12,4,55);
    c1.showTime();
    c2.showTime();

    bool timeCompare = c1>c2;
    cout<<"c1>c2=" << boolalpha << timeCompare << endl;

    timeCompare = c1<c2;
    cout<<"c1<c2=" << boolalpha << timeCompare << endl;
    return 0;
}
View Code

  2、友元函数,运算符 + 、运算符 << 重载

#include<iostream>
using namespace std;
class Counter  {
public:
     Counter (unsigned int xx=0) {value=xx;}
     //友元函数(运算符重载)
     friend Counter operator + (const Counter &c1,const Counter &c2);
     friend ostream & operator<<(ostream &out, const Counter &c);
private:
     unsigned int value;
};
Counter operator + (const  Counter &c1,const Counter &c2)
{
    return  Counter(c1.value + c2.value);
}
ostream & operator <<(ostream &out, const Counter &c)
{   out<< c.value;
    return out;
}
int main()
{
    Counter c1(5), c2(2), c3;
    cout<<"c1="<<c1<<endl;
    cout<<"c2="<<c2<<endl;
    //c3=c1+c2;
    cout<<"c3=c1+c2="<<c1+c2<<endl;
    return 0;
}
View Code

  3、前置++,后置++,友元函数,运算符 << 重载

#include <iostream>
using namespace std;

class A{
public:
    A(int i):x(i){}
    /* 前置++ */
    A& operator++()
    {
        this->x++;
        return *(this);
    }
    /* 后置++ */
    A operator++(int)
    {
        A t(x);
        ++x;
        return t;
    }
    friend ostream& operator << (ostream &o, const A &a);
private:
    int x;
};
/* 友元函数 */
ostream& operator << (ostream &o, const A &a){
    o << a.x;
    return o;
}

int main()
{
    A a(7);
    cout<< "前置++" << ++a << endl;
    cout<< "后置++" << a++ << endl;
    cout<< a << endl;
    return 0;
}
View Code

  4、模板类,运算符 * 重载,构造,析构

#include <iostream>
using namespace std;

class Hero{
public:
    Hero(){
        printf("Hero.\n");
    }
    ~Hero(){
        printf("~Hero.\n");
    }
    void showInfo(){
        printf("This is the Hero.\n");
    }
};

template <class T>
class Point{
    T *ptr;
public:
    Point(T *ptr):ptr(ptr){
    }
    T& operator *(){
        return *ptr;
    }
    ~Point(){
        printf("~Point.\n");
        if(ptr) delete ptr;
    }
};

int main()
{
    Point<Hero> p(new Hero());
    (*p).showInfo();
    return 0;
}
View Code