tailieunhanh - A Complete Guide to Programming in C++ part 68

A Complete Guide to Programming in C++ part 68. This book provides both novice and experienced programmers with a comprehensive resource manual for the C++ programming language. Readers gain experience in all aspects of programming, from elementary language concepts to professional software development, with in depth coverage of all the language elements en route. These elements are carefully ordered to help the reader create useful programs every step of the way. | PERSISTENCE OF POLYMORPHIC OBJECTS 649 Storing Polymorphic Objects Imagine you want to make the objects of a polymorphic class hierarchy persistent that is store them in a file. You need to ensure that an object can be reconstructed precisely when it is read. This gives rise to the fact that objects in polymorphic class hierarchies contain virtual methods. So it is not simply a case of making the data members of an object into records and writing them to a file. NOTE 1. You must write both the type and the data members of the object to a file. 2. If the objects contain dynamic members you must save the referenced objects themselves along with information on the object type. To allow the class to assume control over object storage you need methods that allow the object to write its own data members to a file. The methods can have a virtual definition within the class hierarchy. Thus if pointers are used to reference objects the appropriate read write operation for each object will be called. Storing Objects of the Account Hierarchy The opposite page shows the Account class with which you should already be familiar. Virtual file I O methods have now been added. The implementation of the read and write methods was discussed earlier in Chapter 18 Fundamentals of File Input and Output and is unchanged. The derived classes DepAcc and SavAcc also contain definitions of the read and write methods that read only their own objects and write them to files. The implementation first calls the appropriate base class method. If no errors occur it is simply a question of transferring the additional data members of the derived class to or from a file. At present no type information will be written to file or read from file. This task will be performed by a special class whose features are used for file management. The following section contains more details on this topic. 650 CHAPTER 29 MORE ABOUT FILES PERSISTENCE OF POLYMORPHIC OBJECTS CONTINUED continued Add definition

TỪ KHÓA LIÊN QUAN