C + + Object-oriented programming

Source: Internet
Author: User

1. The core idea of object-oriented program design is data abstraction, dynamic binding and inheritance.

Inheritance: roots are called base classes, and inherited classes are called derived classes. A derived class must explicitly indicate which base class it inherits from by using a list of derived classes, which is a colon, followed by a comma-delimited list of base classes. A base class can declare a function as a virtual function to let a derived class define its own version.

Dynamic binding: Dynamic binding refers to the running version of the function that is known at run time, so we are dynamically bound when invoking a virtual function with a reference (or pointer) to the base class.

2. Defining base classes and derived classes

A. The base class usually defines a virtual destructor;

B. If the member function is not declared as a virtual function, its parsing process occurs at compile time and not at runtime.

C. Derived classes can access the base class public members as well as other classes, and cannot access private members. However, derived classes can access protected (protected) members, and other classes do not.

D. The base class of a class-derived list can use the access specifier: public, protected, or private

E. A derived class object contains multiple parts: a member that contains the derived class's own definition, and a derived class that inherits the corresponding child objects of the base class. Self-understanding: Only a derived class object can be converted to a reference to the base class when needed

F. A member inherited from a base class in a derived class object cannot be initialized with the constructor of a derived class, and must be initialized with the constructor of the base class.

G. The declaration of a derived class contains a derived list of the class name but does not contain it.

H. If you want to use a class as a base class, you must define it rather than just declare it, and prevent inheritance from using the keyword final.

i. There is no implicit conversion from the base class to the derived class, and no type conversions exist between the objects. Therefore, when a reference to a derived class object is converted to a base class, the constructor of the base class is called, and only the members of the base class are called. The members of the derived class are ignored.

3. Virtual functions

A. The fact that the static type of a reference or pointer differs from the dynamic type is the root of the C + + language's support for polymorphism.

B. The override keyword can be used in the new standard to illustrate virtual functions in derived classes.

C.

When a function is declared as a virtual function in the base class, the derived class overrides this function of the base class without adding the virtual keyword, because once a function is declared as a virtual function, it is also a virtual function in all derived classes.

If a function of a derived class overrides a virtual function of the base class, then the function's parameters and return type must be identical to the virtual function.

Under test, when a derived class overrides a function parameter of the base class or the return type is inconsistent with the virtual function of the base class, the function is considered by the compiler to be a new function that the derived class re-defines, rather than being considered a virtual function that overrides the base class. If this error is difficult to find in the actual turn, I was found in the test with the Override keyword added to the function of the derived class, which is why c++11 added it.

However, there is one exception, which is that the return type is invalid if it is a pointer or reference to the class itself. Is that the base class can return base* or Base& the derived class can return dervie* or derive&

Conclusion: a virtual function in a base class is implicitly a virtual function in a derived class, and when a derived class overrides a virtual function, the function must match the parameters in the base class strictly with the formal parameter in the derived class

D. If a function is declared final, any subsequent operation that attempts to overwrite the function throws an error.

4. Abstract base class

' A. Pure virtual function

The newly defined disc_quote is a generic base class for discounting, not a specific discount strategy, so we don't want users to define a disc_count, but rather define a specific discount strategy.

We can define the Net_price in Disc_quote as a pure virtual function to implement. This can clearly tell the user that the current net_price is of no practical significance. is not the same as a normal virtual function.

A pure virtual function does not need to be defined, we can do this by adding = 0 after the function body position. You can turn a virtual function into a pure virtual function. Where =0 can only appear within a virtual function declaration statement inside a class.

Note: We can also provide definitions for virtual functions, but the body of the function must be defined outside the class, that is, we cannot provide a function body within the class for a function of = 0.

B. Classes containing pure virtual functions are abstract base classes

Classes containing (without overwriting directly inheriting) pure virtual functions are abstract base classes, the abstract base class is responsible for defining interfaces, and subsequent classes can overwrite interfaces, and we cannot directly create an object of the abstract base class.

We can define the derived class object of the abstract base class, provided that the interface of the pure virtual function is overwritten.

Note: We cannot create an abstract base class object.

C. A derived class constructor initializes only its direct base class

Base class Quote-----> Abstract base class disc_quote-----> Derived class Bulk_quote

--Represents an inheritance relationship

Then we can only initialize the disc_quote when we define the Bulk_quote constructor, and initialize the members of the quote in the constructor of Disc_quote.

Key Concept Refactoring:

Refactoring is responsible for re-engineering the system of classes to move operations and/or data from one class to another, and refactoring is a common phenomenon for object-oriented applications.

5. Access control and inheritance

Public, private, and protected inheritance (protected) are three common ways to inherit.

A. Public inheritance

Public inheritance is characterized by the public and protected members of the base class as members of the derived class, which remain in their original state, while the private members of the base class are still private and cannot be accessed by subclasses of the derived class.

B. Private inheritance (private)

Private inheritance is characterized by the public and protected members of the base class as private members of the derived class and cannot be accessed by subclasses of the derived class.

C. Protection of inheritance (protected)

The feature of protection inheritance is that all public and protected members of the base class become protected members of the derived class and can only be accessed by its derived class member functions or friends, and the private members of the base class are still private.

D. Accessibility of derived classes to base class conversions

1 user code cannot use a derived class to convert to a base class unless the derive class is publicly inherited by the base class, and if the derive inherits base is protected or private, the conversion cannot be used by user code.

2 Regardless of how derive inherits the base class, both the member function and the friend of derive can use the derived class to convert to the base class, and the type conversion of the derived class to its direct base class is permanently accessible to the members and friends of the derived class.

3 If the way derive inherits base is common or protected, then members and friends of derive's derived classes can use derive to convert to base type, and conversely, if derive inherits base in a private way, you cannot use the

E. By using a using declaration statement inside a class, we can tag any accessible member of the base class directly or indirectly in the base class, and the access to the name in the using declaration statement is determined by the access specifier in which it is located.

6. Class scope in inheritance

Each class defines its own scope, within which we define the members of the class, and when there is an inheritance relationship, the scope of the derived class is nested within its base class scope. If a name cannot be resolved within its scope, it is looked up in the base class.

The name lookup precedes the type check.

7. Constructors and copy Control

A. Virtual destructor: When you use delete a dynamic object, its destructor is executed, and when the delete pointer has a static type that is different from the dynamic type of the object being deleted, the compiler performs a destructor of the dynamic type. We can ensure that the destructor is defined as a virtual function in the base class.

8. Containers and Inheritance

When we use containers to store objects in the inheritance system, we usually have to take a brief approach to storing them, because we don't allow different types of elements to be stored in the container, so we can't put multiple types with inheritance relationships

Objects are stored directly in the container.

Note: When a derived class object is assigned to a base class object, the portion of the derived class is cut off, so the container is incompatible with the type that has the inheritance relationship.

Place (smart) pointers in containers rather than objects

When we want to store an object with an inherited relationship in a container, we actually store pointers to the base class, better to store smart pointers, and, as always, the dynamic type of the objects that the pointers refer to may be

The base class type, or possibly the derived class type.

Just as we can convert a pointer of a derived class to a pointer to a base class

We can also convert a smart pointer of a derived class to a smart pointer to a base class.

std::vector<std::shared<quote>>basket;

C + + Object-oriented programming

Related Article

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.