C ++ object-oriented programming notes (1)

Source: Internet
Author: User

C ++ Object-Oriented Programming (1)

Today I have read a little bit about Object-Oriented Programming in primer. The following is my summary.

I. Virtual Functions

1. A function defined as virtual is defined by a base class that is expected to be redefined by a derived class. The function to be inherited by a base class cannot be defined as a virtual function. The new function in the derived class should have the same number of parameters and type as the virtual function. To implement unified interfaces and different definition processes.

2. dynamic binding occurs when a virtual function is called through a reference (or pointer) of a base class. A reference (or pointer) can point to a base class object or a derived class object, which is the key to dynamic binding. The virtual function called by reference (or pointer) is determined at runtime. The called function is defined by the actual type of the object referred to by reference (or pointer.

3. The root class at the inheritance level generally defines virtual destructor. Constructor cannot be defined as a virtual function.

4. To trigger dynamic binding, two conditions must be met: first, only the member functions specified as virtual functions can be dynamically bound. Second, function calls must be performed through a reference or pointer of the base class type. Each derived class object contains the base class part. Therefore, you can bind the reference of the base class type to the base class part of the derived class object, or point the base class pointer to the derived class object, any operations that can be performed on the base class object can also be used by the derived class object.

Ii. Access Permissions

5. The derived class cannot access the private member of the base class.

6. A derived class can only access the protected member of its base class through the derived class object. The derived class has no special access permission to the protected member of its base class object.

7. Inheritance

* Public inheritance: The base class members maintain their own access level. The Public Member of the base class is the Public Member of the derived class, and the protected member of the base class is the protected member of the derived class.

* Protected inheritance: the public and protected members of the class are protected members in the derived class.

* Private inheritance: all the members of the base class are private members in the derived class.

8. A derived class can restore the access level of the inherited member, but it cannot make the access level more loose than originally specified in the base class.

9. You can use using to declare the names in the access base class. Besides replacing the namespace name with the class name on the left side of the scope operation, the usage form is the same.

10. A derived class defined by the class reserved word has private inheritance by default, while a class defined by the struct reserved word has public inheritance by default.

Iii. Friends and static in inheritance

11. Friendship cannot be inherited. The Friends of the base class does not have special access permissions for the types derived from the base class. Similarly, if both the class and the derived class need to access another class, the class must grant the access permission to the base class and each derived class. Simply put, my friends are not my friends.

12. If the base class defines static members, there is only one such member in the inheritance hierarchy. No matter how many derived classes are derived from the base class, each static member has only one instance.

Iv. conversion relationships in inheritance

13. The derived class object is also a base class object. There is an automatic conversion from a derived type reference (pointer) to a base class reference (pointer, however, there is no (automatic) Conversion from base class reference (or base class pointer) to derived class reference (or derived class pointer. There is no direct conversion from a derived type object to a base class object.

14. If there is an object of the derived type, you can use its address to assign values or initialize pointers of the base class type. You can use a reference of a derived type or an object to initialize a reference of a base class type. Generally, you can use a derived type object to assign values or initialize the object.

15. Reference conversion and conversion objects.

* When you pay an object to a function that you want to accept the reference, the reference is directly bound to this object. In fact, the real parameter is a reference to this object, and the object itself is not copied, and the object is still a derived type object.

* When you pay a derived class object to a function that wants to accept a base class object instead of a reference, the situation is completely different. The fern part of the object in this derived class is copied to the form parameter.

16. automatic conversion from the base class to the derived class does not exist. The base class object cannot be used when a derived class object is required.

5. constructor and replication control in inheritance

17. constructor and replica control members (copy constructor, value assignment operation function, and destructor) cannot inherit. Each class defines its own constructor and replica control member.

18. You can define some classes as protected only the special constructors that you want the derived classes to use;

19. The default constructor of the synthesized derived class. In addition to initializing the data members of the derived class, it also initializes the base class part of the object of the derived class. The base part is initialized by the default constructor of the base class.

20. The initialization list of the derived class constructor can only initialize the members of the derived class, But Cannot initialize the inherited members directly. However, you can include the base class in the constructor initialization list to indirectly initialize the inherited members.

Class B: public

{

Public"

B (): A (1, 0), B (1 ){}

PRIVATE:

Int B;

};

21. A class can only initialize its own direct base class.

22. If the derived class defines its own copy constructor, the copy constructor should explicitly use the base class copy constructor to initialize the base class part of the object.

23. If a derived class defines its own value assignment operator, the operator must explicitly assign values to the base class.

24. The destructor of the derived class is not responsible for revoking the member of the base class object. Each destructor is only responsible for clearing its own members. To run an appropriate destructor, The destructor in the base class must be a virtual one. If the Destructor is a virtual function, the type of the destructor to run varies depending on the object type. If the root class destructor in the hierarchy is a virtual function, the derived class destructor will also be a virtual function.

25. In a base class constructor or destructor, the derived class object is treated as a base class object. If you call a virtual function in a constructor or destructor, the version is defined for the constructor or the Destructor type. After a base class destructor is defined as a virtual function, the system calls the corresponding class destructor when Delete is used to delete an object pointer to a derived class definition. Instead of defining a destructor as a virtual function, only the basic class's destructor are called.

 

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.