Object-oriented understanding in C + +

Source: Internet
Author: User
Tags access properties

??

1, for OO (object-oriented) meaning, not everyone's views are the same.

Even now. If you ask 10 people, you may get 15 different answers. Almost all of them will allow inheritance and polymorphism to be the concept in OO. Most people will also add encapsulation.

Others will add exception handling. And there might be no one to include the template in. The crux of the problem is that there are often different viewpoints about whether an attribute belongs to OO. And every opinion has a passionate advocate.
2. The most important contribution of C + + standardization to C + + is to provide stronger support for powerful abstraction mechanisms, thus reducing the complexity of the software. C + + is not just an object-oriented language. It can support several programming styles, including object-oriented programming styles and generic programming styles. These programming styles are important because each programming style provides a very flexible way to organize your code through an abstraction mechanism.

Object-oriented programming allows us to bind the state of objects and the functions that handle those states, while encapsulation and inheritance enable us to manage interdependencies and enable the reuse of code in a clearer and easier way. Generic programming is a relatively new style that allows us to write some functions and classes. They can operate on other functions and objects that are "undefined, unrelated, and unknown", providing a unique way to reduce the coupling and interdependencies in the program. Currently, support for generics is also available in some other languages. However, there is no such strong support for generics as in C + +. In fact. Today's generic programming may be attributed to the unique template form of C + +. Today. There are many powerful ways to express abstract mechanisms in C + +, and the flexibility that comes with this is the most important achievement in C + + standardization.
3. There is no module system in C + + (complete or incomplete); It lacks other important features, such as the memory garbage collection mechanism, which has a static type check, but not necessarily a "strong" type check.
1. Strong/Weak type refers to the strict degree of type checking. Language has no type, weak type and strong type three kinds. no type is checked. It doesn't even differentiate between instructions and data.

Weak-type checks are very weak and can only strictly differentiate between instructions and data. Strong types are strictly checked at compile time.

strongly typed definition language: a language that enforces the definition of a data type.

Other words. Once a variable is assigned a data type, it is assumed that it is not cast. Then it will always be the data type. For example: Suppose you define an integer variable A, it is not possible for a program to treat a as a string type at all. A strongly typed definition language is a type-safe language.

weak type definition language: a language in which data types can be ignored. In contrast to strongly typed definition languages, a variable can assign values of different data types.

2. Dynamic Type language: Dynamic type language refers to the language in which data type checking is done during execution, that is, when programming in a dynamic type language. You never have to assign a data type to any variable, and that language will be assigned to the variable the first time you assign it. The data type is recorded internally.

statically typed languages: static type languages are just the opposite of dynamic type languages. Its data type is checked during compilation, meaning that the data type of all variables is declared when the code is struck. C + + is a typical representation of statically typed languages, and other static type languages include C #, Java, and so on.

Strongly typed definition language may be slightly slower than weak type definition language, but the rigor of strong type definition language can effectively avoid many errors.

In addition, "The language is not a dynamic language" and "the language is the type of security" is completely no connection between!

3. Encapsulation

Encapsulation is to ensure that the software components have good modularity, the goal of the package is to achieve the "high cohesion, low coupling" software components, to prevent program interdependence and the impact of the changes.

In object-oriented programming languages, objects are the most basic unit of encapsulation, and object-oriented encapsulation is clearer and more powerful than traditional language encapsulation.

Object-oriented encapsulation is to encapsulate the code describing the properties and behavior of an object in a "module". That is, in a class, attributes are defined by variables, behaviors are defined by methods, and methods can be used to directly access properties in the same object.

First, a good package can reduce coupling.
Second, the implementation within the class can be freely changed.


Third, the class has a clear external interface.
The advantages of encapsulation are very well understood, and our house is an example of a class. Interior decoration and furnishings can only be appreciated and used by indoor residents, assuming that there is no four walls of the shelter, all indoor activities in front of the outside world. Because of the encapsulation. All the furnishings in the house can be arbitrarily changed without affecting others. However. Suppose there are no doors and windows. A black box wrapped tightly. Even if its space is wide, it has no useful value. The doors and windows of the House are the properties and methods that the object of the package exposes, specially for people to enter and leave, and to circulate air and bring sunshine.

4. Inheritance

At the time of defining and implementing a class. Can be based on a class that already exists. Inherit the content defined by the existing class as its own content, and be able to add a number of new content, or change the original method to make it more suitable for special needs. Inheritance is the mechanism by which subclasses actively share the data and methods of the parent class, which is a is--a relationship between classes. Improved reusability and scalability of the software.

First, subclasses have properties and functions that are not private to the parent class.
Second, subclasses have their own properties and functions, that is, subclasses can extend the properties and functions that the parent class does not have;
Third, subclasses can also implement the functions of the parent class in their own way (method overrides).


For constructors. It cannot be inherited, it can only be called. Inheritance destroys encapsulation. The implementation details of the parent class are exposed to subclasses.

This actually increases the coupling between the two classes. And it's a strong coupling relationship.

5. polymorphic

The same operation acts on instances of different classes. will produce different running results. That is, when objects of different classes receive the same message. Get a different result. Polymorphism is one of the important features of object-oriented programming, and it is another significant manifestation of extensibility after "inheriting". objects are acted upon by the received message, and the same message is accepted by different objects, which can lead to a completely different behavior, a phenomenon called polymorphism.

A virtual method is called dynamically by its execution-time type and not by the compile-time type.

6. Abstract

Abstraction is the identification of similarities and commonalities of things, and then the attribution of these things to a class that only considers similarities and commonalities of these things, and ignores those aspects that are irrelevant to the current subject and goal. Focus on the aspects that are relevant to the current goal.

First, abstract classes can not be instantiated;
Second, the abstract method is the method that must be overridden by the quilt class;
Thirdly, if the class includes an abstract method, then the class must be defined as an abstract class, whether or not it can include other general methods.

7. Interface

An interface is the combination of implicit public methods and properties to encapsulate a collection of specific functions. Once the class implements the interface. Class is able to support all the properties and members specified by the interface.

Declaring an interface is syntactically identical to declaring an abstract class, but it does not agree to provide a way for the interface to run regardless of its members. So the interface cannot be instantiated. Cannot have constructor methods and fields. Cannot have modifiers, cannot be declared as virtual or static, and so on. There are also classes that implement interfaces that must implement all the methods and properties in the interface.

A class can support multiple interfaces, and multiple classes can support the same interface.
Abstract class can give some of the implementation of the member, the interface does not include the implementation of the member, the abstract class of abstract members can be part of the implementation of the class, the interface members need to implement the full implementation of classes, a class can only inherit an abstract class, but can implement multiple interfaces and so on.


??

Object-oriented understanding in C + +

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.