C + + Fundamentals-Object-oriented programming

Source: Internet
Author: User

First, object-oriented programming

1.1 What is object-oriented programming

The core of process-oriented program design idea is the decomposition of function. Separating the data structure and process, focusing on the process, the designer first considers how to break down the function, and arranges the sequence of operations in each process. One drawback, when the data structure of the program needs to be changed, the need to modify the corresponding miniature module, resulting in poor reusability, maintenance cost is high.

In object-oriented programming, the focus is on the data being manipulated rather than the process of implementing these operations.

Program = object + Message

The basic element is the object, the main structure characteristic: 1 The program generally consists of the definition of the class and the use of the class, defining each object in the main program and stipulating the law of the message passing between them; 2) Everything in the program is implemented by sending messages to the object, and after the object has received the message, it initiates the appropriate operation. ; 3) classes are related to each other by inheritance.

1.2 Objects and classes

1.2.1 Concept

A person is a "class", a collection of objects that have the same data and the same operations.

Yang or Liu Enping is a real person, an example of a human being, an object that describes the data of its properties and the unity that is encapsulated in a set of operations imposed on the data. Object = Property + method. It can be understood that a property is a data member in a class, and a method is a member function.

! A class is a new type of data structure that appears only in the code of the source program and does not appear in a running program, that is, the class exists only at compile time, and the object is present in the running program as an instance of the class, and occupies memory space, and the program's computing task has the object complete.

Interaction of 1.2.2 Objects

A request made by one object to another object is called a message.

An object belonging to a class has the same behavior, when a behavior acts on an object, that is, the object executes a method.

Three properties of the message: 1) The same object can receive different forms of multiple messages, make different responses, 2) the same form of messages can be passed to different objects, the response can be different, 3) the receiving object response to the message is not necessary, the object can respond to the message of course can not respond.

Determination and division of 1.2.3 Class

The difficulty of object-oriented programming: the determination and division of classes. Classify the class according to the actual situation rationally.

1.3 Abstraction and encapsulation of data

Object-oriented Systems highlight features: Abstraction and encapsulation, inheritance, and polymorphism.

1.3.1 Abstraction and encapsulation of the real world

Package: The recorder has a lot of buttons, we operate the recorder by pressing the key, and the details of its internal circuit are concealed to the user.

Abstract: The recorder's instruction manual tells us to let the machine play or stop. Abstraction is what you do, not how you do it.

1.3.2 Basic Concepts

The abstraction of data extracts the results of its common nature by analyzing the data instances.

The encapsulation of the data is the result of hiding the abstract internal implementation details.

Abstraction and encapsulation are complementary, and it is important that abstractions precede encapsulation.

1) The object has a clear boundary, and the properties and methods of the object are encapsulated within the boundary;

2) has an interface that describes the interaction between the object and other objects;

3) The internal code and data of the object should be protected and not easily modified.

Properties of the 1.3.3 object

1) Encapsulation

2) Module Independence

3) dynamic linking (via message mechanism)

1.4 Inheritance

Classification:

Single inheritance and multiple inheritance (from inheritance source)

Supersedes inheritance, contains inheritance, restricted inheritance, special inheritance, and so on (content partitioning)

There are several characteristics of inheritance relationships between classes: 1 There is a shared attribute between classes, 2 there is a difference between classes, or a new Part 3) between classes with hierarchies.

The introduction of inheritance mechanism does not affect the encapsulation of objects.

1.5 polymorphism

Polymorphism is when different objects receive the same message and produce many different ways of behaving.

C + + supports two polymorphism, compile-time polymorphism (implemented by overloading), and run-time polymorphism (implemented by virtual functions).

Overloading: Functions and operator overloading.

Class a{

Public:

Void print (int i);

Void print (float f);

Void print (const char *c);

//.....

};

Objective: In order to better express the behavior sharing, the similar operations are grouped together.

C + + Fundamentals-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.