Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
677 views
in Technique[技术] by (71.8m points)

terminal - Is it reasonable to consider the interface in C++ is for the communication between "the data" and "the program"?

On page 557, the book "C++ Primer Plus by Stephen Prata" says

OOP emphasizes how a program represents data.The first step toward solving a programming problem by using the OOP approach is to describe the data in terms of its interface with the program, specifying how the data is used. Next, you need to design a class that implements the interface.Typically, private data members store the information, whereas public member functions,also called methods, provide the only access to the data.The class combines data and methods into one unit,and the private aspect accomplishes data hiding.

Does it refer to "the data" there, which means the interface is for the communication between "the data" and "the program"?

question from:https://stackoverflow.com/questions/65949048/is-it-reasonable-to-consider-the-interface-in-c-is-for-the-communication-betwe

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Yes, "its" means "the data's interface" in that context.

Another way to look at it is an object is intermediate between data local to a function, and global to the entire program. Sometimes it's convenient for a group of functions to share data, and so those functions and data are grouped into an object. To ensure the consistency of the data, no other functions should have access to it, so the data is private, and all access from other code needs to be through the objects member functions, and that becomes the interface between the data and the rest of the program.

All the regular functions of an object should read or modify the data, or there's no point in putting them in the same object. Static methods are an exception, as they are pure functions where the output is produced only from the input. They should relate to the inputs or outputs of the other functions in some way to make sense, but I wouldn't count them as part of the interface.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...