"C + + Primer Plus" Reading notes 11-Class inheritance

Source: Internet
Author: User

Chapter 13th Class Succession

1. class inheritance : Extend and modify classes.

2 . Public inheritance format: The colon indicates that the base class of Class B is a, a, or a derived class.

Class B:public A

{

。。。

};

3 . The derived class object contains the base class object. with public derivation, the public member of the base class becomes a public member of the derived class, and the private part of the base class becomes part of the derived class, but is accessible only through the public and protected methods of the base class. The derived class object stores the data members of the base class (the derived class inherits the implementation of the base class), and the derived class object can use the method of the base class (the derived class inherits the interface of the base class).

4. What do I need to add to the inheritance feature? first, the derived class needs its own constructor. The derived class can then add additional data members and member functions as needed.

5. The constructor of the derived class must provide data to the new member (if any) and to the inherited member. However, derived classes cannot directly access the private members of the base class, but must be accessed through the methods of the base class . Specifically, the derived class constructor must use the base class constructor.

6. When creating a derived class object, the program first creates the base class object. This means that the base class object should be created before the program enters the derived class constructor. C + + uses the member initialization list syntax to do this work. For example, here is a constructor for the derived class B : Where A (FN,LN,HT) is a member initialization list.

b::b (int r,const char *fn,const char *ln,bool HT): A (FN,LN,HT)//(FN,LN,HT) is a base class private data member

{

Rating =r; Rating is a derived class member

}

  What happens if the member initialization list is omitted ?

b::b (int r,const char *fn,const char *ln,bool HT)//(FN,LN,HT) is a base class private data member

{

Rating =r; Rating is a derived class member

}

The base class object must first be created, and if the base class constructor is not called, the program will use the default base class constructor , so the upper denomination code is equivalent to the following:

b::b (int r,const char *fn,const char *ln,bool HT): A ()//(FN,LN,HT) is a base class private data member

{

Rating =r; Rating is a derived class member

}

  Note: Unless you want to use the default constructor, you should explicitly call the correct base class constructor.

7. See the constructor for the second derived class B: because the type of TP is const A &, the copy constructor of the base class is called. If you need to use a copy constructor without a definition, the compiler will automatically generate one.

B::B (int r,const A & TP): A (TP)

{

Rating =r; Rating is a derived class member

}

8 . If you prefer, you can also use member initialization list syntax for members of a derived class.

  9. Remember: when you create a derived class object, the program first calls the base class constructor and then calls the derived class constructor. The base class constructor is responsible for initializing the inherited data members; The derived class constructor is primarily used to initialize new data members. The constructor of a derived class always calls a constructor of a base class. You can use the member initialization list to indicate which base class constructor to use, otherwise the default base class constructor is used. When a derived class object expires, the program calls the derived class destructor first, and then calls the base class destructor.

10. special relationships between derived and base classes : First, the derived class object can use the method of the base class (in the same way that the base class object calls the method), provided the method is not private. A base-class pointer can point to a derived class object without an explicit type conversion, and a base class reference can reference a derived class object without an explicit type conversion. However, a base class pointer or reference can only be used to call a base class method. Note: You cannot assign base class objects and addresses to derived class references and pointers.

   One , for a function, if its formal parameter is a base class reference, you can use a base class object or a derived class object as an argument.

For a function, if its formal parameter is a pointer to a base class, you can use the address of the base class object or the address of the derived class object as the argument.

12. reference compatibility properties can also initialize a base class object to a derived class object, albeit less straightforward . As follows:

B b= (1840, "abc", "Def", true);//Derived class object

A (b); Initializing a base class object with a derived class object

To initialize a, the matching constructor prototype is: A (const B &), there is no such constructor in the class, but there is an implicit copy constructor : A (const A &); formal parameter is a base class reference, so it can refer to derived classes. The implicit constructor initializes a to the A object nested in B object B.

13. Similarly, you can assign a derived class object to a base class object :

  B b= (1840, "abc", "Def", true);//Derived class object

A;

a=b;//assigning to base class objects with derived class objects

in this case, the program uses an implicitly overloaded assignment operator :

A & operator= (const A &) const; The base class reference is also a derived class object, so the base class part of B is copied to A.

14.C + + has 3 ways of inheriting : public inheritance, protection inheritance and private inheritance. Public inheritance is most commonly used to establish a is-a relationship in which a derived class object is also a base class object that can perform any operation on a base class object, or it can be performed on a derived class object.

15. The behavior of the class method should depend on the object that called the method. This more complex behavior is called polymorphism--with multiple forms, that is, the behavior of the same method will vary from one context to the next. There are two important mechanisms that can be used to implement polymorphic public inheritance : ① The method of redefining a base class in a derived class. ② uses virtual methods.

16. For a method that behaves the same in two classes, it is declared only in the base class.

17. Base class methods If you do not use the keyword virtual, the program chooses the method based on the reference type or pointer type, and if you use virtual, the program chooses the method based on the type of object that the reference or pointer points to.

18. The method that the derived class will redefine is often declared as a virtual method in the base class. When a method is declared as virtual in a base class, it automatically becomes a virtual method in the derived class.

19. In a derived class method, the standard technique is to call the base class method using the scope resolution operator.

20, you can create an array of pointers to the base class, then the pointer in the array can either point to a base class object or to a derived class object, so you can use an array to represent multiple types of objects, that is polymorphic!!

 21. Why do I need a virtual destructor? If the destructor is not virtual, only the destructor corresponding to the pointer type is called. If the destructor is virtual, the destructor for the corresponding object type is called. Therefore, the use of a virtual destructor ensures that the correct destructor sequence is called.

22. Static-linked (early-linked): The compiler is responsible for answering the program call function, which executable code block will be used. The function calls in the source code are interpreted as executing a specific function code block called a function name union. In C + +, because of function overloading, the compiler must look at function arguments and function names to determine which function to use. However, the C + + compiler can do this in the compilation process. A binder is called a static union during compilation. dynamic linking (late linking): which function cannot be determined at compile time, the compiler must generate code that can select the correct virtual method when the program runs, which is called dynamic linking.

23. Converting a derived class reference or pointer to a base class reference or pointer is called an up-casting , which makes public inheritance unnecessary for explicit type conversions. As follows:

B b= (1840, "abc", "Def", true);//Derived class object

A *a=&b; A &a=b;

Converting a base-class pointer or reference to a derived-class pointer or reference, called a down -casting, is not allowed if explicit type conversions are not used.

A a;//base class

B b;//Derived classes

A *a=&b;

b *b= (b *) &a;

24. remember that the derived class object is a base class object because it inherits all the data members and member functions of the base class object. Therefore, the actions that can be performed on the base class object are applied to the derived class object.

25. The compiler uses static linking for non-virtual methods, and dynamic linking for virtual methods.

26. If the class is not used as a base class, dynamic linking is not required. Similarly, if a derived class does not redefine any method of the base class, it does not require a dynamic union. In these cases, it is more reasonable and more efficient to use static linking. Therefore, the static binder is set to the default selection of C + +.

27. How virtual functions work: add a hidden member to each object. A pointer to an array of function addresses is saved in the hidden member. This array is called the virtual function table (VTBL). The address of a virtual function declared for a class object is stored in a virtual table. The base class object contains a pointer to the Address table of all virtual functions in the base class. The derived class object will contain a pointer to the stand-alone Address table. If a derived class provides a new definition of a virtual function, the virtual function saves the address of the new function. If a derived class does not redefine a virtual function, the VTBL saves the definition of the original version of the function. If a derived class redefined a new virtual function, the address of the function is also added to the VTBL. Note: Regardless of whether the virtual function contained in the class is one or 10, you only need to add an address member to the object, but the table is different in size.

28. The cost of memory and execution speed using virtual functions:① Each object will grow to increase the amount of space to store the address. ② for each class, the compiler creates a virtual function Address Table (array). ③ each function call needs to be performed with no extra action, that is, to find the address in the table.

29. The constructor cannot be a virtual function; The destructor should be a virtual function, unless the class is not used as a base class; A friend cannot be a virtual function, because a friend is not a class member, only a class member can be a virtual function.

30. If the derived class does not redefine the function, the base class version of the function will be used. If the derived class is in a derived chain, the most recent version of the virtual function is used, with the exception that the base class version is hidden.

31. The method of redefining inheritance is not overloaded. If you redefine a function in a derived class, you will not overwrite the base class declaration with the same function signature, but instead hide the base class method with the same name, regardless of the feature label.

32, Two rule of thumb:① If you redefine the inherited method, you should ensure that the original prototype is exactly the same, but if the return type is a base class reference or pointer, you can modify it to point to a reference or pointer to a derived class. This attribute is referred to as return type covariance because the return type is allowed to change with the class type. ② If the base class declaration is overloaded, you should redefine all of the base class versions in the derived class. If only one version is redefined, the other two versions will be hidden and derived class objects will not be able to use them. Note that if you do not need to modify, the new definition can call only the base class version.

33. The difference betweenprivate and protected is only manifested in classes derived from the base class. Members of a derived class can directly access the protected members of the base class, but not the private members of the base class directly. Therefore, for the outside world, the behavior of protecting members is similar to that of private members, but for derived classes, protecting members behaves like a public member.

34. C + + provides a function that is not implemented by using pure virtual functions . The end of a pure virtual function declaration is =0. When a class contains pure virtual functions, you cannot create objects of that class, and classes that contain pure virtual functions are used only as base classes!! To be a pure virtual function, you must include at least one pure virtual function. Pure virtual functions can be defined or not defined!!

35. Derived classes of the abstract base class (ABC) are called concrete classes. ABC describes an interface that uses at least one pure virtual function, and classes derived from ABC use regular functions to implement this interface based on the specific characteristics of the derived class. ABC contains all methods and data members that are common to derived classes, and methods that behave differently in derived classes should be declared as virtual functions.

  36. inheritance and dynamic memory allocation:① derived classes do not use NEW: you do not need to define explicit destructors, copy constructors, and assignment operators for derived classes. ② derived classes use NEW: explicit destructors, copy constructors, and assignment operators must be defined for derived classes. Note: A derived class copy constructor can only access data from a derived class, so it must call the base class copy constructor to process the shared base class data.

37. Summary: When both the base class and the derived class use dynamic memory allocation, the destructor, copy constructor, and assignment operators of the derived class must use the corresponding base class method to handle the base class element. This requirement is met in three different ways. For destructors, this is done automatically; for constructors, this is done by invoking the copy constructor of the base class in the initialization member list; If you do not do so, the default constructor for the base class is automatically called. For an assignment constructor, this is done by using the assignment operator of the base class called by the scope resolution operation modifier explicitly.

  38. About friend functions: If you want a friend function of a derived class to be able to use a friend function of the base class , you can cast the derived class reference or pointer to a base class reference or pointer by forcing the type conversion, and then use the converted pointer or reference to call the base class's friend function.

39. If a function declares a parameter as a reference or pointer to a const, the parameter cannot be passed to another function unless the latter ensures that the parameter is not modified.

40 . What cannot be inherited? constructors, destructors, assignment operators, friend functions.

41. can I assign a base class object to a derived class object ? OK!! ① derived classes have a transform constructor: B (const A &), the conversion constructor can accept a type as a base class parameter and other parameters, provided the other parameters have default values: B (const A & a,double c=1.0,double r=0.1) If there is a conversion function, the program creates a derived class object from the base class object and then uses it as an argument to the assignment operator. Method ② defines an assignment operator for assigning a base class to a derived class: B & b::operator= (const A &) {...}

"C + + Primer Plus" Reading notes 11-Class inheritance

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.