C + + Primer Plus reading notes--11th use class

Source: Internet
Author: User

The 11th chapter uses the class

1. Operator overloading is a form of C + + polymorphism.

2. Do not return a reference to a local variable or temporary object. When the function finishes executing, the local variables and temporary objects disappear, and the reference points to the nonexistent data.

3. The format of operator overloading is as follows:

Operatorop (argument-list);

You can use function notation or operator notation to work with it.

Total = coding + fixing;

Total = coding.operator+ (fixing);

4. Limitations of operator Overloading:

1) An overloaded operator must have at least one operand that is a user-defined type, which prevents the user from overloading the operator for the standard type.

2) When using operators, the original syntactic rules of the operator cannot be violated. For example, the operand number cannot change, and the operator's precedence cannot be modified.

3) You cannot create a new operator. For example, you cannot define the operaor** () function to exponentiation.

4) The following operator cannot be overloaded.

sizeof

. (Member operator)

. * (Member pointer operator)

::

? :

typeID (a rtti operator)

const_cast Coercion type conversion operators

dynamic_cast Coercion type conversion operators

reinterpret_cast Coercion type conversion operators

static_cast Coercion type conversion operators

5) Most operators can be overloaded with members or non-member functions, but the following operators are overloaded only by member functions.

= Assignment operator

() function operator

[] subscript operator

Operators for accessing class members through pointers

5. There are three types of friends:

Friend function

Friend class

Friend member function

    1. 6. A friend function, although declared in a class, is not a member function of a class and therefore cannot be invoked using the member operator. A friend function is not a member function of a class, but it has the same access rights as a member function.

7. Create Friend

1) Place the prototype in the class declaration and add the keyword friend before the prototype declaration

Friend Time operator* (double m, const time & T);

2) write the function definition. Because it is not a member function, do not use the time:: qualifier. Also, do not use the keyword friend in the definition.

Time operator* (double m, const time &t)

{

...

}

8. Only class declarations can determine which function is friend, so the class declaration still controls which functions can access private data. In short, class methods and friends are just two different mechanisms for expressing class interfaces.

9. Class inheritance properties enable Ostream references to point to Ostream objects and Ofstream objects.

10. In general, to overload the << operator to display C_name objects, you can use a friend function, which is declared as follows:

Friend Ostream & operator<< (ostream & OS, const c_name & obj);

11. Overloaded operator: As a member function or a friend function (non-member function)

For some operators, the member function is the only valid choice. In other cases, the two formats are not much different. Sometimes, depending on the class design, it may be better to use a non-member function version, especially when defining a type conversion for a class.

12. When you want to return a custom class object, you can return one of its constructors. Using constructors to generate a new object can avoid trouble.

13. A friend function is defined in a class declaration, although there is no inline keyword, but it will be an inline friend function.

14. Random Number

The header file Cstdlib (stdlib.h) contains the prototypes of Srand () and Rand (), while CTime (Time.h) contains the prototype of time (). C + + uses the header file function in the random to provide more robust random number support.

15. In C + +, the constructor that accepts a parameter provides a blueprint for converting a value of the same type as the parameter to a class, and supports implicit conversions.

STONEWT (double lbs);

STONEWT Mycat;

Mycat = 19.6;

Only constructors that accept one parameter can act as a conversion function. The following constructor has two parameters and cannot be used for conversion.

STONEWT (int stn, double lbs);

However, if the second parameter provides a default value, it can be used to convert an int:

STONEWT (int stn, double lbs = 10);

16. When does the compiler use the STONEWT (double) function for implicit type conversions?

1) When the Stonewt object is initialized to a double value.

2) When assigning a double value to a Stonewt object.

3) When you pass a double value to a function that accepts the STONEWT parameter.

4) The return value is declared as STONEWT when the function tries to return a double value.

5) in either case, when using a built-in type that can be converted to a double type.

STONEWT Jumbo (7000);

Jumboo = 7300;

17. Implicit type conversions can sometimes lead to unexpected disasters, and C + + has a new keyword explicit to turn off this feature.

Explicit STONEWT (double lbs);

This closes the implicit conversion in the example above, while still allowing the conversion to be displayed.

STONEWT Mycat;

Mycat = 19.6; Not valid

Mycat = STONEWT (19.6);//OK

Mycat = (STONEWT) 19.6;//OK

18. Conversion Functions

Constructors are only used for conversions from a type to a class type. To perform the opposite conversion, you must use a transform function. P415

After you define a transform function, you can either use the cast or allow the compiler to automatically make an implicit conversion.

Stone Wolfe (285.7);

Double host = double (Wolfe);

Double thinker = (double) Wolfe;

Double star = Wolfe;

To convert to the typename type, you need to use this form of conversion function:

    • operator TypeName ();

Please note the following points:

1) The conversion function must be a class method;

2) The conversion function cannot specify a return type (TypeName is already given);

3) The conversion function cannot have parameters (already implicitly provided);

For example, the prototype converted to a double type is as follows: operator double ();

19. Implicit conversion functions should be used sparingly. In principle, it is preferable to use an explicit conversion function to avoid implicit conversion functions. In c++98, the keyword explicit cannot be used for conversion functions, but c++11 eliminates this limitation. Therefore, in c++11, the conversion operator can be declared explicit.

For example:

explicit operator int () const;

C + + Primer Plus reading notes-11th chapter using classes

Related Article

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.