C + + Primer Chinese version of Learning notes (14) __c++

Source: Internet
Author: User
Tags class definition function definition

15th Chapter Object-oriented programming

1 object-oriented programming is based on three basic concepts: Data abstraction, inheritance, and dynamic binding.

2 inheritance levels share common things, only special things that are essentially different.

3 a function defined as virtual is a base class that expects a derived class to redefine, and the base class expects that a function inherited by a derived class cannot be defined as a virtual function.

4 in C + +, dynamic binding occurs when a virtual function is invoked through a reference (or pointer) to a base class. The fact that a reference (or pointer) can point to either a base class object or a derived class object is the key to dynamic binding. A virtual function called with a reference (or pointer) is determined at run time, as defined by the actual type of the object referred to (or pointer) when the called function is invoked.

5 other functions can be defined as virtual functions except constructors and static member functions. Reserved word virtual appears only in the declaration of a member function inside a class and cannot be used on a function definition that appears outside the class definition body.

6 Another important property of protected:

Derived classes can access protected members of their base classes only through derived class objects, and derived classes do not have special access to protected members of their base class type objects.

7 The declaration of a virtual function in a derived class must exactly match the method defined in the base class, with one exception: a virtual function in a derived class can return a reference (or pointer) to a PEI of the type returned by the base class function.

Item_base class can define virtual functions that return item_base*

An instance defined in the Bulk_item class can be defined to return item_base* or bulk_item*.

8 to trigger a dynamic binding, you must meet two conditions:

A) First, only member functions that are specified as virtual functions can be dynamically bound, the member function defaults to non-virtual functions, and non-virtual functions are not dynamically bound.

b) Second, a function call must be made by accumulating a reference or pointer to the type.

9 The static type of references and pointers can differ from the dynamic type of the object being bound, which is the cornerstone of C + + to support polymorphism.

10 when a derived class virtual function calls the base class version, the scope operator must be explicitly used. If the derived class function ignores doing so, the function call is determined at run time and will be a call to itself, resulting in infinite recursion.

11 A virtual function can have a default argument, and if a call omits an argument with a default value, the value used is defined by the type that calls the function, regardless of the object's dynamic type. Using different default arguments in the base class version and derived class version of the same virtual function can almost certainly cause trouble.

12 derived classes can further restrict, but not relax, access to inherited members.

13 access to a name in the base class by using a declaration.

14 friend relationship cannot be inherited.

The difference between a function (object & a) and a function (object a) object when an argument is invoked:

A one is to convert a derived class object to a base class type reference.

b One is to initialize or assign the base class object with the derived class object.

16 The compiler determines whether the conversion is legitimate, looking only at the static type of the pointer or reference.

Bulk_item Bulk;

Item_base *itemp = &bulk;

Bulk_base *BULKP = itemp; Error:can ' Tconvert base to DriveD

17 The constructor of a derived class initializes the base class part and initializes its own special part.

The initialization list of derived class constructors can only initialize members of derived classes and cannot initialize inherited members directly. Instead, the derived class constructor indirectly initializes an inherited member by including the base class in the constructor initialization list.

Bulk_item (const string&book,double salses_price,size_t qty = 0,double Disc_rate = 0.0):

Item_base (Book,salse_price), Min_qty (qty), Discount (disc_rate) {}

18 classes that contain only class types or built-in type data members, and those that do not contain pointers, can generally use compositing operations, copying, assigning, or revoking such members do not require special control. Classes with pointer members generally need to define their own replication controls to manage these members.

19 if the derived class explicitly defines its own copy constructor or assignment operator, the definition will completely override the default definition. The copy constructors and assignment operators of the inherited class are responsible for copying and assigning the base class composition and the members of the class themselves.

20 The static type of the pointer differs from the dynamic type of the deleted object, so a virtual destructor is required.

21 constructors and assignment operators are not virtual functions:

A The constructor is run before the object is fully constructed, and the dynamic type of the object is incomplete when the constructor is run.

b Subscript character the assignment operation of a class to a virtual function can be confusing and not useful.

22 If you call a virtual function in a constructor or destructor, you are running a version that is defined for the constructor or destructor's own type.

Explanation: When constructing a derived class object, the base class constructor is run first to initialize the base class part, at which point the object is not a derived class object. So does the same destructor.

23 if a derived class wants to use all overloaded versions through its own type, the derived class must either redefine all overloaded versions or not one. Sometimes a class needs to simply redefine the behavior of certain versions of an overloaded set and want to inherit the meaning of other versions.

Using declaration

24 The key to understanding the inheritance hierarchy in C + + is to understand how to determine a function call:

A determines the static type of the function call object

b) Look up the name, upstream.

c) General type checking

d) whether dynamic binding

25 classes that contain pure virtual functions are abstract classes and cannot create objects of an abstract type except as part of an object that is a derived class of an abstract class.

261 containers that hold the base class object, and truncation occurs when the derived class object is added. Workaround:

A to save the object pointer, but the user needs to face the problem of managing objects and pointers.

b The handle class (the user obtains dynamic behavior but does not need to worry about the pointer management, the intelligent pointer is an example of the handle class).

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.