File1
#include <string>
namespace data {
// The next two variables have external linkage
int count; // will be initialized to 9 by default
float phi = 1.618f; // the divine proportion or golden ratio
// without the extern, the following would not be accessible
// from another file because they would have internal linkage
extern const double pi = 3.14159265;
extern const std::string days[] = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" };
}
File2
#include <iostream>
#include <string>
#include <iomanip>
namespace data {
//Declare external variables
extern float phi;
extern const double pi;
extern const std::string days[];
extern int count;
}
void main() {
std::cout << std::setprecision(3) << std::fixed;
std::cout << std::endl
<< "To 3 decimal places" << std::endl;
std::cout << " a circle with a diameter of phi has an area of"
<< data::pi * data::phi * data::phi /4
<< std::endl;
std::cout << "phi squared is " << data::phi * data::phi << std::endl;
std::cout << " in fact, phi+1 is also " << data::phi+1 << std::endl;
std::cout << "value of count is " << data::count << std::endl;
data::count += 3;
std::cout << "today is " << data::days[data::count] << std::endl;
}