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标准;












猜你喜欢

转载自www.cnblogs.com/xuxaut-558/p/10041560.html