Seventh Chapter
Notes
1. A function defined inside a class is an implicit inline function.
2. member functions that use const are called constant member functions (const member function).
3. First compile the declaration of the member and then turn to 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.
4. The IO classes belong to types that cannot be copied , so they can only be passed by reference.
5. Each class defines how its objects are initialized, and the class controls the initialization of its objects by one or several special member functions. These functions are called Constructors .
6. The default constructor does not require any arguments . If our class does not show any constructors defined, then the compiler implicitly defines a default function, called the composition's default constructor. The synthesized default constructor is only suitable for very simple classes.
7. In the new C++11 standard, if we need to synthesize the behavior of the default constructor, you can ask the compiler to generate the constructor by writing a =default after the argument list. If =default is inside a class, the default constructor is inline, and if it is outside the class, the member is not inline by default.
8. classes also need to control the behavior that occurs when copying, assigning, and destroying objects . If we do not actively define these operations, the compiler will synthesize them for us.
9. A class can allow other classes or functions to access its non-public members by making other classes or functions a friend of it.
10. A mutable data member (mutable data member) is never a const, that is, it is a member of a const object.
11. Declaring a class only temporarily does not define it, which is called a forward declaration (forward Declaraion), which is an incomplete type before it is declared.
12. When a name appears for the first time in a friend declaration , we implicitly assume that the name is visible in the current scope (that is, the name is used before the declaration).
13. As the constructor starts executing, the initialization is complete. Therefore, the only chance we have to initialize a const or reference type data member is through the constructor's initial value . The initialization order of members is consistent with the order in which they appear in the class definition.
14. If a constructor provides default parameters for all parameters, it actually defines the default constructor.
The new C + + standard extends the functionality of the constructor's initial values, allowing us to define the so-called delegate constructors .
classsales_data {pulic://the non-delegate constructor initializes the member with the corresponding argumentSales_data (std::strings, unsigned cnt,DoublePrice ): Bookno (s), Units_sold (CNT), Revenue (CNT*Price ) {}//The rest of the constructors are all delegated to another constructorSales_data (): Sales_data ("",0,0) {} sales_data (std::strings): Sales_data (s),0,0) {} sales_data (Std::istream& is): Sales_data () {Read ( is, * This); }//other members are the same as previous versions }; //execution Order: When the constructors of these delegates are finished executing, the contents of the istream& constructor body are then executed. //when a constructor is delegated to another constructor, the initial list of values for the constructor of the delegate and the body of the function are//execute in turn.
16. We associate the keyword static with the class by adding a key to it before the member's declaration. Static member functions are not bound to any objects, and they do not contain the this pointer. We can use scope operators to access static members directly (Account::rate ()), or you can use the object of a class to access static members.
17. We cannot initialize static members within the class. Instead, each static member must be defined and initialized outside of the class .
Summary of key points of knowledge
Aggregation class and Literal constant class (to be supplemented).
Implicit class-Type conversions
The C + + language defines several automatic conversion rules between built-in types. Similarly, we can define implicit conversion rules for classes. If the constructor accepts only one argument, it actually defines an implicit conversion mechanism for converting to such a type, and sometimes we call this constructor a conversion constructor (converting constructor).
string " 9-999-99999-9 " ; // constructs a temporary Sales_data object // The object's Units_sold and revenue equals 0,bookno equals Null_book Item.combine (Null_book); // because the combine parameter is a constant reference, we can pass a temporary amount to the parameter
Only one-step class type conversions are allowed.
// Error: Two user-defined conversions are required // (1) Convert "9-999-99999-9" to string // (2) and convert this (temporary) string to Sales_data item.combine (" 9-999-99999-9"); // Error
suppresses implicit conversions of constructor definitions. we can block by declaring the constructor as explicit. Also, the explicit keyword allows only the constructor declarations that appear within the class.
classSales_data { Public: Sales_data ()=default; Sales_data (ConstSTD::string&s, unsigned n,Doublep): Bookno (s), Units_sold (n), Revenue (P*N) {}ExplicitSales_data (ConstSTD::string&s): Bookno (s) {}ExplicitSales_data (std::istream&); }; Item.combine (Null_book); //Error: String constructor is explicitItem.combine (CIN);//Error: The IStream constructor is explicit
explicitly forces the conversion .
Item.combine (static_cast<sales_data>(CIN)); // correct: static_cast can use the explicit constructor
Terms
Data abstraction, Encapsulation (encapsulation), abstract data type (abstract datatype), member functions (member function), access specifiers (access specifiers), constructor initializer list (constructor initializer list), display constructor (explicit constructor), friend (friend), composition default constructor (synthesized defaults constructor).
Constructor initializer list: Describes the initial value of the data member of a class, first initializing the data member with the value from the initial value list before the constructor body executes. Uninitialized members will be initialized by default.
Explicit constructors: constructors that can be called with a single argument but cannot be used for implicit conversions.
2016-10-30 15:37:31
C + + Primer 5th notes: Seventh Chapter