[   ^ to index...   |   <-- previous   |   next -->   ]

Multi-file programs: a concrete example

All this is rather abstract. Here's a program that demonstrates a simple math package:

main.cpp

#include <iostream.h> #include "mymath.hpp" int main(void) { cout << gcd(1122, 1265) << endl; return 0; }

mymath.hpp

// Function to compute a greatest common // divisor of two integers. int gcd(int a, int b); // ... other math functions here

mymath.cpp

int gcd(int a, int b) { // Uses Euclid's algorithm to compute the // greatest common denominator of a and b int larger, smaller; if (a > b) { larger = a; smaller = b; } else { larger = b; smaller = a; } int remainder = larger % smaller; if (remainder == 0) { return smaller; } else { return gcd(smaller, remainder); } }
Last modified: Wed Jun 21 20:33:28 PDT 2000