Summary of C + + Primer chap5-7

Source: Internet
Author: User
Tags class definition define local terminates

CHAP5:

The dangling else:else is matched with its nearest unmatched if, or it is made into a block with curly braces.

Switch (shape/character) {case shaping/character: Break;default:break;} It is best to use curly braces in case to define local variables.

Break: Used to terminate the block of the nearest switch/loop and come to the next sentence at the end of the block.

Continue: Terminates the block of the most recent switch/loop and starts the next loop.

Goto: First to have a label, Mark Goto here.

Throw: anomaly detection.

Try...catch ... : Exceptions thrown in a try statement block are usually handled by a catch clause, and try to find the appropriate type of catch clause by layer-by-step fallback, and if no matching catch clause is eventually found, the program goes to the standard library function named terminate. If a program does not have a try statement block and an exception occurs, the system invokes the Terminate function and terminates the execution P174 of the current program.

Exception header file: Stdexcept the class created object to initialize

CHAP6:

Local static object: initialized when the program's execution path passes through the object definition statement for the first time, and is not destroyed until the program terminates, Static.

The name of the formal parameter is often omitted from the declaration of the function, preferably declared in the header file, defined in the source file.

If the parameter is a reference type, it is bound to the corresponding argument, otherwise the value of the argument is copied to the parameter, and in C + + it is recommended that the parameter of the reference type be used instead of the pointer.

You can use reference parameters to save additional information to return multiple parameters.

When an argument is initially taxiing, the top-level const is ignored, that is, when the formal parameter has a top-level const, it is possible to pass it to a constant object or to a non-const object, but not to the actual argument, the parameter is not const.

You cannot pass a const object or literal value to a normal reference parameter, so try to use a constant reference as a formal parameter

The use of while (Beg!=end) {}.

Use the reference parameter of the array: void print (int (&arr) [10]);

function declaration for multidimensional arrays: void print (int (*matrix) [10]);

int main (int argc, char **argv)//ARGV is an array whose elements are pointers to C-style strings

To write a function that can handle different numbers of arguments, the new C++11 standard provides two main methods: if all arguments are of the same type, you can pass a standard library type named Initializer_list, and a special argument type (that is, an ellipsis) that you can use to pass a variable number of arguments. However, it is important to note that this functionality is typically used only for interface programs that interact with C functions.

The initializer_list<string> list is used as a formal parameter, {"", "", ""} as a formal parameter, and in the body of the function you can use iterators and scopes for.

Do not return a reference or pointer to a local object.

The return type of the function determines whether the function call is an lvalue, calls a function that returns a reference, gets an lvalue, and the other return type gets the right value.

Main () return value: Under the Cstdlib header file, exit_failure and exit_success are defined.

Recursion: if (val>1) return func (VAL-1) *val.

Tail return type: auto func (int t), int (*) [10].

Two functions are not allowed except for the return type, all other features are the same, assuming that two functions, their formal parameter list is the same but the return type is different, then the declaration of the second function is wrong.

In function overloading, the top-level const cannot be distinguished, and the underlying const can be distinguished.

Return Const_cast<>: Converts a non-const argument to a const return.

Function names cannot be overloaded in different scopes, and the external function declarations can be hidden locally.

Default argument for a function: When declaring a parameter, it is important to note that once a parameter is given a default value, all parameters following it must have a default value, and the default argument is responsible for filling the missing trailing arguments of the function call.

When designing a function with default arguments, one of the tasks is to properly set the order of formal parameters, and try to make formal parameters that do not use the default values appear in front, and those that often use the default values appear later.

A parameter can only be given a default argument in a given scope, that is, subsequent declarations can only add default arguments for parameters that do not have a default value, and all parameters to the right of the parameter must have a default value.

constexpr function: The return type of a function and the type of all formal parameters are literal types.

function pointers: You cannot directly use a function as a formal parameter, but a formal parameter can be a pointer to a function, and the pointer type must match exactly one of the overloaded functions, and the function name can be used as an argument.

You cannot return a function, but you can return a pointer to a function type.

Overloaded function match: P217.

CHAP7:

Interface: What the user can do; implementation: The data members of the class, the functions responsible for the interface implementation, and the various private functions required to define the class

This: When we invoke a member function, initialize this with the object address of the request, by default, the type of this is a constant pointer to the very version of the class type

Define member functions: Std::string ISBN () const {returnbookno;}

The compiler processes the class in two steps: the declaration of the member is compiled first and then the member function body. Therefore, the member function body is free to use other members of the class without caring about the order in which the members appear.

return *this; The return is a reference.

The default constructor for a composition: This class is appropriate only if the member is all assigned the initial value in the class.

Constructor: sales_data () = default;

Constructor initializer list: Sales_data (const std::string&s, unsigned n, double p): Bookno (s), Units_sold (n), Revenue (P*n) {};

The constructor is defined outside the class: Sales_data::sales_data (Std::istream&is) {read (is, *this);}.

Friend: If the class allows another class or function as its friend, make a friend declaration in the class: Friend Std::istream&read (,); it is a good idea to declare a friend in the position set before the start or end of the class. The friend function will appear three times before use: 1. Friend declaration 2. Within the header file declaration 3. Within the source file definition.

The member functions defined inside the class are automatically inline, preferably within the class declaration function is not inline, outside the class definition plus inline.

Mutable: Even within the const member function, it can be changed, mutable size_t ival;

Const-based overloading: P248.

Friend class: If a class specifies a friend class, the member functions of the friend class can access all members of this class, including non-public members, and there is no transitivity for the friend relationship.

Making the member function of another Class A friend function, you must explicitly indicate which class the member function belongs to, and each of the overloaded friend functions needs to be declared.

Even if we only invoke the function with members of the class that declares the friend, it must also be declared, that is, the friend declaration is not an ordinary declaration, and a common declaration is still to be made.

Outside of a class, operations on members of a class have fields that can be omitted within a class.

A class is defined in two steps: the declaration of a member is compiled first, and the function body is compiled until the class is all visible (only for the name used in the member function body, including the return type or the name in the parameter list must be visible before use).

Name lookup: 1. Before using the member function 2. All members within the Class 3. The scope before the function definition.

If a member is a const, a reference, or a class type that does not provide a default constructor, we must provide the initial values for those members through a list of constructor initializers.

If a constructor provides default arguments for all parameters, it actually defines the default constructor as well.

Static members: independent of a particular object, only related to the class itself, static can only appear once, generally not static members are initialized inside the class, and defined outside the class, at which time the definition cannot be static. If a static member is to be defined within a class, the constexpr:static constexpr int period=30, which is a literal constant type, must be required, and if this value is to be passed again, it needs to be defined again outside the class, without writing the initial value.

A static member can be the default argument.

Static member functions: You cannot make a const or use the this pointer within a static member function.

Summary of C + + Primer chap5-7

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.