因为最近由于工作的原因,主要的编程语言从用了10多年的C切换到了C++,在实际应用中才发现,有些地方还是很困难的,所以也是为了自己的积累,在这里分享一些从C转变到C++的一些注意点。
1.头文件的包含可以不再使用.h扩展名(当然如果你使用扩展名也无可厚非),如果你喜欢使用C的库,可以用c.作为库的开头。
而且有了命名空间的概念 namespace std,可以保证一些命名能够在全局不冲突。
using namespace std; #include <iostream> // This is a key C++ library #include < cmath> // The standard C library math.h int main () { double a; a = 1.2; a = sin (a); cout << a << endl; return 0; }the output : 0.932039
2.可以使用//来作为注释,窃以为这个还是分方便的。在 C99 和 ANSI C 2000也可以使用了
using namespace std; // Using the standard library namespace. #include <iostream> // The iostream library is often used. int main () // The program's main routine. { double a; // Declaration of variable a. a = 456.47; a = a + a * 21.5 / 100; // A calculation. cout << a << endl; // Display the content of a. return 0; // Program end. }
Output |
554.611 |
3.可以使用标准输入输出流来进行打印cout<<和键盘输入cin>>
using namespace std; #include <iostream> int main() { int a; // a is an integer variable char s [100]; // s points to a string of max 99 characters cout << "This is a sample program." << endl; cout << endl; // Just a line feed (end of line) cout << "Type your age : "; cin >> a; cout << "Type your name: "; cin >> s; cout << endl; cout << "Hello " << s << " you're " << a << " old." << endl; cout << endl << endl << "Bye!" << endl; return 0; }
Output |
This is a sample program. Type your age : 12 Type your name: Edmond Hello Edmond you're 12 old. Bye! |
using namespace std; #include <iostream> int main () { double a; cout << "Hello, this is a test program." << endl; cout << "Type parameter a: "; cin >> a; a = (a + 1) / 2; double c; c = a * 5 + 1; cout << "c contains : " << c << endl; int i, j; i = 0; j = i + 1; cout << "j contains : " << j << endl; return 0; }Hello, this is a test program.
Type parameter a: 7
c contains : 21
j contains : 1
我们可以利用这个特性来使得我们的程序可读性更高,像C语言一样,我们可以在某个程序段内声明并且使用某个变量,同时,在该程序段外,这个变量不会产生任何影响。
using namespace std; #include <iostream> int main () { double a; cout << "Type a number: "; cin >> a; {
int a = 1;
a = a * 10 + 4;
cout << "Local number: " << a << endl;
} cout << "You typed: " << a << endl; return 0; }
Output |
Type a number: 9 Local number: 14 You typed: 9 |
5.变量可以定义为另外一个变量和其他变量/常量的计算结果
using namespace std; #include <iostream> int main () { double a = 12 * 3.25; double b = a + 1.112; cout << "a contains: " << a << endl; cout << "b contains: " << b << endl; a = a * 2 + b; double c = a + b * a; cout << "c contains: " << c << endl; return 0; }
Output |
a contains: 39 b contains: 40.112 c contains: 4855.82 |
6.可以在循环条件检查同时定义变量,使得程序更简洁。
using namespace std; #include <iostream> int main () { int i; // Simple declaration of i i = 487; for (int i = 0; i < 4; i++) // Local declaration of i { cout << i << endl; // This outputs 0, 1, 2 and 3 } cout << i << endl; // This outputs 487 return 0; }
Output |
0 1 2 3 487 |
如果你想在循环结束之后使用在循环条件检测时候定义的变量,这是不正确的行为。
using namespace std; #include <iostream> int main () { for (int i = 0; i < 4; i++) { cout << i << endl; } cout << i << endl; // Bad practice! i += 5; // Bad practice! cout << i << endl; // Bad practice! return 0; }
Gnu C++ compiler complain |
t.cpp: In function ‘int main()’: t.cpp:12: error: name lookup of ‘i’ changed for new ISO ‘for’ scoping t.cpp:7: error: using obsolete binding at ‘i’ |
7.全局变量也可以在函数内部被访问,即时我们在函数内部已经有了同样的变量声明。
using namespace std;
#include <iostream>
double a = 128;
int main ()
{
double a = 256;
cout << "Local a: " << a << endl;
cout << "Global a: " << ::a << endl;
return 0;
}
Output |
Local a: 256 Global a: 128 |
8.使用引用,要注意,引用的变量如果修改是要改变被引用的变量的。
如果你过去使用的是指针而没有使用过引用,像下面这样是不是会更有利于你的理解:
double &b = a ----> double *b = &a,所有使用b的地方都用*b来代替。
引用b在声明之后是不能被改变的,你不能像&b=c这样写,所有的b, &b, a其实都是一个值。
using namespace std; #include <iostream> int main () { double a = 3.1415927; double &b = a; // b is a b = 89; cout << "a contains: " << a << endl; // Displays 89. return 0; }
outputa contains: 89
using namespace std;
#include <iostream>
void change (double &r, double s)
{
r = 100;
s = 200;
}
int main ()
{
double k, m;
k = 3;
m = 4;
change (k, m);
cout << k << ", " << m << endl; // Displays 100, 4.
return 0;
}
Output |
100, 4 |
上面的程序如果用C来写.....
using namespace std;
#include <iostream>
void change (double *r, double s)
{
*r = 100;
s = 200;
}
int main ()
{
double k, m;
k = 3;
m = 4;
change (&k, m);
cout << k << ", " << m << endl; // Displays 100, 4.
return 0;
}
我们经常用引用来让函数返回一个值。
using namespace std; #include <iostream> double &biggest (double &r, double &s) { if (r > s) return r; else return s; } int main () { double k = 3; double m = 7; cout << "k: " << k << endl; // Displays 3 cout << "m: " << m << endl; // Displays 7 cout << endl; biggest (k, m) = 10; cout << "k: " << k << endl; // Displays 3 cout << "m: " << m << endl; // Displays 10 cout << endl; biggest (k, m) ++; cout << "k: " << k << endl; // Displays 3 cout << "m: " << m << endl; // Displays 11 cout << endl; return 0; }
9.我们可以声明命名空间,如果想使用命名空间里面的变量用::
using namespace std; #include <iostream> #include <cmath> namespace first { int a; int b; } namespace second { double a; double b; } int main () { first::a = 2; first::b = 5; second::a = 6.453; second::b = 4.1e4; cout << first::a + second::a << endl; cout << first::b + second::b << endl; return 0; }
Output |
8.453 41005 |
10.用最简单的代码来实现函数,最好不要包括For循环之类,我们可以是用inline,这样用的好处是程序可以跑得更快
using namespace std; #include <iostream> #include <cmath> inline double hypothenuse (double a, double b)
{
return sqrt (a * a + b * b);
} int main () { double k = 6, m = 9; // Next two lines produce exactly the same code: cout << hypothenuse (k, m) << endl; cout << sqrt (k * k + m * m) << endl; return 0; }