C++ Programming/Scope/Examples
Appearance
Scope
[edit | edit source]Confusing Scope Program
[edit | edit source]
// Confusing Scope Program
#include <iostream>
using namespace std;
int i = 5; /* The first version of the variable 'i' is now in scope */
void p(){
int i = -1; /* A ''new'' variable, also called 'i' has come into existence. The 'i' declared above is now out of scope,*/
i = i + 1;
cout << i << ' '; /* so this ''local'' 'i' will print out the value 0.*/
} /* The newest variable 'i' is now out of scope. The first variable, also called 'i', is in scope again now.*/
int main(){
cout << i << ' '; /* The first variable 'i' is still in scope here so a ''5'' will be output here.*/
char ch;
int i = 6; /* A ''new'' variable, also called 'i' has come into existence. The first variable 'i' is now out of scope again,*/
i = i + 1;
p();
cout << i << endl; /* so this line will print out a ''7''.*/
return 0;
} /* End of program: all variables are, of course, now out of scope.*/
Average Program
[edit | edit source]
// Program Average
#include <iostream>
using namespace std;
float a[10]; /* a is now in scope.*/
int length; /* length is now in scope.*/
float average(){
float result = 0.0; /* result is now in scope.*/
for(int i = 0; i < length; i++){ /* i is now in scope.*/
result += a[i];
} /* i is now out of scope.*/
return result/length;
} /* result is now out of scope.*/
int main(){
length = 0;
cout << "enter a number for each of the next 10 lines" << endl;
while( length != 10 )
{ cin >> a[length++]; }
float av = average(); /* av is now in scope.*/
cout << endl << "average: " << av << endl;
return 0;
} /* All variables now out of scope.*/
// Program Average rewritten using a class
#include <iostream>
using namespace std;
class StatisticsPackage{
private:
float aa[20]; /* aa scope start*/
int length; /* length scope start*/
public:
float average(){
float result = 0.0; /* result scope start*/
for(int i = 0; i < length; ++i) /* i scope start*/
result += aa[i];
return result/length;
} /* result and i scope end*/
void get_data(){
length = 0;
while(cin >> aa[length++]);
--length;
}
}; /* aa and length scope end*/
int main(){
StatisticsPackage sp; /* aa and length lifetimes start */
sp.get_data();
float av = sp.average(); /* av scope start*/
cout << av << endl;
return 0;
} /* av scope end*/
Complicated Scope Program
[edit | edit source]
// Complicated Scope Program
#include <iostream>
using namespace std; /* outermost level of scope starts here */
int i;
int main(){ /* next level of scope starts here */
int i;
i = 5;
{ /* next level of scope starts here */
int j,i;
j = 1;
i = 0;
{ /* innermost level of scope of this program starts here */
int k, i;
i = -1;
j = 6;
k = 2;
} /* innermost level of scope of this program ends here */
cout << j << ' ';
} /* next level of scope ends here */
cout << i << endl;
return 0;
} /* next and outermost levels of scope end here */
// Complicated Scope Program, variation 1
#include <iostream>
using namespace std; /* outermost level of scope starts here */
int i;
int main(){ /* next level of scope starts here */
int i;
i = 5;
while(i != 5) { /* next level of scope starts here */
int j,i;
j = 1;
i = 0;
switch (i) { /* next level of scope starts here */
int i;
case 1:
if (i != 4) { /* innermost level of scope of this program starts here */
int k, i;
i = -1;
j = 6;
k = 2;
} /* innermost level of scope of this program ends here */
break;
case 2:
j = 5;
break;
} /* next level of scope ends here */
cout << j << ' ';
} /* next level of scope ends here */
cout << i << endl;
return 0;
} /* next and outermost levels of scope end here */
// Complicated Scope Program, variation 2
#include <iostream>
using namespace std; /* outermost level of scope starts here */
int i;
int main(){ /* next level of scope starts here */
int i;
i = 5;
for( /* next level of scope starts here */
int i = 1;
i<10 && cout << i << ' ';
++i )
{ /* next level of scope starts here */
int i = -1;
cout << i << ' ';
} /* two levels of scope end here*/
cout << i << endl;
return 0;
} /* next and outermost levels of scope end here */
Namespaces Program
[edit | edit source]
// Namespaces Program, an example to illustrate the use of namespaces
#include <iostream>
namespace first {
int first1;
int x;
}
namespace second {
int second1;
int x;
}
namespace first {
int first2;
}
int main(){
//first1 = 1;
first::first1 = 1;
using namespace first;
first1 = 1;
x = 1;
second::x = 1;
using namespace second;
//x = 1;
first::x = 1;
second::x = 1;
first2 = 1;
//cout << 'X';
std::cout << 'X';
using namespace std;
cout << 'X';
return 0;
}