Simple example one: linkage
hello.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
#include <iostream>
#include <string>
using namespace std;
#include "greeting.hpp"
int main()
{
string myname;
cout << "What is your name? ";
getline( cin, myname );
salute( myname );
salute( "Séanaéd" );
return 0;
}
|
greeting.hpp
1 2 3 4 5 6 7 8 9
|
#ifndef GREETING_HPP
#define GREETING_HPP
#include <string>
void salute( const std::string& name );
// Gives a timely greeting to the named individual.
#endif
|
greeting.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
|
#include <ctime>
#include <iostream>
#include <string>
#include "greeting.hpp"
void salute( const std::string& name )
{
std::time_t timevalue = std::time( NULL );
struct std::tm* timedata = std::localtime( &timevalue );
if (timedata->tm_hour < 12)
std::cout << "Good morning ";
else if (timedata->tm_hour < 18)
std::cout << "Good afternoon ";
else if (timedata->tm_hour < 22)
std::cout << "Good evening ";
else
std::cout << "Good night ";
std::cout << name << "!\n";
}
|
Compile and link
hello.cpp and
greeting.cpp. Using the GCC, that is done with
g++ -Wall -o hello hello.cpp greeting.cpp |
Technically, you don't have to compile them both at the same time. You could do it at different times:
On Monday: compile the 'greeting' module only
g++ -Wall -c greeting.cpp |
On Friday: compile the main program only
On Saturday: link the two together
g++ -o hello hello.o greeting.o |
The reason that
hello.cpp can use things in
greeting.o is because
greeting.hpp tells it what is in
greeting.o.
When you run the program it will say hello to whomever you identify yourself as as is appropriate to the time of day. So if it were 3:00 pm, and I ran the program, it might look like:
D:\prog\hello> hello
What is your name? Dúthomhas
Good afternoon Dúthomhas!
Good afternoon Séanaéd!
D:\prog\hello>
|
Simple example two: recursion (Thanks
helios!)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
|
#include <iostream>
using namespace std;
bool odd( unsigned n );
int main()
{
int n;
cout << "Please enter a positive number> ";
cin >> n;
if (odd( n ))
cout << "That sure is an odd number!\n";
else
cout << "What a well-balanced number!\n";
return 0;
}
bool odd( unsigned n )
{
bool even( unsigned n );
switch (n)
{
case 0: return false;
case 1: return true;
default: return even( n -1 );
}
}
bool even( unsigned n )
{
switch (n)
{
case 0: return false;
case 2: return true;
default: return odd( n -1 );
}
}
|
In this example there is only one file, but the relative position of the functions are a bit wonky to illustrate a point.
odd() is
declared (aka
prototyped) before main(), so main() can use it there on line 12 without any grief. Notice how the prototype is in the global space. (Any function appearing after the prototype can use odd().)
The odd() function is mutually recursive with even(), but even() is neither declared nor defined before odd() is defined. Therefore, we prototype even() so that odd() can use it. This time, though, the prototype is in odd()'s local space, so any functions following odd() but preceding even() cannot use even() [without also prototyping it]. So on line 28 there odd() can use even() without any trouble.
Finally, even() can use odd() on line 38 because odd() is both declared and defined (definition implies declaration) before even().
When you compile and run it, try not to blow the stack with really huge numbers.
Hope this helps.