C++primer Review (vi)-Object oriented programming

Source: Internet
Author: User

1 in C + +, dynamic binding occurs when a virtual function is called through a reference (or pointer) of a base class, and two conditions (base class reference or pointer, virtual function) are indispensable

Static bindings for default real arguments of virtual functions

2 The root class of an inheritance hierarchy generally needs to define a virtual destructor

3 any non-static member can be a virtual destructor, and the static member function cannot be a virtual destructor (the static member should not belong to an object, but to the entire class)

4 Protected Members: Derived classes can access only the protected members of the base class through derived class objects, and derived classes do not have special access rights to protected members of their base class type objects

Private member: The private data member of the base class exists in the object of the derived class type, but the derived class does not have any permissions to access the private member of the base class

class a{private:    int  val;}; class b:a{public:    void func () {cout << val;}};

Func will error because Val is not authorized to access in a derived class

It can be understood that the private and protected members in the base class are accessible outside the base class through the base class type object, the protected member is visible in the derived class, and the private member is invisible in the derived class

5 virtual functions in derived classes and virtual functions in base classes are consistent unless the return type is a reference to a base class type, a virtual function in a derived class can return a reference to a class type or a derived class type

6 when a pointer or reference to a base class type calls a virtual function, it is possible to implement a dynamic binding mechanism that overrides virtual functions by a scope operator, that is, the version of the specified virtual function

7 Interface Inheritance: Public inheritance, which is the interface that inherits the base class

Implementation inheritance: Protected inheritance and private inheritance, that is, the part that inherits from the base class is not part of its interface, just for its own sake

In implementing inheritance, you can implement the public inheritance of these members by declaring the members of the base class under the public declaration of the derived class

8 static members have only one in the entire inheritance hierarchy, and are not accessible in derived classes if they are private types. A static member can be invoked either by using a scope operator, or by using the arrow operator to invoke

9 in derived classes, constructors always first construct the base class part and then construct the derived class part, even if there is no base class constructor in the derived class constructor initialization list, the default constructor of the base class is called first, and the base class part is constructed

In a derived class constructor initialization list, you can only initialize your own direct base class (respecting the base class interface)

10 derived class destructor: The derived class destructor is not responsible for revoking the members of the base class object, the compiler always calls the destructor of the base class part of the derived class object explicitly, and each destructor is only responsible for cleaning its own members

The undo order of an object is reversed from the construction order, first the derived class destructor is executed, and then the base class destructor is called up by the inheritance hierarchy.

11 Virtual destructor:

Derived classes automatically call the base class destructor the Undo base class part has an important influence on the design of the base class.

When you delete a pointer to a dynamically allocated object, you need to run the object's destructor before releasing the object's memory. However, the static type of the pointer may be different from the dynamic type of the object being deleted (a pointer to the base class type that actually points to the derived class object may be deleted), so the destructor of the base class is a virtual destructor, which guarantees the dynamic binding of the destructor

12 Pure virtual functions:

Add =0 to the virtual function parameter list to define virtual functions as pure virtual functions, classes that contain (or inherit) one or more pure virtual functions are called abstract base classes, and abstract classes cannot create objects

If a class does not declare a virtual function inherited from a base class as a pure virtual function, and the class does not implement its own version of the virtual function, a pointer to that class will use the virtual function version of the inheriting class when the virtual function is called dynamically, which may not satisfy the expectation of the user of the class to invoke the function, so declare the function as pure This will not create an object of the class

13 Handle Class

C + + Object-oriented programming, but cannot use the object itself to support object-oriented programming, but must use pointers or references

The generic technique is to define handle classes, handle classes to store and manage pointers to base classes, and handle users to get dynamic behavior.

C++primer Review (vi)-Object oriented programming

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.