Tag:cout inline clu for me arc Keywords compare size one compile
Operator is a C + + keyword that is used in conjunction with an operator to represent an operator function that should be understood as a function name as a whole operator=. This is a method of the C + + extension operator function, although it looks strange, but it can be understood: on the one hand, the use of the operator is consistent with its original, on the other hand, extending its function only through the function of the way (C + +, "function" is implemented by the function )。 Why do I use operator overloading? For all operators of the system, in general, only the basic data type and the class provided in the standard library are supported, for the user-defined class, if you want to support basic operations such as comparing size, judging equality, and so on, you need to define a specific implementation of this operator. For example, judging whether two people are the same size, our default rules are based on their age to compare, so, in the design of the person this class, we need to consider the operator = =, and, according to the analysis just now, the basis of the comparison should be ages. So why is it called overloading? This is because, at the time of the compiler implementation, we have provided the basic data type implementation version of this operator, but now his operand becomes the user-defined data type class, so the user is required to provide the implementation of the parameter version. How do I declare an overloaded operator? A: The operator overload is declared in the class body as an operator of a class member function overload, except that his name contains the keyword operator, followed by a C + + pre-defined operator. You can declare a predefined = = operator in the following way: Class Person{private:int Age;public:person (int a) {this->age=a;} inline bool operator = = (Const person &PS) const;}; The implementation is as follows: inline bool person::operator== (const person &PS) Const{if (this->age==ps.age) return True;return false;} This is called as follows: #includeusing namespace Std;int main () {person P1 (Ten);p Erson P2, if (P1==P2) cout<< "The Age is equal!" < return 0;} Here, because OperAtor = = is a member function of class person, so object P1,P2 can call this function, the above if statement, the equivalent of P1 call function = =, the P2 as a parameter of the function to the function, so as to achieve a comparison of two objects. B: operator overloading is implemented as a non-class member function (global function) for the global overloaded operator, the argument that represents the left operand must be explicitly specified. Example: #include #includeusing namespace Std;class person{public:int age;public:}; bool operator== (Person const & P1, Person const & P2)//meet requirements, do operand type is displayed specifying {if (P1.age==p2.age) return True;return false;} int main () {person Rose;person jack;rose.age=18;jack.age=23;if (rose==jack) cout<< "OK" < return 0;} c: How do you decide to overload an operator with a class member function or a member of a global namespace? ① If an overloaded operator is a class member, the operator is called only if the left operand that is used with him is an object of that class. If the left operand of the operator must be a different type, the operator must be overloaded to a member of the global namespace. ②c++ requires assignment =, subscript [], call (), and member-to-operator must be defined as a class member operator. Any definition of these operators as namespace members will be marked as compile-time errors. ③ If an operand is a class type such as a string class, it is best to define a global namespace member for a symmetric operator such as the Equals operator. Example: #include <iostream> Using namespace Std;class a{public: a (double _data = 0.0):d ata (_data) {} A& operator = (const a& RHS) { data = Rhs.data; return *this; } friend A operator + (const a& lhs,const a& RHS); & Nbsp; friend a operator-(const a& lhs,const a& RHS); friend a operator * (const A&AM P Lhs,const a& RHS) friend A operator + (const a& lhs,double RHS); friend a operator + (double lhs,const a& RHS); friend a operator * (const a& Lhs,doub Le RHS); friend a operator * (double lhs,const a& RHS); friend A Operator-(const a& lhs,double RHS); friendA operator-(double lhs,const a& RHS); friend ostream& operator << (ostream& fout,a& A);// A& operator + = (Const a& r HS);// A& operator-= (const a& RHS);// A& operator *= (const a& RHS); private: double data;}; a operator + (const a& lhs,const a& RHS) { a res (0); Res.data = Lhs.data + Rhs.data; return res;} A operator-(const a& lhs,const a& RHS) { a res (0); res.data = LHS . Data-rhs.data; return Res;} A operator * (const a& lhs,const a& RHS) { a res (0); res.data = LHS . Data * Rhs.data; return Res;} a operator + (const a& lhs,double RHS) { a Res (0); res.data = Lhs.data + Rhs; return res;} a operator + (double lhs,const a& RHS) { a res (0); res.data = LHS + Rhs.data; return Res;} A operator * (const a& lhs,double RHS) { a res (0); res.data = Lhs.data * Rhs; return Res;} A operator * (double lhs,const a& RHS) { a res (0); res.data = LHS * RHS. Data; return Res;} A operator-(const a& lhs,double RHS) { a res (0); res.data = Lhs.data -Rhs; return res; }a operator-(double lhs,const a& rhs) { a Res (0); res.data = Lhs-rhs.data; return res; } ostream& operator << (ostream& Fout,a& A) { fout << a.data; return fout;} int main (int argc, char* argv[]) { a A (2.3); a B (1.2); a d (3.4); a c; c = A + B + d; c=a+b;& nbsp; c=a+1.0; c=a-b; c=a-1.0; c=a*b; c=a*1.0; cout << C << endl; c=1.0+2.0*a*a-3.0*a*b; cout << C << Endl; return 0;} http://blog.sina.com.cn/s/blog_4b3c1f950100kker.htmlhttp://www.cnblogs.com/speedmancs/archive/2011/06/09 /2076873.html
C + + operator keyword (overloaded operator)