CS201 Introduction to Programming GDB Solution Fall 2013

Does separation of interface from implementation within classes provide advantage?  If yes, then how.

Note: Keep you comment brief and to the point, GDB will not be accepted through email in any case.


Advantages of seperating interface/implementation?

I think there are two different meanings of “separating interface from implementation” going on here.

One meaning is header files vs. cpp files. The reason for doing this is that if you fix a bug in the implementation part, only that .cpp file needs to be recompiled and none others. If the implementation is in the header file instead, you have to recompile every cpp file that includes that header file either directly or indirectly. In a large project, such a change could end up causing a recompile of hundreds or thousands or more source files which might take minutes or hours, instead of compiling a single source file which would take seconds.

The second meaning is from an object-oriented standpoint. Separating interface from implementation there means that the methods you provide in your object should not expose the internal implementation details of the object. The reason for this is because if you change the internal implementation of the object, you end up having to change the interface as well. Once you change the interface, you have to change all the code that uses that interface. This type of change could then ripple through the entire project.

Simple example of the latter.

Today, your Person object stores the phone number as an int, because you only need 7-digit phone numbers. So Person::getPhoneNumber() returns an int, and all the code that calls getPhoneNumber() stores the result in ints. Your interface — getPhoneNumber() is now tied to an implementation detail — that a phone number is stored internally as an int. Tomorrow, you need to support international phone numbers. Suddenly, int isn’t good enough any more. So you’re forced to change the phoneNum data member to a long long. Now getPhoneNumber() has to change and every place in the code where getPhoneNumber() is called has to change to store the result in a long long. And then all the functions that get called with a phone number as parameter have to change from taking an int to taking a long long. Etc, etc