Note 2 Object-Oriented Programming

Source: Internet
Author: User
Object-oriented three basic concepts: encapsulate inheritance polymorphism 1 Define the base class and the derived class 1 Define the base class virtual modified functions as virtual functions, the call of virtual functions in Program When running, it is determined that the member function is a non-virtual function by default. When calling a non-virtual function, it is determined that, apart from the constructor, any non-static member function can enable the virtual function, virtual only appears in the member function declaration within the class, and cannot appear outside the class definition body on the function definition. 2. Access Control and inherit from users Code It can be a public member of the primary class but not a private member of the primary class. Private Members can only be accessed by members and friends of the base class. A protected member can be accessed by a derived class object but cannot be accessed by a common member of this type. 3. A derived class can only access the protected member of the base class through the derived class object. The derived class has no special access permission to the protected member of the base class object. Protected price; // defined in the base class void bulk_item: memfcn (const bulk_item & D, const item_base & B) {double ret = price; // OK this-> price ret = D. price; // OK derived class Object ret = B. price; // error base class Object} 4 derived class classname: Access-label base-classaccess-label: Public Private protected access label determines the access permissions of inherited members, if you want to inherit the interface of the base class, use the public derived class to redefine the inherited virtual function. If the derived class does not redefine a virtual function, use the version in the base class. The Declaration of the virtual function in the derived class must exactly match the definition method in the base class, but there is an exception: the virtual function that returns the reference (or pointer) of the pair and type, the virtual function in the derived class can return the derived type reference (OR) pointer of the type returned by the base class function. 5. The class used as the base class must be defined as 6 virtual and other member functions. To trigger dynamic binding, the following two conditions must be met: 1) only member functions specified as virtual functions can be dynamically bound. 2) function calls must be performed through a reference of the base class type or pointer. 7. The static and dynamic types of pointers can be different, this is the basis for C ++ to support polymorphism. Static type: the reference type or pointer type that can be known during compilation. Dynamic type: pointer or reference the type of the bound object, this is only known during the runtime. Eight non-Virtual Member calls are identified as members of the Call base class during compilation. Non-virtual functions are always determined by the objects, references, or pointer types that call the function during compilation. 9

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.