c++基礎文法

來源:互聯網
上載者:User

標籤: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 }
  • new delete
 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 }
  • for迴圈

1 for(auto i : arr)//auto可自動識別arr中類型2 {3     cout<< i << " ";4 }
  •  函數重載

函數名相同,函數參數個數或類型不同

c++基礎文法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.