C ++ object-oriented function operator overloading

Chapter XII Operator Overloading

1. C ++ may be referred to as operators operator function.

2. The operator function as member functions of a class, can be used as an ordinary C ++ functions. (Often as a friend function class)

3. When overload operators, have at least one operand is a user-defined class types (if there are the basic data types of basic data types pointer, already defined)

4. Overload operator function is not a static class member functions.

The rules to follow operator overloading:

(1) as a parameter reference type, plus possible const limit (to avoid the copy constructor, the number of arguments protection operation, while the operator has a function of constant operation)

(2) return a reference to the extent possible (to avoid constructing a temporary object value left to become the operational expression)

(3) if the first operand is not possible when the object of this class, as a friend must override function.

Examples: class object desired date Date d (2008,8,8); integer int n = 6; for addition operation. Operational expression d + n and n + different from the d-operator functions performed. The former can be a member function of a class, d as the first operand. The latter can be defined as a function of a friend, the integer n as the first operand.

6. Overload arithmetic operators (addition, subtraction)

Does not modify the operands, the calculation results are generally stored in variables or other objects in the then returns the value.

E.g:

Date operator + (const Date & d, int n) // friend function

{

       Date temp(d);

       int m=temp.DaysOfYear()+n;

       temp.setDate(d.year,m);

       return temp;

}

 

int Date :: operator- (const Date & d) const // const member functions

{

       return GetTotalDays()-GetTotalDays(d.year,d.month,d.day);

}

7. overloads of assignment operator (+ =, - =, * =, / =)

Right operand is not modified, the calculated results which is stored in the left operand (These operator functions can be friend function may be a function of a member) and should be designed to return a reference to the object itself (return * this)

Date & Date::operator+=(int n)

{

       *this=*this+n;

       return *this;  

}

8. Overload relational operators (greater than or less than or equal to equal to not equal to less than)

Boolean function is typically: bool Date :: operator> (const) {return * this-d> 0;}

 

--------------------------------- ------------- division Recess --------------------------

9. Overload operator O

(1) Output overload operator <<

This operator is a binary operator, the first operand is often cout, cout ostream class is an object, it is the first formal parameter and returns OUT osream & out, so that the output can be continuously

(2) Input Overload operator >>

The first operand operator is often cin, and may be continuous input

Code examples:

ostream &operator<<(ostream &out, const Data &d)

{

       out<<setfill('0')<<setw(4)<<d.year

       <<'/'<<setw(2)<<d.month

       <<'/'<<setw(2)<<d.day<<setfill(' ');

       return out;

}

 

istream &operator>>(istream &in,Data &d)

{

       int year,month,day;

       char str[100];

      

       in.getline(str,100,'/');

       if(in==NULL) return in;

       year=atoi(str);

      

       in.getline(str,100,'/');

       if(in==NULL) return in;

       month=atoi(str);

      

       in.getline(str,100,'\n');

       if(in==NULL) return in;

       day=atoi(str);

      

       d.setDate(year,month,day);

       return in;

}

 

注意返回in和out是实现连续的输入和输出的关键。

 

重载单目运算符(前后自增):

10.  重载前增(减)量运算符

先修改操作数的值然后以新的值参与运算。注意经常需要在前增或者前减运算符里面做的一件事就是需要判断一下数据有没有超过某一个值。(比如说时间里面的++minute,要是++之后的minute大于等于60,就需要往前hour进位,然后minute设置为原来的减去60)

11.  重载后增(减)量运算符

为了区别后增(减)量运算符,C++中在函数首部添加一个纯形式的参数int(这个参数只起到记号的作用),告诉编译器这是后增量、后减量的意思

后增和后减的特点是先以原来的值参与表达式的其他运算一次,然后修改操作数的值。

代码:

Date Date::operator++(int n)   //对象值返回,不宜做左值(?为啥)

{

              Date temp(*this);

              ++(*this);

              Return temp;

}

由于temp是一个局部自动变量,生命期随着函数返回而结束,所以上面两个后增运算符函数不能够采用引用返回

Guess you like

Origin www.cnblogs.com/joelovescoding/p/11829619.html