標籤:main bsp 函數名 識別 void 參數 聲明 nbsp auto
噹噹噹噹!
時間過的真的好快,C語言已經學完了,開始學c++了,部落格也沒有堅持寫,新階段新開始嘛,希望能按時寫奧!
範圍:不可以在同一個範圍下聲明兩個相同名字的變數
1 #include <iostream> 2 using namespace std; 3 int age = 300;//注釋掉這行 4 namespace Person 5 { 6 int age = 200; 7 } 8 namespace Bird 9 {10 int age = 100;11 int sex = 101;12 }13 using namespace Bird;14 int main()15 {16 cout<<::age<<endl;//查看這行變化 全域變數優先順序高17 cout<<Person::age<<endl;18 cout<<Bird::age<<endl;19 cout<<::sex<<endl;20 }
1 #include <iostream> 2 using namespace std; 3 int main() 4 { 5 int *p = new int; 6 *p = 10; 7 cout<<*p<<endl; 8 delete p; 9 p = 0;10 11 int *arr = new int[10];12 arr[0] = 0;13 arr[1] = 1;14 cout<<arr[0]<<" "<<arr[1]<<endl;15 cout<<arr<<endl;16 delete[] arr;//delete 數組要加[]17 //cout<<arr<<endl;18 //delete之後指標還指向地址,但該空間已經不屬於我,不要對它進行操作!19 arr = 0;20 21 int *a = new int(10);//將a指向的int初始化為1022 cout<<*a<<endl;23 delete a;24 a = 0;25 }
1 #include <iostream> 2 using namespace std; 3 4 void Show(int a ,int b , int c = 2, int d = 3); 5 int main() 6 { 7 Show(100,200); 8 int arr[10] = {0,1,2,3,4,5,6,7,8,9}; 9 }10 //不能再函數實現中加參數,如果函式宣告中已存在則重複定義,如果沒有則格式錯誤11 void Show(int a ,int b , int c , int d )12 {13 cout << a << " " << b <<endl;14 cout << c << " " << d <<endl;15 }
1 for(auto i : arr)//auto可自動識別arr中類型2 {3 cout<< i << " ";4 }
函數名相同,函數參數個數或類型不同
c++基礎文法