Object-oriented notes in C ++

Source: Internet
Author: User

Object-oriented notes in C ++

Public inheritance is equivalent to a link. For example, class B: public A, which means that B is A, A does, and B does.

Class a {}; class B: public A {}; A a; B B; a = B; // error, because a is not necessarily bb = a; // correct, B is

Under public inheritance, the name will be overwritten: If B and A have functions with the same name, the function in A will be overwritten, including the overload function of the function in.

 

 

Virtual Functions

Classes containing pure virtual functions such as virtual void fun () = 0; are abstract classes. We cannot create abstract class objects. If A has A pure virtual function, a A = new A is wrong, and a A = new B is correct.

Pure virtual functions:

The function must be declared again in the derived class. (The purpose of declaring a pure virtual function is to make the derived class inherit only the interface)

To implement pure virtual functions, you need to apply for them, as shown below:

Class a {virtual void fun () = 0 ;}; void A: fun () {// Implementation} class B {virtual void fun () {:: fun (); // The Implementation of A needs to be declared}; class c {virtual void fun (); // You can implement it yourself}; void C: fun () {// C fun}

Non-pure virtual functions:

The derived class inherits the declaration and implementation of the function, which can be implemented by itself to overwrite. The security is caused by the default implementation (that is, the implementation of the base class) when the derived class forgets the UDF inherited from the custom class ). The default function implementation may not be suitable for this derived class.

 

Non-virtual functions:

Abstract classes (only abstract classes) will force the derived classes to inherit the non-virtual functions of the class, that is, the non-virtual functions of the abstract class will overwrite all functions of the same name of the derived classes, this approach is just opposite to the inheritance of non-abstract classes.

 

Summary:

  Under Public inheritance, pure virtual functions, non-pure virtual functions, and non-virtual functions play the following roles in the inheritance of abstract classes: only interfaces, inherited interfaces and default implementations, inherited interfaces, and forced implementations are inherited.

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.