C++基礎學習(04PM)

1.有默認值的形參必須靠右放

2.returne 數據;執行就是將return後面的數據複製一份回來

3.內斂函數:在編譯時把代碼在調用的地方插入一份。效率要高一點,但內聯函數要簡短

4.遞歸思路:把規模變小,直接解決簡單問題。

相關代碼:

#include <iostream>
using namespace std;

void func(int n)
{
 ++n;
}

void const_func(const int n)
{
// ++n; buxunxugaibian n de zhi
 cout << "n=" << n << endl;

}

int main()
{
   int n=100;
   func(n);
   cout << "n=" << n << endl;
   const_func(n);

 return 0;
}

 

5.一定要先聲明後使用

6.一個程序放在多個文件中:將每個程序簡一個目錄,只有一個main函數

   每個文件都要先聲明後使用。

  將聲明單獨列出來叫頭文件

  將定義寫在 一個文件中,這個文件叫做實現文件

   把main函數寫成一個文件叫做主文件

   每個函數都可以獨立編譯,

   在頭文件中,爲了避免重複定義,方便編譯器編譯(第二次編譯的時候條件已經不成立了,就不再編譯了),將規範寫這三條語句:

   #ifndef name //如果沒有定義name

   #define name //則定義這個name

   #endif 

 相關代碼:

  頭文件:func.h

   #ifndef  _FUNC_H_

   #define _FUNC_H_

//名字的命名方法 _大寫字母文件名_H_

   void func(int n);

   void const_func(int n);

//空一行必避免錯誤警告還好看

  #endif

 實現文件:func.cc

 #include <iostream>

using namespace std;

 

void fun(int n)

{

 ++n;

 cout << "n=" << n << endl;

}

 

void const_fun(int n)

{

cout << "n=" << n << endl;

}

 

主函數:

#include <iostream>

using namespace std;

 

int main()

{

  int n=0;

  int m=10;

 const_fun(n);

}

 

 

發佈了147 篇原創文章 · 獲贊 20 · 訪問量 19萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章