Sie sind auf Seite 1von 2

Final Touches: Multiple-File Reasons for Multifile Programs

Programs, Inheritance,
• Class Libraries
Templates – Libraries of functions: (in traditional language)
• ready-made functions for a wide variety of fields, e.g. statistics calculations,
• Multiple-File Programs or one for advanced memory management.
– Libraries of classes (in C++)
– header files • A class library can take over a greater portion of the programming burden. An
applications programmer, if the right class library is available, may find that only a
minimal amount of programming is necessary to create a final product. Also, as more
– implementation files and more class libraries are created, the chances of finding one that solves your
particular programming problem continues to increase.
– main-program file) – A class library usually includes two components: the
interface and the implementation. Let’s see what the
• Templates in C++ difference is.

1 2

Reasons for Multifile Programs Reasons for Multifile Programs


• Interface • Implementation
– Let’s say that the person who wrote a class library is called the class – On the other hand, the inner workings of the member functions of the various classes
developer, and the person who uses the library is called the don’t need to be known by the programmer. The class developers, like any other
programmer. To use a class library, the programmer needs to access software developers, don’t want to release source code if they can help it, since it
might be illegally modified or pirated. Member functions—except for short inline
various declarations, including class declarations. These declarations
functions—are therefore often distributed in object form, as .OBJ files or as library
can be thought of as the public part of the library and are usually (.LIB) files.
furnished in source-code form as a header file, with the .H extension.
• Re-use
This file is typically combined with the client’s source code using an
• Better data abstraction (data hiding)
#include statement.
• More manageability of large programs
• Organization and Conceptualization
– These declarations are called the interface because that’s what a user of
the class (the programmer) sees and interacts with. The programmer
need not be concerned with the other part of the library, the
implementation.

3 4

Creating a Multifile Program


• Suppose that you have purchased a prewritten class file
called THEIRS.OBJ. (A library file with the .LIB
extension is deal with in much the same way.) It probably
comes with a header file, say THEIRS.H. You have also
written your own program to use the classes in the library;
your source file is called MINE.CPP. Now you want to
combine these component files—THEIRS.OBJ,
THEIRS.H, and MINE.CPP—into a single executable
program.

5 6

1
Header Files
• Header Files The Multiple-File Approach
– The header file THEIRS.H is easily incorporated into your own source
file, MINE.CPP, with an #include statement: #include “THEIRS.H”
Quotes rather than angle brackets around the filename tell the compiler to • Have each major class as a separate
look first for the file in the current directory, rather than in the default
include directory.
program
• Directory – Re-use: The class can then be used by other
– Make sure all the component files, THEIRS.OBJ, THEIRS.H, and programs using the #include macro instead of
MINE.CPP, are in the same directory. In fact, you will probably want to the copy-&-paste approach
create a separate directory for the project, to avoid confusion. (This isn’t
strictly necessary, but it’s the simplest approach.)
• Put the class/function declarations in a
header (.h) file, and the class/function
implementations in another (.c or .cpp) file.
This achieves data hiding.
7 8

Example
• .Stack.h file: Stack.cpp file: Project1.cpp file:
What should Go into a .h File
#include "Stack.h"
class Stack { Stack::Stack(int cap){
public: top=0;
• Only declarations (i.e., info to the compiler)
#include <cstdlib>
typedef int dtype; capacity = (cap>0)?cap:100;
#include <iostream> • Nothing that requires storage
Stack(int cap=100); dataptr = new int[capacity];
#include "Stack.h” • Reason:
int getCapacity(); };
void push(dtype b); int Stack::getCapacity( ) {
using namespace std; – a header file gets included in several files of a project;
dtype pop(); return capacity;
// local stuff, if any – if storage for a variable (or for a code) is allocated
dtype peek(); };
bool isEmpty(); int Stack::dtype Stack::pop(){
int main(int argc, multiple times, you get a multiple-definition
private: assert(!isEmpty()); compilation error
char *argv[]){
dtype *dataptr; return dataptr[--top];
………
int top; };
}
• Advanced: One exception to this rule is static data
int capacity; // the implementation of (data local to a file), because the linker does not
}; // the remaining methods
........ allocate multiple instances to static data.
9 10

Das könnte Ihnen auch gefallen