Book Note: C ++ primer 4th (ch15-18)

Source: Internet
Author: User

Chapter 2 Object-Oriented Programming

Three basic concepts in object-oriented model: data abstraction, inheritance, and dynamic binding.

In C ++, classes are used for data abstraction. Classes are derived from one class to inherit from another class. Dynamic binding enables the compiler to determine whether to use the functions defined in the base class or the functions defined in the derived class at runtime.

1. Polymorphism

The key idea of Object-oriented thinking is polymorphism. In C ++, polymorphism is only used to reference or pointer to the type associated by inheritance.


2. Inheritance

In C ++, the base class must specify which functions you want the derived class to redefine. A function defined as virtual is the base class that is expected to be redefined by the derived class. The functions that the base class expects to inherit from the derived class cannot be defined as virtual functions.

In addition to constructors, any non-static member function can be a virtual function. In addition, the reserved word 'ural' only appears in the member function declaration within the class, and cannot be used in the function definition that appears outside the class definition body.

Public: any external classes and functions can be accessed.

PRIVATE: only the class itself and its friends can access it. Its subclass does not work either.

Protected: it cannot be accessed by class users, but can be accessed by Derived classes.

Note that a derived class can only access the protected member of its base class through the derived class object. The derived class does not have special access permissions to the protected member of its base class object.



3. Derived classes and virtual functions

The Declaration of the virtual function in the derived class must exactly match the definition method in the base class, but there is an exception: the virtual function that returns a reference (or pointer) to the base class. The virtual function in the derived class can return the reference (or pointer) of the derived class of the type returned by the base class function)







Youyuan relationship and inheritance

The membership relationship cannot be inherited. The membership of the base class does not have special access permissions to the members of the derived class. If the base class is granted a friend relationship, only the base class has special access permissions. The derived class of the base class cannot access the class that grants the friend relationship.

Constructor and replication Control




1. Class scope under Inheritance

In the case of inheritance, the scope of the derived class is nested in the scope of the base class. If the name cannot be determined in the scope of the derived class, search for the definition of the name in the scope of the peripheral base class.

2. Name Search occurs during compilation
The static types of objects, references, or pointers determine the actions that an object can perform. Even when the static type and the dynamic type may be different, just as the reference or pointer of the base class type may occur, the static type still determines what Members can be used.
The dynamic type is the runtime type. The pointer and reference of the base class type can be bound to an object of the derived type. In this case, the static type is a base class reference (or pointer), and the dynamic type is a derived class reference or pointer.
Static type is the compile-time type. The static and dynamic types of objects are the same. The dynamic types of objects referenced by a reference or pointer can be different from those of a reference or pointer.


3. A name conflict with a derived class member that inherits the same name as the base class member will block direct access to the base class member. However, we can use the scope operator class to access blocked members.
4. To obtain dynamic binding between virtual functions and scopes, virtual members must be called through reference of the base class or pointer members. When we do this, the compiler will look for functions in the base class. If the name is found, the compiler checks whether the real parameter matches the form parameter. Now we can understand why the virtual function must have the same prototype in the base class and the derived class. If the base class member and the derived class member accept different arguments, there is no way to call the derived class function through the base class type reference or pointer. Note: The exception here is that the return type can be slightly different. Also, const is part of the function declaration, so the const functions with the same name (the same parameter list) and non-const functions are different.

If the subclass defines a function with the same virtual function name as the base class but with different parameter lists, this function will block the virtual function in the base class, although the subclass inherits the virtual function from the base class, it cannot call the virtual function through the subclass object because the virtual function has been blocked. For example:

Base bobj; D1 d1obj; d2 d2obj;
Base * bp1 = & bobj, * bp2 = & d1obj, * bp3 = & d2obj;
Bp1-> FCN (); // OK: virtual call, will call base: FCN at run time
Bp2-> FCN (); // OK: virtual call, will call base: FCN at run time
Bp3-> FCN (); // OK: virtual call, will call D2: FCN at run time

A pure virtual function contains (or inherits) one or more classes of pure virtual functions. In addition to being part of an abstract base class derived class object, you cannot create an abstract type object. The pointer or reference of an abstract class can be declared.

5. There are not many containers and inherited objects.
Multiset <item_base> basket;
Item_base base;
Bulk_item bulk;
Basket. insert (base); // OK: Add copy of base to basket
Basket. insert (bulk); // OK: But bulk sliced down to its base part
The code above shows that when adding an object of the derived class, the basic class of the object is saved in the container. This is equivalent to copying a derived class object to a base class object. During the copying process, the derived class object will be cut out. You cannot convert a base class object to a derived type. If such a standard conversion does not exist, you can forcibly convert a base class object to a derived class object, however, this will cause some members of the derived class to be uninitialized. You can use the container to save the object pointer, but in this case, you must process the object and pointer. (The pointer is bound dynamically.)

In C ++, the irony of object-oriented programming is that objects cannot be used to support object-oriented programming. Instead, pointers or references must be used.

Chapter 2 templates and generic programming
[1]


Generic programming is to write code independently of any specific type.
The template definition starts with the keyword template, followed by the template parameter table. The template parameter table is a list of one or more template parameters enclosed by Angle brackets. The parameters are separated by commas.
The template parameter table cannot be empty.
1. template parameters
A template parameter can be a type parameter or a non-type parameter that represents a constant expression.
The type parameter is defined after the keyword class or typename. For example, class T is a type parameter named T. Here, class and typename are no different.
2. Use the function Template
When using a function template, the compiler determines which (or which) template real parameters are bound to the template parameters. Once the compiler determines the actual template parameters, it is called an instance of the function template. In essence, the compiler will determine what type is used to replace each type parameter, and what value is used to replace each non-type parameter. After the actual template parameters are exported, the compiler uses the real parameters instead of the corresponding template parameters to generate functions for compiling this version. The compiler undertakes the monotonous work of writing functions for each type we use.
[2]

There is a difference between typename and class
In standard C ++, there is no difference between the type parameters declared as typename and the type parameters declared as class. However, the system before Standard C ++ may only support using the keyword class to declare the template type parameters.
If you want to use a type member defined in the class within the function template, you must add the keyword typename before the member name to inform the compiler to treat the member as a type. (Because it is treated as a type member by default)
[3]


Non-type template parameters
[4]


Compile generic programs
[5]


Template instantiation
The process for the compiler to generate a template of a specific type is called Instantiation.
The template will be instantiated during use. The class template will be instantiated When referencing the actual template class type. The function template will be instantiated when calling it or using it to initialize or assign values to the function pointer.
Each instantiation of a class template produces an independent class type.
When using a function template, the compiler usually deduce the real parameters of the template for us.
Template real parameter Inference
The template type parameter can be used as the type of more than one function parameter. In this case, the template type inference must generate the same template real parameter type for each corresponding function real parameter. If the inferred type does not match, the call will fail:
[6]


The type conversion restrictions only apply to those parameters whose types are template parameters. For common type-defined parameters, you can use regular conversion.
[7]

Explicit parameters of the function Template
[8]


Template compilation Model
[9]

Non-type parameter template parameters
The real parameter of a non-type template must be a regular expression for compiling:
Template <int hi, int WID>
Class Screen {
Public:
// Template nontype parameters used to initialize data members
Screen (): screen (Hi * WID, '#'), cursor (0 ),
Height (HI), width (WID ){}
//...
PRIVATE:
STD: string screen;
STD: String: size_type cursor;
STD: String: size_type height, width;
};
Screen <24, 80> hp2621; // screen 24 lines by 80 characters
[10]

[11]


[12]


Static member of the class template
Each class instantiation has its own static member, because each instantiation represents a completely different type.
Like any other static data member, the data member definition must appear outside the class. When a class template contains a static member, the member definition must indicate that it is a member of the class template:

Template <class T>
Size_t Foo <t >:: CTR = 0; // define and initialize CTR

Template Specialization

Function template Specialization
[13]


Like any function, function templates can be customized without being defined. The template special declaration looks like the definition, but the function body is omitted:
// Declaration of function template explicit Specialization
Template <>
Int compare <const char *> (const char * const &,
Const char * const &);
If you can infer the template parameters from the function parameter table, you do not need to explicitly specify the template parameters:

// Error: Invalid specialization declarations
// Missing Template <>
Int compare <const char *> (const char * const &,
Const char * const &);
[14]


// Error: function parameter list missing
Template <> int compare <const char *>;

// OK: explicit template argument const char * deduced from parameter types
Template <> int compare (const char * const &,
Const char * const &);

Class template Specialization
Class template specialization should be the same interface as the template definition that it is specific to. Otherwise, users may be surprised when trying to use undefined members.
When a class-specific external definition member is added, the member cannot be marked with template <>.
Partial specialization of class templates
If the class template has more than one template parameter, we may want to specify some template parameters rather than all. Some features of the class template can be used to achieve this:

Template <class T1, class T2>
Class some_template {
//...
};
// Partial specialization: fixes T2 as int and allows T1 to vary
Template <class T1>
Class some_template <t1, int> {
//...
};
Some of the class templates are also specific templates. Some special definitions appear like template definitions. These definitions start with the keyword template, followed by the template parameter table enclosed by Angle brackets (<>. Some special template parameter tables are the subsets of the corresponding class template definition parameter tables.

Chapter 1 tools for large programs
1. namespace
Namespace pollution
Like other names, the namespace name must be unique in the scope that defines the namespace. A namespace can be defined within a global scope or other scope, but not within a function or class.
The namespace scope cannot end with a semicolon.



Multi-Inheritance





Virtual inheritance

Chapter 4 special tools and technologies
Optimized Memory Allocation


Class member pointer
The member pointer contains the class type and the member type.
The member pointer is only applicable to non-static members of the class. Static class members are not part of any object, so special syntax is not required to point to static members. static member pointers are common pointers.
(1) data member pointer



Union in C ++
To be compatible with C, C ++ also has union. In C ++, union is a special class.
A union object can have multiple data members, but at any time, only one member can have a value. When a value is assigned to a member of the Union object, all others become undefined.
Union is a non-static data member, referenced member, or class data member.
Some (but not all) class features also apply to union. For example, like any class, union can specify protection tags to make members public, private, or protected. By default, Union performs like struct: Unless otherwise specified, the union members are all public members.
Union can also define member functions, including constructor and destructor. However, union cannot be used as a base class, so the member function cannot be a virtual number.
Union cannot have static data members or reference members, and Union cannot have class members that define constructors, destructor, or assignment operators:

Union illegal_members {
Screen s; // error: Has Constructor
Static Int Is; // error: static member
Int & RFI; // error: Reference Member
Screen * pS; // OK: ordinary built-in pointer type
};
This restriction includes classes with constructors, destructor, or assignment operators.

Nested class
Local class

Non-portable features inherited from C language in C ++: bitfield and volatile qualifier
The bit field must be of the integer data type, either signed or unsigned. After a member name is followed by a colon and a constant expression of the specified number of digits, it is pointed out that the member is a single-digit field.

The exact meaning of volatile is machine-related and can only be understood by reading the compiler documentation. Volatile programs must be changed when moved to a new machine or compiler.
In the same way as defining a const member function, a class can also define a member function as volatile. A volatile object can only call the volatile member function.

The merging replication control is not applicable to volatile objects.
One important difference between const and volatile is that you cannot use the composite copy and value assignment operators to initialize or assign values to volatile objects. The merged copy control Member accepts the const parameter, which is a const reference to the class type. However, the volatile object cannot be passed to a common reference or const reference.

Link instructions in C ++





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.