C ++ FAQ Lite [7] - Class and Objects (Update)

zhaozj2021-02-08  224

[7] Class and Objects (Part of C FAQ Lite, Copyright © 1991-2001, Marshall Cline, Cline@parashift.com)

体,, nicrosoft @ sunistudio.com (East day production room, East day document)

FAQS in section [7]:

[7.1] What is the class? [7.2] What is the object? [7.3] What kind of interface is "good"? [7.4] What is packaged? [7.5] How does C be balanced between security and availability? [7.6] How can I prevent other programmers from viewing my class's private part of the package? [7.7] Is the package a safety device? [7.8] What is the difference between keyword struct and class?

[7.1] What is the class?

The basic composition of the object-oriented software defines the data type, as the structure in C. It is understood from the perspective of computer science, and the type is constructed by the state set and converted these states. Because int existing status set, it is also an operation like I J or I , so INT is a type. Similarly, the class provides a set of operational sets (usually public:) and a set of data sets of an abstract value owned by a set of description type instances. INT can be regarded as a class with Operator . (INT is not a class, but it is basically similar: a class is a type, just like INT is a type) Note: The C programmer can regard classes as a member default to private structure. However, if that is all your understanding of the class, then you may have to experience a model of personal changes.

[TOP | BOTTOM | Previous Section | Next Section]

[7.2] What is the object?

Storage area related to semantics When we declare Int i, we said: "I is an object of int type". In OO / C , "Object" usually means "an instance of the class". Therefore, the class defines the possible behavior of multiple objects (instances).

[TOP | BOTTOM | Previous Section | Next Section]

[7.3] What kind of interface is "good"?

A view that simplifies the "block"-shaped software and expresses the interface with the "user" vocabulary. ("Block" is usually a class or a set of closely connected classes; "users" refers to other developers rather than end customers)

"Simplified View" refers to the unnecessary detail hidden. This reduces the user's error rate. "User's vocabulary" means that users do not need to learn new vocabulary or concepts, which can reduce the user's learning curve.

[TOP | BOTTOM | Previous Section | Next Section]

[7.4] What is packaged?

Prevent unauthorized access to some information and functions.

The key to save money is to separate the variable portion from the stabilization portion of the software "block". Package this "block" places a firewall, which prevents other "block" from accessing the variable portion; other "block" can only access a stable portion. In this way, when the variable portion changes, it is possible to prevent other "block" from being destroyed. In the concept of object-oriented software, "chunk" typically refers to a class that is closely linked.

"Variable part" is a detail of implementation. If "block" is a single class, then variable portions typically encapsulate with private: and / or protected: keywords. If "block" is a set of closely connected classes, the package can be used to refuse access to all classes in the group. Inheritance can also be used as a form of packaging. "Stable part" is an interface. A good interface provides a view that simplifies the user's vocabulary and is designed from the outside. ("User" here refers to other developers, not the end users who purchase full applications). If "block" is a single class, the interface is just a class of public: member functions and friend functions. If "block" is a set of closely connected classes, the interface can include multiple classes in the module.

Design a clear interface and separate the interface, just allow the user to use the interface. The package implementation can force the user to use the interface.

[TOP | BOTTOM | Previous Section | Next Section]

[7.5] How does C be balanced between security and availability?

In C, the package is done by declaring the object as static by in the editing unit or module. This prevents other modules from accessing the static area. (By the way, this practice is abandoned: don't do this in C )

Unfortunately, this processing method does not support multiple instances of data because there is no direct support for a plurality of instances of static data for a module. If multiple instances are required in C, the programmer is generally used. But very unfortunate, the structure of C does not support packaging. This increases the difficulty of breaking between security (information hide) and availability (multi-instance).

In C , you can use classes to get multi-instances and encapsulation. The PUBLIC: section contains the interface of the class, which is usually composed of classes of the PUBLIC: member function and its friend function. The Private: and / or Protected: section contains the implementation of the class, and usually the data is here.

The final result is like "packaged structure". This is easy to make balance between security (information hide) and availability (multi-instance).

[TOP | BOTTOM | Previous Section | Next Section]

[7.6] How can I prevent other programmers from viewing my class's private part of the package?

Don't have to do this - package is for code, not to people.

As long as the code written by other programmers does not depend on their opinion, even if they look at your classes: and / or protected: part, it will not damage the package. In other words, the package does not prevent people from understanding the interior of the class. The package only prevents them from writing code on internal implementation. Your company does not have to maintain the payment maintenance costs you see by your eyes, but must pay maintenance costs to maintain your fingertips. As you know, if they write the code depending on the interface, it doesn't increase the maintenance cost.

In addition, this is rarely a problem. I would like to deliberately try to access the programmer of private parts of the class. My Recommendation In Such Cases Would Be To Change The Programmer, NOT The code "[James Kanze].

[TOP | BOTTOM | Previous Section | Next Section]

[7.7] Is the package a safety device?

Do not.

Package! = Safe.

The package should be prevented from being errors, not a spy.

[TOP | BOTTOM | Previous Section | Next Section]

[7.8] What is the difference between keyword struct and class? Struct's members are public, and the members of the class are private. Note: You should understand that your class member is public, private, or protected, not dependent on the default attribute.

Struct and Class are equivalent to other aspects.

Ok, clear technology is more than enough. From feelings, most developers have a big difference in class and structures. It feels that the structure is just a bunch of open internal storage, and the class is like a living and reliable social member, which has smart services, a firm package barrier and a good definition interface. Since most people think that there are very few methods in your class and have public data (this kind of thing exists in a well design!), You may use the struct keyword, otherwise, You should use the Class keyword.

[TOP | BOTTOM | Previous Section | Next Section]

E-mail the author [C FAQ Lite | Table of Contents | Subject Index | About The Author | © | Download Your Own Copy] Revised Apr 8, 2001

转载请注明原文地址:https://www.9cbs.com/read-2040.html

New Post(0)