-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexternal.cpp
45 lines (40 loc) · 1.02 KB
/
external.cpp
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
41
42
43
44
45
/*
In the C++ language, we have three kinds of approaches to define
static variables.
1. int global = 1000; // static duration, external linkage
2. static int one_file = 50; // static duration, internal linkage
3. void funct1(int n)
{
static int count = 0; // static duration, no linkage
int llama = 0;
}
*/
// external.cpp -- external variables
#include <iostream>
using namespace std;
double warming = 0.3;
void update(double dt);
void local();
int main() // uses global variable
{
cout << "Global warming is " << warming << " degrees.\n";
update(0.1);
cout << "Global warming is " << warming << " degrees.\n";
local();
cout << "Global warming is " << warming << " degrees.\n";
return 0;
}
void update(double dt)
{
extern double warming;
warming += dt;
cout << "Updating global warming to " << warming;
cout << " degrees.\n";
}
void local()
{
double warming = 0.8;
cout << "Local warming = " << warming << " degrees.\n";
cout << "But global warming = " << ::warming;
cout << " degrees.\n";
}