tailieunhanh - Thinking in C plus plus (P15)

Tham khảo tài liệu 'thinking in c plus plus (p15)', công nghệ thông tin, kỹ thuật lập trình phục vụ nhu cầu học tập, nghiên cứu và làm việc hiệu quả | Instruments to create a common interface for all of the classes derived from it. The only reason to establish the common interface is so it can be expressed differently for each different subtype. It creates a basic form that determines what s in common with all of the derived classes - nothing else. So Instruments an appropriate candidate to be an abstract class. You create an abstract class when you only want to manipulate a set of classes through a common interface but the common interface doesn t need to have an implementation or at least a full implementation . If you have a concept like Instrumentthat works as an abstract class objects of that class almost always have no meaning. That is Instruments meant to express only the interface and not a 680 Thinking in C www. BruceEckel .com particular implementation so creating an object that is only an Instrumentmakes no sense and you ll probably want to prevent the user from doing it. This can be accomplished by making all the virtual functions in Instrumentprint error messages but that delays the appearance of the error information until runtime and it requires reliable exhaustive testing on the part of the user. It is much better to catch the problem at compile time. Here is the syntax used for a pure virtual declaration virtual void f 0 By doing this you tell the compiler to reserve a slot for a function in the VTABLE but not to put an address in that particular slot. Even if only one function in a class is declared as pure virtual the VTABLE is incomplete. If the VTABLE for a class is incomplete what is the compiler supposed to do when someone tries to make an object of that class It cannot safely create an object of an abstract class so you get an error message from the compiler. Thus the compiler guarantees the purity of the abstract class. By making a class abstract you ensure that the client programmer cannot misuse it. Here s to use pure virtual functions. Because the class has .