I am confused about such concepts when I discussed with my friend.
My friend's opinions are
1) abstraction i开发者_C百科s about pure virtual function.
2) interface is not member functions, but interface is pure virtual functions.
I found that in C++ primer, interface are those operations the data type support, so member functions are interface.
My opinions are
1) abstraction is about speration of interface and implementation;
2) member functions are interfaces.
So could anybody clarify these concepts for me?
1) the difference among abstraction, abstract data type and abstract class.
2) the difference between interface and member functions.
3) the difference between abstraction and encapsulation.
I think your main problem is that you and your friend are using two different definitions of the word "interface", so you're both right in different ways.
You are using "interface" in the everyday sense of "a defined way to inter-operate with something", as in "the interface between my computer and my keyboard is USB" or "the interface between the vacuum and the wall power is an outlet." In that sense, yes, methods (even concrete ones) are interfaces, since they define a way to inter-operate with an object. That's not to say that this is not applicable to software -- it is the sense of "interface" used in the term Application Programming Interface (API).
Your friend is using "interface" in the more specific object oriented programming jargon sense of "a separately defined set of operations that a class can choose to guarantee that it will support". Here, the defining characteristic of an "interface" is that it has no implementation of its own. A class is supposed to support an interface by providing an implementation of the methods defined by the interface. Since C++ has no explicit concept of an interface in this sense, the equivalent construct is a class with only pure virtual functions (aka an Abstract Data Type).
"Abstraction", on the other hand, is about many things and again you are both right. Abstraction in a general sense means being able to focus on higher-level concepts rather than lower level details. Encapsulation is a type of abstraction because its purpose is to hide the implementation details of the methods of a class; the implementation can change without the class definition changing. Pure virtual functions ("interfaces" in the OO-jargon sense) are another type of abstraction because they can, if used properly, hide not only the implementation but also the true underlying object type; the type being used can change so long as both types implement the same interface.
The same terms can be used for different things, which is what is happening here.
"Abstract" in C++ means a method that doesn't have an implementation at all (you can't instantiate an object with Abstract members.)
"Abstraction" is simply the concept of "modeling". Modelling is making something complex look simpler by ignoring some details. In programming, you want to break operations and concepts up into components, and for each component, abstract away details about external components that don't affect the current component's operation.
A programming "Interface" is a way of implementing "Abstraction". Rather than have all the source code and internal operations of a component, you only see the operations that are relevent to how you're using the object. An "Interface" in C++ is implemented by marking all methods on a class as "abstract" (also called "pure virtual".) That's done by putting an '= 0' after the method declaration but before the semicolon. The method has to be marked 'virtual' for this to be legal.
In other words, an abstract C++ class is one that has at least one pure virtual method, and an interface is implemented in C++ by making all member functions pure virtual.
Encapsulation is a fuzzy term, but to me it means a technique of implementing abstraction. It means "information hiding". You're hiding the internal details of how an object performs its "contract". The Contract is expressed through interfaces, which to my mind is a more powerful form of abstraction. Any C++ class with protected or private members uses encapsulation, but a class implementing only pure virtual methods is describing a contract, promising to deliver certain services for which you need to know absolutely nothing about how they are implemented or about other services the same object may implement.
The same object may fill several contracts, and by exposing multiple, disjoint interfaces, it doesn't force clients to know about all of the auxilliary functions of the object. For example, an object may be able to tell you a bank account balance, and it also may be able to be serialized/deserialized to a database. You could just have one class with all of those operations exposed as member functions. I prefer to define two interfaces, 'IDatabaseSerializable' and 'IBankAccount', and put the appropriate operations in the appropriate interfaces and derive from both interfaces in my implementation class. Then clients that only care about bank balances see as little extra information as possible, and the database only sees the operations that it cares about.
Virtual member functions (not neccessarily pure) in combination with inheritance are one way to express the concept of interfaces in C++, i.e. interfaces and methods are orthogonal concepts.
Another approach to interfaces in C++ is with generic code (i.e. templates), where you often don't expect any concrete type - instead you expect the type to have certain member functions with a certain semantic - if it doesn't you will get a compile error.
Some people call this concepts and you talk of a type modelling a certain concept. Concepts are not formally supported in C++ though, but libraries like Boost.ConceptCheck do a pretty good job of providing a substitute.
Abstraction means that someone can use your code without knowing about implementation details. The thing that makes this complicated is that things that are implementation details in one context may not be in others. An abstract data type is a data type that cannot be instantiated and only describes attributes of its subtypes. An abstract class is just something that's an abstract data type and a class.
An interface can be implicitly defined by a set of member functions (though to be a useful abstraction these should at least be virtual so that there is more than one possible implementation of the interface). It can also be defined explicitly as a class with only pure virtual functions (in C++) or an
interface
(in Java, C# and D).Abstraction is when you don't have to know about the implementation details of something. Encapsulation is when you can't know about them. For example, a class that is otherwise well-designed from an OO perspective, but doesn't bother setting its member variables to
private
can still be a useful abstraction, but it is not encapsulated.
精彩评论