添加名称名称空间和常量作用域
This commit is contained in:
parent
00407a1645
commit
5624ce6ddb
@ -38,4 +38,11 @@ add_executable(twotemps template/twotemps.cpp)
|
||||
add_executable(twotemp template/twotemp.cpp)
|
||||
add_executable(temptempover template/temptempover.cpp)
|
||||
add_executable(choices template/choices.cpp)
|
||||
add_executable(testtemplate template/testtemplate.cpp)
|
||||
add_executable(testtemplate template/testtemplate.cpp)
|
||||
add_executable(file1 namespacetest/file1.cpp)
|
||||
add_executable(file2 namespacetest/file2.cpp)
|
||||
add_executable(test10001 namespacetest/test10001.cpp)
|
||||
add_executable(auto namespacetest/auto.cpp)
|
||||
add_executable(external namespacetest/external.cpp)
|
||||
add_executable(support namespacetest/support.cpp)
|
||||
add_executable(ststic namespacetest/ststic.cpp)
|
36
base/namespacetest/auto.cpp
Normal file
36
base/namespacetest/auto.cpp
Normal file
@ -0,0 +1,36 @@
|
||||
//
|
||||
// Created by zhuyijun on 2021/8/16.
|
||||
//
|
||||
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
void oil(int x);
|
||||
/**
|
||||
* 变量的作用域
|
||||
* @return
|
||||
*/
|
||||
int main() {
|
||||
using namespace std;
|
||||
int texas = 31;
|
||||
int year = 2011;
|
||||
cout << "In main(), texas = " << texas << ", &texas = " << &texas << endl;
|
||||
oil(texas);
|
||||
cout << " In main(), texas = " << texas << " , &texas = " << &texas << endl;
|
||||
cout << "In main(), year = " << year << " , &year = " << &year << endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
void oil(int x) {
|
||||
using namespace std;
|
||||
int texas = 5;
|
||||
cout << "In oil(), texas = " << texas << ", &texas = " << &texas << endl;
|
||||
cout << "In oil(), x = " << x << ", &x = " << &x << endl;
|
||||
{
|
||||
int texas = 113;
|
||||
cout << "In block, texas = " << texas<< ", &texas = " << &texas << endl;
|
||||
cout << "In block,x = " << x << ", &x = " << &x << endl;
|
||||
}
|
||||
cout << "Post-block texas = " << texas << ", &texas = " << &texas << endl;
|
||||
}
|
@ -14,6 +14,6 @@ struct rect{
|
||||
double y;
|
||||
};
|
||||
|
||||
polarrect_to_polar(rect xypos);
|
||||
polar rect_to_polar(rect xypos);
|
||||
void show_polar(polar dapos);
|
||||
#endif //BASE_COORDIN_H
|
||||
|
36
base/namespacetest/external.cpp
Normal file
36
base/namespacetest/external.cpp
Normal file
@ -0,0 +1,36 @@
|
||||
//
|
||||
// Created by zhuyijun on 2021/8/16.
|
||||
//
|
||||
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
double warming = 0.3;
|
||||
|
||||
void update(double dt);
|
||||
|
||||
void local();
|
||||
|
||||
int main() {
|
||||
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";
|
||||
}
|
@ -9,6 +9,15 @@ using namespace std;
|
||||
|
||||
int main() {
|
||||
|
||||
rect rplace;
|
||||
polar pplace;
|
||||
|
||||
cout << "Enter the x and y values: ";
|
||||
while (cin >> rplace.x >> rplace.y) {
|
||||
pplace = rect_to_polar(rplace);
|
||||
show_polar(pplace);
|
||||
cout << "Next two numbers (q to quit)";
|
||||
}
|
||||
cout << "Bye! \n";
|
||||
return 0;
|
||||
}
|
||||
|
24
base/namespacetest/file2.cpp
Normal file
24
base/namespacetest/file2.cpp
Normal file
@ -0,0 +1,24 @@
|
||||
//
|
||||
// Created by zhuyijun on 2021/8/16.
|
||||
//
|
||||
|
||||
#include "coordin.h"
|
||||
#include <iostream>
|
||||
#include <cmath>
|
||||
|
||||
polar rect_to_polar(rect xypos) {
|
||||
using namespace std;
|
||||
polar answer;
|
||||
|
||||
answer.distance = sqrt(xypos.x * xypos.x + xypos.y * xypos.y);
|
||||
answer.angle = atan2(xypos.y, xypos.x);
|
||||
return answer;
|
||||
}
|
||||
|
||||
void show_polar(polar dapos) {
|
||||
using namespace std;
|
||||
const double Rad_to_deg = 57.29577951;
|
||||
cout << "distance = " << dapos.distance;
|
||||
cout << " , angle = " << dapos.angle * Rad_to_deg;
|
||||
cout << " degrees \n";
|
||||
}
|
40
base/namespacetest/ststic.cpp
Normal file
40
base/namespacetest/ststic.cpp
Normal file
@ -0,0 +1,40 @@
|
||||
//
|
||||
// Created by zhuyijun on 2021/8/16.
|
||||
//
|
||||
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
const int ArSize = 10;
|
||||
|
||||
void strcount(const char *str);
|
||||
|
||||
int main() {
|
||||
|
||||
char input[ArSize];
|
||||
char next;
|
||||
|
||||
cout << " Enter a line:\n";
|
||||
cin.get(input, ArSize);
|
||||
while (cin) {
|
||||
cin.get(next);
|
||||
while (next != '\n')
|
||||
cin.get(next);
|
||||
strcount(input);
|
||||
cout << "Enter next line (empty line to quit):\n";
|
||||
cin.get(input, ArSize);
|
||||
}
|
||||
cout << "Bye\n";
|
||||
return 0;
|
||||
}
|
||||
|
||||
void strcount(const char *str) {
|
||||
static int total = 0;
|
||||
int count = 0;
|
||||
cout << "\"" << str << "\" contains ";
|
||||
while (*str++)
|
||||
count++;
|
||||
total += count;
|
||||
cout << " characters \n";
|
||||
cout << total << " characters total\n";
|
||||
}
|
30
base/namespacetest/support.cpp
Normal file
30
base/namespacetest/support.cpp
Normal file
@ -0,0 +1,30 @@
|
||||
//
|
||||
// Created by zhuyijun on 2021/8/16.
|
||||
//
|
||||
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
extern double warming;
|
||||
|
||||
void update(double dt);
|
||||
|
||||
void local();
|
||||
|
||||
int main() {
|
||||
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";
|
||||
}
|
17
base/namespacetest/test10001.cpp
Normal file
17
base/namespacetest/test10001.cpp
Normal file
@ -0,0 +1,17 @@
|
||||
//
|
||||
// Created by zhuyijun on 2021/8/16.
|
||||
//
|
||||
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
int main() {
|
||||
int teledeli = 5;
|
||||
{
|
||||
cout << "Hello\n";
|
||||
int websight = -1;
|
||||
cout << websight << ' ';
|
||||
}
|
||||
cout << teledeli << endl;
|
||||
}
|
26
base/namespacetest/twofile1.cpp
Normal file
26
base/namespacetest/twofile1.cpp
Normal file
@ -0,0 +1,26 @@
|
||||
//
|
||||
// Created by zhuyijun on 2021/8/16.
|
||||
//
|
||||
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
int tom = 3;
|
||||
int dick = 30;
|
||||
static int harry = 300;
|
||||
|
||||
void remote_access();
|
||||
/**
|
||||
* 编译时请同时编译twofile1.cpp 和twofile2.cpp 命令如下 g++ .\twofile2.cpp .\twofile1.cpp -o test
|
||||
* @return int
|
||||
*/
|
||||
int main() {
|
||||
|
||||
using namespace std;
|
||||
cout << "main() reports the following address:\n";
|
||||
cout << &tom <<" = &tom, " << &dick <<" = &dick, ";
|
||||
cout << &harry <<" = &harry\n";
|
||||
remote_access();
|
||||
return 0;
|
||||
}
|
||||
|
18
base/namespacetest/twofile2.cpp
Normal file
18
base/namespacetest/twofile2.cpp
Normal file
@ -0,0 +1,18 @@
|
||||
//
|
||||
// Created by zhuyijun on 2021/8/16.
|
||||
//
|
||||
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
extern int tom;
|
||||
static int dick = 10;
|
||||
int harry = 200;
|
||||
|
||||
void remote_access() {
|
||||
using std::cout;
|
||||
cout << "remote_access() reports the fllowing addresses:\n";
|
||||
cout << &tom << " = &tom, " << &dick << " = &dick, ";
|
||||
cout << &harry << " = &harry\n";
|
||||
}
|
Loading…
Reference in New Issue
Block a user