C + + five major features

Source: Internet
Author: User

1. Object

In the objective world, any thing can be regarded as an object, or our world is made up of countless objects. objects can be natural objects, such as Cheonan, Noah's Ark, and so on, can also be some logical structure, such as the United Nations, C + + language, and even can be a math problem, a research paper, tomorrow's study plan. The object can be very small, it depends on what you want to do.  For example, a school is an object, a class can also be an object, a student can also be an object. Any object will have two features: one is static, we call this static property (attribute), and the other is the dynamic property, which we call behavior (behavior). For a student, his name, gender, age, etc. will not change for a period of time, that is, his static characteristics, that is, attributes, and he will do different things at different times, such as waking up in the morning, sleeping at night, these are his dynamic properties, that is, behavior.  If you want to control the behavior of this classmate, we can tell him a message, such as a holiday, if he is not silly, he will not go to school. An object may have multiple properties and behaviors, and the outside world can control behavior through messages, which is the general pattern of things in our real life. In an object in C + +, attributes are mainly represented by data, and behaviors are mainly represented by functions.  The message is often called the public member of this object (which is generally a public function), and we call the public member that can be controlled by outside messages the external interface of the object. When using object-oriented programming to design a complex software, the first problem is to consider how the software system consists of those objects, and then consider the properties and behavior of those objects. After knowing this, we design the object's abstract-class. And some classes are often used, so there is a class library, each time you want to use in the header file contains the corresponding class library.

2. Encapsulation and information concealment

In C + +, object member properties can be defined to implement the encapsulation of objects, that is, some of his data and functions to the outside world, so that the outside world do not know, or even know. For example, a recorder, it is seen only outside the outer shell, and the inside of the mechanical control and circuit board for the user is not aware.      The user knows just a few keys have what function. This has two major benefits: one is to greatly reduce the difficulty of people to use objects, users at all regardless of how the implementation of the inside. For a tape recorder, the user only needs to know when the recording button is pressed and the recorder will start recording. Second, the protection of intellectual property rights, through careful design, you can hide the hidden, only to leave some interface to the user of the class, users can not modify the existing functions, and can not know how the existing functions are implemented, so as to achieve information concealment. External interface is generally the function name, this in the later study we will introduce in detail.

3. Abstraction

In the program design, we often see abstract the term, in fact, abstract concept is not abstract, we have been on the abstract commonplace. For example, the word "people", we curse is to say "xxx is not a person." That is to say, whether Zhang San, John Doe or Harry can be called people, people are Zhang San, John Doe, Harry Abstraction.  And as long as people with Chinese nationality, we can call him Chinese, no matter where the birthplace of the Chinese word is obtained through abstraction. The process of abstraction is to find common ground about things, and abstract function is to express the essence of the same kind of things. In fact, in the language of other places we have already embodied the role of abstraction. such as "int" is an abstraction of all shaping data. In object-oriented C + +, one of the most important abstractions is the class, which is abstracted from the desired object. That is, a class is an abstraction of an object, and an object is a special case of a class.

4. Inheritance and Reuse

Inheritance and reuse are C + + and important features of face objects, which can make object-oriented programming more efficient when designing large programs. It's like a car factory want to produce a new type of car, generally do not start from scratch, but choose a car as the basis, and then add some new features, the development of a model of the car.  C + + differs from C in some ways. If a class named "A" has been established in software development, and you want to create a class named "B", which is basically the same as the former, it simply adds some attributes or functionality (that is, behavior). This is the inheritance mechanism in object-oriented programming. For example, we first design a "person" class to express the human characteristics, his attributes are name, age, gender, etc., if we also want to design a "Student" class to represent the characteristics of students, we only need to add the "person" class based on the number, class and other attributes can be,  This greatly simplifies the process of programming. It can be seen that the inheritance mechanism of C + + can easily use the existing classification to establish a new classification, so that the existing software can be reused or even a large part, greatly reducing the workload of programming. In addition, we can not only make use of the classes we have built in the past, but also the classes that others have built or those that are stored in the class library. This will greatly reduce the development cycle of software, the development of large-scale software has important significance.

5. Polymorphism

Polymorphism in our daily life is often seen, such as the end of the exam, students are relaxed to eat a big meal, and teachers are hard to change the test paper, roadside cleaners do not have to do anything.  Like this same message to send a different object, different objects react to different phenomena, is polymorphism. In C + +, polymorphism manifests itself in a class that is generated by inheritance although related but different, the objects of these classes may react differently to the same message.  For example, the students, teachers and cleaners all inherited the character of the person, but they reacted differently to the news that the exam was over. Polymorphism is an important feature of object-oriented programming, which greatly increases the flexibility of the program.

C + + five major features

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.