1. 程式人生 > >5.1-day01-C++語言語法基礎

5.1-day01-C++語言語法基礎

bank.cpp
   
  1. #include <iostream>
  2. using namespace std;
  3. //namespace {
  4. void print (int money) {
  5. cout << money << endl;
  6. }
  7. //}
  8. // 農行名字空間
  9. namespace abc {
  10. int balance = 0;
  11. void save (int money) {
  12. balance += money;
  13. }
  14. void draw (int money) {
  15. balance -= money;
  16. }
  17. }
  18. namespace abc {
  19. void salary (int money) {
  20. balance += money;
  21. }
  22. void print (int money) {
  23. cout << "農行:";
  24. ::print (money);
  25. }
  26. }
  27. // 建行名字空間
  28. namespace ccb {
  29. int balance = 0;
  30. void save (int money) {
  31. balance
    += money;
  32. }
  33. void draw (int money) {
  34. balance -= money;
  35. }
  36. void salary (int money) {
  37. balance += money;
  38. }
  39. }
  40. int main (void) {
  41. using namespace abc; // 名字空間指令
  42. save (5000);
  43. cout << "農行:" << balance << endl;
  44. draw (3000);
  45. cout <<
    "農行:" << balance << endl;
  46. ccb::save (8000);
  47. cout << "建行:" << ccb::balance << endl;
  48. ccb::draw (5000);
  49. cout << "建行:" << ccb::balance << endl;
  50. using ccb::salary; // 名字空間宣告
  51. // using abc::salary;
  52. salary (6000);
  53. cout << "建行:" << ccb::balance << endl;
  54. abc::print (abc::balance);
  55. return 0;
  56. }
 先進行編譯,然後再取別名執行檔案; g++ bank.cpp g++ -o bank bank.cpp ./bank

 std:: 名字空間: 作用域限定運算子,相當於:“的”字元。

bool.cpp
   
  1. #include <iostream>
  2. using namespace std;
  3. void print (bool sex) {
  4. if (sex)
  5. cout << "男生" << endl;
  6. else
  7. cout << "女生" << endl;
  8. }
  9. int main (void) {
  10. bool b = true;
  11. cout << sizeof (b) << endl;
  12. cout << b << endl;
  13. b = false;
  14. cout << b << endl;
  15. b = 3.14;
  16. cout << b << endl;
  17. char* p = NULL;
  18. b = p;
  19. cout << b << endl;
  20. cout << boolalpha << b << endl;
  21. bool sex;
  22. sex = 3;
  23. print (sex);
  24. return 0;
  25. }
  ----------------------------------------------------- 因為,char *p = null, b = p; 此時,boolalpha = false; boolalpha以字元顯示方式進行格式控制;
 
因為b是bool型別的,b = 3.14 , cout << b << endl; 後的值為1. --------------------------------------------------  sex  = 3  是bool型別的,猜測sex是True,男生”; -------------------------------- 驗證:    ---------------------------- sizeof(空結構)-> 1

cfunc.c
   
  1. void foo (int a) {}

defarg.cpp
   
  1. #include <iostream>
  2. using namespace std;
  3. void foo (int a = 10, double b = 0.01,
  4. const char* c = "tarena");
  5. void foo (void) {}
  6. void bar (int) {
  7. cout << "bar(int)" << endl;
  8. }
  9. void bar (int, double) {
  10. cout << "bar(int,double)" << endl;
  11. }
  12. int main (void) {
  13. foo (1, 3.14, "hello");
  14. foo (1, 3.14);
  15. foo (1);
  16. // foo (); // 歧義
  17. bar (100);
  18. bar (100, 12.34);
  19. return 0;
  20. }
  21. void foo (int a /* = 10 */, double b /* = 0.01 */,
  22. const char* c /* = "tarena" */) {
  23. cout << a << ' ' << b << ' ' << c << endl;
  24. }
 -----------------------    過載:在同一作用域中,函式名相同,引數表不同的函式,構成過載關係
 int foo (int a,  double b) {}   int foo (int b, double a)   函式名相同,引數名相同的不構成過載關係;   關注的是引數表的型別,不關心其名字;
過載解析: (1)最優匹配原則; (2)最小工作量原則;
函式名標識的是函式的地址,C++中函式名 可以重複,但是地址不一樣。 const char * c= "tarena" 表示預設值;預設值一律靠右; 預設引數放在宣告語句當中 ,宣告是給編譯器看的;

過載:需要用同一個函式(函式名是死的)來實現兩個不同的功能;  v1 : void decode (int arg) {...}  v2:  void decode (void)  {} ---------------------------------------------
  兩個不同 函式用同一個名;  只有型別而沒有名字的形參,渭之“啞元”。  佔著位置而不起任何作用; -------------------------------------------------- gcc -std=c++  0x 使用2011標準去編譯;
g++ 1.cpp -std=c++ 0x -------------------------------------------- (1)如果呼叫一個函式時,沒有提供實參,那麼對應的形參就取預設值; (2)如果一個引數帶有預設值,那麼它後面的所有引數都必須都帶有預設值; (3)如果一個函式宣告和定義分開,那麼預設引數只能放在宣告中; (4)避免和過載發生歧義; (5)只有型別而沒有名字的形參,謂之啞元;
enum.cpp
   
  1. #include <iostream>
  2. using namespace std;
  3. int main (void) {
  4. enum E {a, b, c};
  5. E e;
  6. e = a;
  7. e = b;
  8. e = c;
  9. // e = 1000;
  10. // e = 1;
  11. return 0;
  12. }
 在這種 情況下,程式可以順利進行編譯;
   在這種情況下,編譯出錯。  列舉是 一種獨立的資料型別,和整型之間不能進行隱式轉換。

hello.cpp
   
  1. #include <iostream>
  2. int main (void) {
  3. std::cout << "Hello, World !" << std::endl;
  4. int i;
  5. double d;
  6. char s[256];
  7. // scanf ("%d%lf%s", &i, &d, s);
  8. std::cin >> i >> d >> s;
  9. // printf ("%d %lf %s\n", i, d, s);
  10. std::cout << i << ' ' << d << ' ' << s << '\n';
  11. return 0;
  12. }
 
 輸出: cout - 標準輸出物件;(控制檯輸出物件)  輸入: cin   - 標準輸入物件; 插入運算子: << (把字串的字面值插入到cout內) 提取運算子: >>
std::cout (控制檯輸出物件) << "Hello,world !" <<(多次插入) std::endl;(換行\n)



math.cpp
   
  1. extern "C" {
  2. int add (int a, int b) {
  3. return a + b;
  4. }
  5. int sub (int a, int b) {
  6. return a - b;
  7. }
  8. }
  9. /*
  10. extern "C" int add (int a, int b, int c) {
  11. return a + b + c;
  12. }
  13. */
calc.c3
   
  1. #include <stdio.h>
  2. int add (int, int);
  3. int main (void) {
  4. printf ("%d\n", add (10, 20));
  5. return 0;
  6. }


nsover.cpp
   
  1. #include <iostream>
  2. using namespace std;
  3. namespace ns1 {
  4. int foo (int a) {
  5. cout << "ns1::foo(int)" << endl;
  6. return a;
  7. }
  8. };
  9. namespace ns2 {
  10. double foo (double a) {
  11. cout << "ns2::foo(double)" << endl;
  12. return a;
  13. }
  14. };
  15. int main (void) {
  16. using namespace ns1;
  17. using namespace ns2;
  18. cout << foo (10) << endl;
  19. cout << foo (1.23) << endl;
  20. using ns1::foo;
  21. cout << foo (10) << endl;
  22. cout << foo (1.23) << endl;
  23. using ns2::foo;
  24. cout << foo (10) << endl;
  25. cout << foo (1.23) << endl;
  26. return 0;
  27. }
using ns1::foo; ns1中的foo被引入到作用域中了;
 
overload.cpp
   
  1. #include <iostream>
  2. using namespace std;
  3. void foo (int a) {
  4. cout << "foo(int)" << endl;
  5. }
  6. void bar (int a) {}
  7. //int foo (int a) {}
  8. int foo (int a, double b) {
  9. cout << "foo(int,double)" << endl;
  10. }
  11. int foo (double a, int b) {
  12. cout << "foo(double,int)" << endl;
  13. }
  14. //int foo (int b, double a) {}
  15. int main (void) {
  16. foo (100);
  17. foo (100, 1.23);
  18. foo (1.23, 100);
  19. // foo (100, 100);
  20. return 0;
  21. }
 
struct.c
   
  1. #include <stdio.h>
  2. int main (void) {
  3. struct A {};
  4. printf ("%d\n", sizeof (struct A));
  5. struct A a;
  6. struct A* pa = &a;
  7. printf ("%p\n", pa);
  8. return 0;
  9. }

C語言中的空結構大小為0;
struct.cpp
   
  1. #include <iostream>
  2. using namespace std;
  3. struct Student {
  4. char name[128];
  5. int age;
  6. void who (void) {
  7. cout << "我叫" << name << ",今年" << age
  8. << "歲了。" << endl;
  9. }
  10. };
  11. int main (void) {
  12. Student student = {"張飛", 25}, *ps = &student;
  13. student.who ();
  14. ps->who ();
  15. struct A {};
  16. cout << sizeof (A) << endl;
  17. return 0;
  18. }
  C++中類和結構沒有區別;    sizeof (空結構) ->  1;
union.cpp
   
  1. #include <iostream>
  2. using namespace std;
  3. int main (void) {
  4. // 匿名聯合
  5. union {
  6. int x;
  7. char c[4] /*= {'A', 'B', 'C', 'D'}*/;
  8. };
  9. cout << (void*)&x << ' ' << (void*)c << endl;
  10. x = 0x12345678;
  11. for (int i = 0; i < 4; ++i)
  12. cout << hex << (int)c[i] << ' ';
  13. cout << endl;
  14. return 0;
  15. }

     gcc -std=c++0x 使用2011編譯器去編譯;-2011標準;









來自為知筆記(Wiz)