從C到C++要注意的33件事(0)

因爲最近由於工作的原因,主要的編程語言從用了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!
4.變量可以在程序的任何地方聲明並且馬上使用.


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;
}
output
a 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; }

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章