ch3-Global variables and functions in separate files

Chapter_3     Exercise_3-7     OnTheFly Exercise_3-9     Exercise_3-8







CONTENTS:     Global1.cpp     Global2.cpp




Global1.cpp     TCP1, p. 159-160         download


// Demonstration of global variables
#include <iostream>
using std::cout;
using std::endl;

int globe; // Global variable defined here

void func(); // Declared here, defined in Global2.cpp

int main()
{
globe = 12;
cout << globe << endl; // 12
func(); // Modifies globe
cout << globe << endl; // 47

return 0;
}
/*
g++ -c Global1.cpp Global2.cpp // create object files
g++ -c Global*.cpp
g++ Global1.o Global2.o -o Global // link object files and create executable
g++ Global*.o -o Global
rm Global1.o Global2.o // clean (remove object files)
rm Global*.o
./Global // run program
Global
12
47
*/











Global2.cpp     TCP1, p. 160         download


extern int globe; // Access external global variable (defined in Global1.cpp)
// (The linker resolves the reference)
void func() // Function definition (used in Global1.cpp)
{
globe = 47;
}
/*
g++ -c Global2.cpp // create object file
*/









Chapter_3     Exercise_3-7     OnTheFly BACK_TO_TOP Exercise_3-9     Exercise_3-8



Comments

Popular posts from this blog

Contents