C + + Primer Learning Summary 15th Chapter object-oriented programming

Source: Internet
Author: User

The 15th chapter object-oriented programming


1. Construct the base class and the derived class.

Where Class A is the base class and Class B is a derived class. constructors for derived classes must be re-written and cannot be inherited. (because, after all, the class names of the two classes are different, it is not possible to inherit from the constructor) only other member functions and member variables are inherited.

A derived class can override a virtual function of a base class, but you can also choose not to overwrite (that is, to use the function version of the parent class directly) the Print_1 () virtual function of the scale a class is not overwritten.

static members of a base class: If the base class has a static member, then the base class and all derived classes collectively own the only static member.


2. The virtual function default arguments of the base class are best consistent with the virtual function default arguments of the derived class.

Because if a function is called through a reference or pointer to a base class, the default argument version of the base class is used, but the implementation of the function is dynamically bound, that is, a function that may be a base class or a function of a derived class.


3. Derived classes can change the access rights of members in a base class.

However, only those base class members that derived classes can access are limited.

You can use a using declaration.

4. A member of the same name as a base class in a derived class hides that member of the base class.

5. In the current derived class, you can access the protected member of the base class of the current derived class, but you cannot access another base class or a member of a derived class.

6. In addition to redefining the virtual function, the members of the derived class must not have the same name as the members of the base class. And be sure to precede the virtual function redefined by the derived class with the virtual keyword ( Even if you do not add,C + + defaults to the function as a virtual function, But in order to use the derived class of " customer " to see Clearly, must add )

If the base class has virtual functions, virtual void P (); Then, if the derived class defines a function void P (); void P () is also a virtual function because the function has the same name as the virtual function of the base class and is called in the same form . It is strongly recommended that you add the virtual keyword before the derived class void P ().

If the function in the derived class is void P (int x=1); In this case, the P function is not a virtual function and masks the virtual functions of the base class called P ().

We analyze each of the 4 calls in turn:

First is the first pa->p (), at this time pa=&b. Because PA is a class A pointer, so the compiler looks for the P function in Class A, but it finds that the P function is a virtual one, so the virtual P () function of Class B is called after dynamic binding, but virtual P () of Class B uses the default implementation of Class A, So the final call here is a::p () function.

The second pa->p () is similar to the first, also the virtual function call, and the PA binds to the &a, so the call is a::p () this virtual function.

The third b.p (), because B is an object, so the P function is statically bound, so the call is a function of B::p (int x=1).

Fourth A.P (), statically bound, invokes a::p () function.

7. The destructor for a polymorphic base class must be a virtual function.

Because if the destructor of the base class is not virtual, delete a base class pointer calls only the destructor of the base class and does not call the destructor of the subclass ( even if the pointer is a pointer to a subclass ).

C + + Primer Learning Summary 15th Chapter object-oriented programming

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.