C ++ Object-Oriented Programming details,

Source: Internet
Author: User

C ++ Object-Oriented Programming details,
C ++ object-oriented programming is designed in the C ++ language. When we use a base class reference or pointer to call a virtual function, dynamic binding will occur, that is to say, the polymorphism base class in JAVA usually defines a virtual destructor, even if the function does not perform any actual operations. In C ++, there are two types of basic class member functions: 1. The base class wants the function to be rewritten by the derived class; 2. The base class wants the derived class to inherit directly without changing the function. For the former, the base class defines it as a virtual function. When a pointer or reference calls a virtual function, the call is dynamically bound (that is, polymorphism ). Non-static functions other than constructors can be virtual functions. The keyword virtual can only appear before the function declaration statement inside the class, but not before the Function Definition Statement outside the class. If the base class calls a function as a virtual function, the function is implicitly a virtual function in the derived class. If a member function is not declared as a virtual function, its parsing process occurs during compilation rather than runtime. Therefore, in the hierarchy of inheritance, this function has only one and cannot be rewritten into multiple versions. The derived class must re-declare all the virtual functions in the base class. A derived class can be added with the keyword virtual before such a function, but this is not necessary. If the class Access Control Character in the derived list is public, we can bind an object of the derived type to a reference or pointer of the base class. Although the derived classes have data members of the base classes, they can only be initialized through the constructors of the base classes.

Class A {public: int a; A (int p): a (p) {}} class B: public A {public: int B; B (int p1, int p2): A (p1), B (p2) {}} // The constructor of B First initializes A by the constructor of a, then executes the constructor of A, and then initializes B, when the final declaration of the constructor body of B is executed, the Declaration contains the class name but does not contain its derived list class B: public A; // error class B; // correct
If a class is treated as a base class, the class must be defined rather than declared.
Class A; // only declares class A class B: public A {...} // error, class A must be defined
Type conversion from a derived class to a base class only applies to a pointer or a reference type. A base class does not have an implicit type conversion. When we use a derived class object to initialize or assign a value to a base class object, only the basic part of the object in this derived class will be copied, moved, and assigned, and its derived class part will be ignored. Therefore, the base class object cannot be forcibly converted to a derived class object.
Class A {} class B: public A {} B B; A a (B); // actually, A copy constructor is called, that is, :: A (const A &) a = B; // In fact, the copy assignment operator is called, that is, A: operator = (const &)
List content

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.