Final Touches: Multiple-File Programs, Inheritance, Templates Reasons For Multifile Programs
Final Touches: Multiple-File Programs, Inheritance, Templates 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
3 4
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