C ++ day12 Study Notes

Source: Internet
Author: User

1. copy constructors and Operator Overloading
(1) When the pointer type appears in the member variables of the class, the space needs to be dynamically applied. In this way, the problem of shallow copy needs to be solved.
When an object is declared and assigned with another object, the copy constructor is called.
The default copy constructor provided by the system is a shortest copy function. We can write a copy constructor to copy the variable pointed to by the pointer.
(2) The member variables in the class have the pointer type. When both objects are created and assigned values to each other, You need to overload the value assignment operator number.
Manually assign values to the variables pointed to by the pointer

2. overload of other operators
Addition and subtraction between objects is not allowed by the system, but operations between objects are implemented based on their own rules by using their own operators.

 
IntegerOperator+ (ConstInteger &I ){IntT = * P + *(I. p); integer temp (t );ReturnTemp ;}

(1) Auto increment operator
The front ++ is the left value and returns a reference.
The plus ++ is the right value and returns a temporary value.

The "++" operator has a higher priority than the "+" operator.

Integer & operator ++ (){}
Integer operator ++ (int I ){}
Int I is used to distinguish between front ++ and back ++. It is a parameter of no practical significance. It is called a dummy element and must be of the int type.

The main difference between the operations of the front ++ and back ++ is that the returned values are different and the variable is added to 1 internally.
Before ++, (++ I) is added first and then used. The variable value after 1 is returned. You can add 1 to the variable and then return the result. All variables can return the reference directly.
After ++ and (I ++), first add the variable and return the variable value before adding 1, that is, to return the original old value,
In this case, you need to create an object inside the function of the overload operator to save the old value, and then add 1 to return the old value itself.

(2) Heavy Load "="
Assign a value to an integer object of the int type.

Integer &Operator= (IntI ){//The value assignment operation changes the int value in the object and returns itself. Therefore, the return value is a reference.* P = I;//Manually assign the int type value to the objectReturn*This;}

(3) Operator Overloading
It can be implemented not only by class member functions, but also by common functions.
Implemented using a member function. There is only one parameter. The operator is itself on the left and a1.operator = (A2) on the right );
Implemented using a common function. Two parameters are required. The first parameter is the value on the left side of the operator, and the second parameter is the value on the right side of the operator. this parameter is used as a member function overload.
Operator (a1, a2 );

(4) Recommendation principles
All unary operators --- member overloading "=", "[]" only supports Member Overloading
Binary operator-friend overload

When the integer object type and INT type are added,
Implement 5 + I and I + 5
It can be reloaded twice by friends

 
Friend integerOperator+ (ConstInteger & I,IntA); friend integerOperator+ (IntA,ConstInteger & I );//Member function declaration in class

(5) Forced type conversion Operator
Operator int () {...} // declare a strongly converted int type

3. Stream Operator Overloading
(1) the output stream operator can only be reloaded by the user.

Friend ostream &Operator<(Ostream & O, integer & I );//Shengmingyou FunctionOstream &Operator<(Ostream & O, integer & I ){//ImplementationO <* (I. p );//* P in the output object, that is, the variable pointed to by the pointerReturnO ;}

Cout <I; <=> operator (cout, I );

(2) The input operator can also be overloaded to implement object reading. It can only overload the user function.

 friend istream &  operator  (istream &  in , integer & I); ///   listener functions  istream &  operator  (istream &  in , integer & I) {///   Implementation   in  * (I. p); ///   put the read data in the variable pointed to by * P   return   in   ;}  

(3) Why can I only use friends for heavy load?
Because the location of the CIN cout is fixed, CIN> I; cout <I;
In this way, the object itself cannot be used to call the overloaded stream operator I. Operator (cout)-> This is incorrect.
You can only enable the UFIDA to reload the object and cout as parameters, so that the operation can be performed.

Exercise: account class, directly output the account

View code

 Account A1; cout <A1 < Endl; friend ostream & Operator <(Ostream & O, account & A); ostream & Operator <(Ostream & O, account & A) {o < "  Name:  " <A. Name < Endl; o < "  Password:  " <A. Password < Endl; o < "  ID:  " <A. ID <Endl; o < "  Balance:  " < A. balance;  Return  O;} friend istream & Operator >>( Istream & In , Account & A); istream & Operator >>( Istream & In , Account &A) {cout < "  Enter your name>  "  ;  In > A. Name; cout < "  Enter your password>  "  ;  In > A. Password; cout <"  Enter your ID>  "  ;  In > A. ID; cout < "  Enter your balance>  "  ;  In > A. balance;  Return   In  ;} 

Job:
Write a class named "RMB"
RMB {
Int * yuan;
Int * jiao;
Int * fen;

RMB ();
RMB (INT y, Int J, int F );
RMB (const RMB & R );
~ RMB ();
Operator =
Friend ostream & operator <(ostream & O, RMB & R );
Friend istream & operator> (istream & in, RMB & R );
Addition and subtraction between RMB and INT Multiplication
Operator double () {}// forced conversion operation
Operator float (){}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.