C++学习:类的使用--运算符重载

我们知道C++可以对函数进行重载,让同名的函数来完成相同的基本操作。其实运算符也是可以重载的,而且有的运算符已经在使用了,就像*,既可以用于地址,又可以用于乘法。

下面是一个计算时间的类

#ifndef MYTIME_H
#define MYTIME_H

#include 

using namespace  std;

class Time
{
private:
    int hours;
    int minutes;

public:
    Time();
    Time(int h, int m = 0);
    void AddMin(int m);
    void AddHr(int h);
    void Reset(int h = 0, int m = 0);
    Time Sum(const Time& t) const;
    void Show() const;
};

#endif // MYTIME_H

 

#include "mytime.h"


Time::Time()
{
    hours = minutes = 0;
}

Time::Time(int h, int m)
{
    hours = h;
    minutes = m;
}

void Time::AddMin(int m)
{
    minutes += m;
    hours += minutes/60;
    minutes %= 60;
}

void Time::AddHr(int h)
{
    hours += h;
}

void Time::Reset(int h, int m)
{
    hours = h;
    minutes = m;
}

Time Time::Sum(const Time &t) const
{
    Time sum;
    sum.minutes = minutes + t.minutes;
    sum.hours = hours + t.hours + sum.minutes/60;
    sum.minutes %= 60;

    return sum;
}

void Time::Show() const
{
    cout << hours << " hours, " << minutes << " minutes.\n";
}

 

#include 
#include "mytime.h"

using namespace std;

int main()
{
    Time planning;
    Time coding(2, 40);
    Time fixing(5, 55);
    Time total;

    cout << "planning time = ";
    planning.Show();
    cout << endl;

    cout << "coding time = ";
    coding.Show();
    cout << endl;

    cout << "fixing time = ";
    fixing.Show();
    cout << endl;

    total = coding.Sum(fixing);
    cout << "coding.Sum(fixing) = ";
    total.Show();
    cout << endl;

    return 0;
}

输出结果

planning time = 0 hours, 0 minutes.

coding time = 2 hours, 40 minutes.

fixing time = 5 hours, 55 minutes.

coding.Sum(fixing) = 8 hours, 35 minutes.

添加加法运算符

将Time类转换为重载的加法运算法很容易,只要将Sum()的名称改为operator +()即可。只要把运算符+放在operator的后面

#ifndef STOCK_H
#define STOCK_H
#include 

using namespace std;

class stock
{
private:
    string company;
    long shares;
    double share_val;
    double total_val;
    void set_tot() {total_val = shares * share_val;}

public:
    void buy(long num, double price);
    void sell(long num, double price);
    void update(double price);
    void show() const;
    const stock& topval(const stock& s) const;

    stock(const string& co, long n, double pr);
    stock();
    ~stock();
};

#endif // STOCK_H

#include "mytime.h"


Time::Time()
{
    hours = minutes = 0;
}

Time::Time(int h, int m)
{
    hours = h;
    minutes = m;
}

void Time::AddMin(int m)
{
    minutes += m;
    hours += minutes/60;
    minutes %= 60;
}

void Time::AddHr(int h)
{
    hours += h;
}

void Time::Reset(int h, int m)
{
    hours = h;
    minutes = m;
}

//运算符+重载
Time Time::operator+(const Time &t) const
{
    Time sum;
    sum.minutes = minutes + t.minutes;
    sum.hours = hours + t.hours + sum.minutes/60;
    sum.minutes %= 60;

    return sum;
}

void Time::Show() const
{
    cout << hours << " hours, " << minutes << " minutes.\n";
}
operator+(const Time &t) const与Sum(const Time &t) const效果完全一样,可以理解operator+()就是一个函数,也可以被调用。就像下面的语句一样。
total = coding.operator+(fixing);

但是可以进一步简化

total = coding + fixing;

 这两种方法都讲使用operator+()函数,运算符左侧的coding是调用对象,右边的fixing是参数被传递的对象。

#include 
#include "mytime.h"

using namespace std;

int main()
{
    Time planning;
    Time coding(2, 40);
    Time fixing(5, 55);
    Time total;

    cout << "planning time = ";
    planning.Show();
    cout << endl;

    cout << "coding time = ";
    coding.Show();
    cout << endl;

    cout << "fixing time = ";
    fixing.Show();
    cout << endl;


    //total = coding.operator+(fixing);
    total = coding + fixing;
    cout << "coding.Sum(fixing) = ";
    total.Show();
    cout << endl;

    return 0;
}

输出结果没有变化

planning time = 0 hours, 0 minutes.

coding time = 2 hours, 40 minutes.

fixing time = 5 hours, 55 minutes.

coding.Sum(fixing) = 8 hours, 35 minutes.

 运算符重载的限制:

1、重载后的运算符必须至少有一个操作数是用户定义的类型;

2、使用运算符使不能违反运算符原来的句法规则;

3、不能创建新的运算符;

4、有些运算符不能被重载:如sizeof, ?:, ::等

5、大多数运算符都可以通过成员或者非成员函数进行重载,但=、()、[]、->只能通过成员函数进行重载。

+ 、-、 *重载的应用

#ifndef MYTIME_H
#define MYTIME_H

#include 

using namespace  std;

class Time
{
private:
    int hours;
    int minutes;

public:
    Time();
    Time(int h, int m = 0);
    void AddMin(int m);
    void AddHr(int h);
    void Reset(int h = 0, int m = 0);
    Time operator+(const Time& t) const;
    Time operator-(const Time& t) const;
    Time operator*(double mult) const;
    void Show() const;
};

#endif // MYTIME_H
#include "mytime.h"


Time::Time()
{
    hours = minutes = 0;
}

Time::Time(int h, int m)
{
    hours = h;
    minutes = m;
}

void Time::AddMin(int m)
{
    minutes += m;
    hours += minutes/60;
    minutes %= 60;
}

void Time::AddHr(int h)
{
    hours += h;
}

void Time::Reset(int h, int m)
{
    hours = h;
    minutes = m;
}

//运算符+重载
Time Time::operator+(const Time &t) const
{
    Time sum;
    sum.minutes = minutes + t.minutes;
    sum.hours = hours + t.hours + sum.minutes/60;
    sum.minutes %= 60;

    return sum;
}

Time Time::operator-(const Time& t) const
{
    Time diff;
    int tot1, tot2;
    tot1 = t.minutes + 60 * t.hours;
    tot2 = minutes + 60 * hours;
    diff.minutes = (tot2 - tot1) % 60;
    diff.hours = (tot2 - tot1) / 60;

    return diff;
}


Time Time::operator*(double mult) const
{
    Time result;
    long totalminutes = hours * mult *60 + minutes * mult;

    result.hours = totalminutes / 60;
    result.minutes = totalminutes % 60;

    return result;
}

void Time::Show() const
{
    cout << hours << " hours, " << minutes << " minutes.\n";
}
#include 
#include "mytime.h"

using namespace std;

int main()
{
    Time weeding(4, 35);
    Time waxing(2, 47);

    Time total;
    Time diff;
    Time adjusted;

    cout << "weeding time =  ";
    weeding.Show();
    cout << endl;

    cout << "waxing time =  ";
    waxing.Show();
    cout << endl;

    cout << "total working time =  ";
    total = weeding + waxing;
    total.Show();
    cout << endl;

    diff = weeding - waxing;
    cout << "weeding time - waxing time = ";
    diff.Show();
    cout << endl;

    adjusted = total * 1.5;
    cout << "adjusted work time = ";
    adjusted.Show();
    cout << endl;

    return 0;
}

输出结果

weeding time =  4 hours, 35 minutes.

waxing time =  2 hours, 47 minutes.

total working time =  7 hours, 22 minutes.

weeding time - waxing time = 1 hours, 48 minutes.

adjusted work time = 11 hours, 3 minutes.

你可能感兴趣的:(C++,c++,学习,算法)