CSC 418 Lecture 3 Notes
CSC 418 Lecture 3 Notes
IMPLEMENTATION
A key principle that is part of both the procedural and object-oriented paradigms is data
abstraction: the separation of the interface of a data type/class from its implementation.
However, the mechanisms for doing this in C++ support a stronger separation than those in Java
do.
C++
C++ allows the interface of a class to be declared in a separate file from its
implementation.
It is common practice to put the declaration of a class in a separate header file. (i.e, as an
aside -but an important point for people used to Java to note. The declaration of a C++
class always ends with a semicolon. Forgetting this can lead to really strange compiler
errors!)
Client files will use a #include directive to include the header file. When a header file is
included, the effect is the same as if the text of the header file were physically present in
The implementation goes in an implementation file whose name is typically the same as
that of the header, but with a “.cc” suffix. The implementation file also “#include”’s the
o Note that a header (.h) file can (and often does) include declarations for
multiple related classes, in which case the implementation (.cc) file will
this is not normally considered good practice. In particular, if the implementation code is
placed in the class declaration, then every time a source file includes the declaration, the
Java
Java does not really support the kind of separation of interface and implementation
Each publicly accessible Java class must be placed in its own source (.java) file. This
source file includes both the declaration and the full implementation of the class.
When a java source file is compiled, the resulting .class file contains a representation of
Client classes will import the class. This causes the compiler to extract the declaration
The java doc program does allow the interface portion of this file to be extracted to a
separate html documentation file; but this is only useful to a human reader, not client
code.