11.07.2015 Views

tYSR20

tYSR20

tYSR20

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

292Part IV: InheritanceThe programmer can divide a single program into separate files known asmodules. These individual source files are compiled separately and then combinedduring the build process to generate a single program. Modules can thenbe allocated to separate groups known as namespaces.The process of combining separately compiled modules into a single executableis called linking.There are a number of reasons to divide programs into more manageablepieces. First, dividing a program into modules results in a higher level ofencapsulation. Classes wall off their internal members in order to provide acertain degree of safety. Programs can wall off functions to do the same thing.Encapsulation is one of the advantages of object-oriented programming.Second, it is easier to comprehend and, therefore, easier to write and debug aprogram that consists of a number of well-thought-out modules than a singlesource file full of all of the classes and functions that the program uses.Next comes reuse. I used the reuse argument to help sell object-based programming.It is extremely difficult to keep track of a single class reusedamong multiple programs when a separate copy of the class is kept in eachprogram. It is much better if a single class module is automatically sharedamong programs.Finally, there is the argument of time. A compiler such as Visual C++ or Dev-C++ doesn’t need very long to build the examples contained in this book usinga high-speed computer like yours. Commercial programs sometimes consist ofmillions of source lines of code. Rebuilding a program of that size can take morethan 24 hours. A programmer would not tolerate rebuilding a program like thatfor every single change. However, the majority of the time is spent compilingsource files that haven’t changed. It is much faster to recompile just thosemodules that have changed and then quickly link all modules together.Separate namespaces allow a further level of encapsulation. A namespaceshould consist of a set of modules that perform a single capability. For example,all of the mathematical functions might be combined into a Math namespace.This lesson builds a simplistic program, called SeparateModules, that consistsof a Student class, a GraduateStudent subclass, and a main() moduleto test both.Dividing the program — StudentYou begin by deciding what the logical divisions of SeparateModules shouldbe. First, you notice that Student is an entity of its own. It does not depend

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!