踏进C++程序世界-操作符运算及操作符重载

走进C++程序世界------操作符运算及操作符重载

重载,在一个类定义中,可以编写几个同名的方法,但是只要它们的签名参数列表不同,Java就会将它们看做唯一的方法。简单的说,一个类中的方法与另一个方法同名,但是参数表不同,这种方法称之为重载方法

下面关于操作符重载的具体实例(单目运算的自增及自减)

/*
 *operator.cpp
 *cDate : 2013-9-28
 *Author: sjin
 *Mail:413977243@qq.com
 */
#include <iostream>
using namespace std;

/*C++中的运算符
 * 基本格式:
 * 返回值类型  operator 操作符号(参数。。。)
 * C++运算符分为2类 :单目与双目运算符
 * */

class cDate {
private:
    int m_nDay; //范围 1-30(按照每月30天计算)
    int m_nMonth; // 1-12
    int m_nYear;
    void AddDays(int nDaysToadd);
    void AddMonths(int nMonthsToadd);
    void AddYears(int nYearToadd);
public:
    cDate(int nDay,int nMonth,int nYear)
        :m_nDay(nDay),m_nMonth(nMonth),m_nYear(nYear){cout << "cDate coreate..." << endl;};
    ~cDate() {cout << "cDate destory..." << endl;}

    /*操作符重载 operator */
    cDate& operator ++();
    cDate operator ++(int);
    void DisplayDate();
};

/*add days*/
void cDate::AddDays(int nDaysToadd)
{
    m_nDay += nDaysToadd;
    if(m_nDay > 30){
        AddMonths(m_nDay/30);
        m_nDay %= 30;
    }
}

/*Add Months*/
void cDate::AddMonths(int nMonthsToadd)
{
    m_nMonth += nMonthsToadd;
    if(m_nMonth > 12){
        AddYears(m_nMonth/12);
        m_nMonth %= 12;
    }
}

/*Add Years*/
void cDate::AddYears(int nYearToadd)
{
    m_nYear += nYearToadd;
}

/*++x 引用*/
cDate & cDate::operator ++()
{
    AddDays(1);
    return *this;
}

/*x++*/
cDate cDate::operator ++(int)
{
    cDate mdate(m_nDay,m_nMonth,m_nYear);
    AddDays(1);
    //mdate.DisplayDate();
    return mdate;
}

/*output*/
void cDate::DisplayDate()
{
    cout << m_nDay <<"/"<< m_nMonth << "/" << m_nYear<< endl;
}

int main()
{
    cDate mdate(25,6,2013);
    mdate.DisplayDate();
    
    cout << "**********前缀递增++mdate***********"<<endl;
    (++mdate).DisplayDate();/*相当于 mdate.operator++() y=++x;*/
    mdate.DisplayDate();

    cout << "**********后缀递增mdate++***********"<<endl;

    (mdate++).DisplayDate();/*相当于 mdate.operator++(int) y=x++;*/
    mdate.DisplayDate();
    return 0;
}

输出:

cDate coreate...
25/6/2013
**********前缀递增++mdate***********
26/6/2013
26/6/2013
**********后缀递增mdate++***********
cDate coreate...
26/6/2013
cDate destory...
27/6/2013
cDate destory...

解除引用运算符* 和成员选择运算符-〉

这两种运算符在智能指针类编程中使用最为广泛,下面就简单介绍下:
/*
 *auto_ptr.cpp
 *cDate : 2013-9-28
 *Author: sjin
 *Mail:413977243@qq.com
 */
#include <iostream>
#include <memory>
using namespace std;

/*解除引用运算符*及成员属性运算符-> */

class dog{
public:
    dog(){cout << "dog constructor.."<<endl;}
    ~dog() {cout << "dog destructor.."<<endl;}

    void Bark(){
        cout << " Bark Bark!..."<<endl;
    }
};

int main()
{
    /*相当于 int *int_ptr = new int;*/
    auto_ptr <int> int_ptr(new int);
    *int_ptr = 25;

    cout << "*int_ptr = " << *int_ptr << endl;

    /*相当于 dog * dog_ptr = new dog;*/
    auto_ptr <dog> dog_ptr(new dog);
    dog_ptr->Bark();
}



[jsh@localhost operation]$ ./a.out
*int_ptr = 25
dog constructor..
 Bark Bark!...
dog destructor..


上面的打印中我们并没有使用delete释放dog,但是在打印中有输出,这个工作有智能指针完成。