//以前学习过C语言,这是学习C++的笔记.所有的代码在dev c++ 5.15 下测试
/*
1.C++头文件不必是.h结尾,C语言中的标准库头文件如math.h , stdio.h 在
C++中被命名为cmath,cstdio .
*/
// 2. 除了C的多行注释,C++可以使用单行注释,以两个斜杠开始
/*
3. 为了防止名字同名冲突,C++引入了名字空间(namespace)
通过::运算符限定某个名字属于哪个名字空间
*/
#include <iostream>
#include <cmath>
#include <cstdio>
namespace first {
int a = 1;
void display() {
std::cout << "a=" << a << " in namespace first" << std::endl;
}
}
/*
cout 和 endl 前要加std:: , 若不加编译器会报错,
若要省略,则要在前面恰当位置加上以下一行:
using namespace std;
*/
using namespace std;
namespace second {
double a = 2.0;
void display() {
cout << "a=" << a << " in namespace second " << endl;
}
}
int main(int argc, char **argv) {
double a = 1.2;
a = sin(a);
printf("%lf", a);
first::a = 50; //注意这里,不加first:: 会发生什么事情呢?
first::display();
second::display();
return 0;
}
学点C++ 笔记1:头文件、注释、名字空间
最新推荐文章于 2024-06-27 20:00:00 发布