一、布尔类型
#include <iostream> using namespace std; int main(void) { bool b = false; //boolalpha:流控制符,以字符串形式打印bool cout << boolalpha << "b=" << b << endl; cout << "sizeof(b)=" << sizeof(b) << endl; b = 3+5; cout << "b=" << b << endl;//true b = 1.2*3.4; cout << "b=" << b << endl;//true char *p = NULL;//NULL-->(void*)0 b = p; cout << "b=" << b << endl;//false return 0; }
二、函数重载
#include <iostream> using namespace std; int foo(int i){ cout << "foo(int)" << endl; } void foo(int i,int j){ cout << "foo(int,int)" << endl; } void foo(int a,float b){ cout << "foo(int,float)" << endl; } int main(void) { foo(10); foo(10,20); foo(10,1.23f); //函数指针的类型决定其匹配的重载版本 void (*pfoo)(int,float) = foo; pfoo(10,20);//foo(int,float) return 0; }
#include <iostream> using namespace std; //char->int:升级转换 void foo(int i){ cout << "foo(1)" << endl; } //char->const char:常量转换 void foo(const char c){ cout << "foo(2)" << endl; } //short->char:降级转换 void fun(char c){ cout << "fun(1)" << endl; } //short->int:升级转换 void fun(int i){ cout << "fun(2)" << endl; } //省略号匹配 void hum(int i,...){ cout << "hum(1)" << endl; } //double->int:降级转换 void hum(int i,int j){ cout << "hum(2)" << endl; } int main(void) { char c = 'a'; foo(c); short s = 100; fun(s); hum(10,1.23); return 0; }
三、函数声明和定义
#include <iostream> using namespace std; //函数声明 void func(int a=10,int b=20,int c=30); //void func(int i){//注意调用时的歧义错误 //}; int main(void) { func(11,22,33);//11 22 33 func(11,22);//11 22 30 func(11);//11 20 30 return 0; } //函数定义 void func(int a/*=10*/,int b/*=20*/,int c/*=30*/){ cout << "a=" << a << ",b=" << b << ",c=" << c << endl; }
四、数组
#include <iostream> using namespace std; int main(void) { /* int* parr = new int[10]; for(int i=0;i<10;i++){ parr[i] = i; cout << parr[i] << ' '; }*/ //new数组同时初始化,C++11语法支持 int* parr = new int[10]{0,1,2,3,4,5,6,7,8,9}; for(int i=0;i<10;i++){ cout << parr[i] << ' '; } cout << endl; delete[] parr; parr = NULL; return 0; }
五、值引用
#include <iostream> using namespace std; int main(void) { int a = 60; int& b = a;//b引用a,b就是a的别名 cout << "a=" << a << endl; cout << "b=" << b << endl; cout << "&a=" << &a << endl; cout << "&b=" << &b << endl; b++; cout << "a=" << a << endl;//61 cout << "b=" << b << endl;//61 //int& r;//error int c = 80; b = c;//ok,但不是修改引用目标,仅是赋值操作 cout << "a=" << a << endl;//80 cout << "b=" << b << endl;//80 cout << "&b=" << &b << endl; cout << "&c=" << &c << endl; //char& rc = c;//error return 0; }
#include <iostream> using namespace std; int func(void){ int num = 100; cout << "&num=" << &num << endl; return num;//int tmp = num; } int main(void) { //int r4 = tmp; //int& r4 = func(); const int& r4 = func(); cout << r4 << endl;//100 cout << "&r4=" << &r4 << endl; /* //int& r = 100;//error const int& r = 100;//ok cout << r << endl;//100 int a=3; int b=5; //(a+b) = 10;//error //a+b表达式结果是一个临时变量(右值) //int& r2 = a+b;//error const int& r2 = a+b;//r2引用就是临时变量 cout << r2 << endl;//8 char c = 'a'; //首先需要将c隐式转换为int,隐式转换的结果 //是临时变量,r3实际引用的就是临时变量 //int& r3 = c;//error const int& r3 = c;//ok cout << r3 << endl;//97 cout << "&c=" << (void*)&c << endl; cout << "&r3=" << &r3 << endl; */ return 0; }
#include <iostream> using namespace std; int main(void) { int a = 1; cout << ++a << endl;//2 cout << a << endl;//2 ++a = 10;//ok cout << a << endl;//10 ++++++a;//ok cout << a << endl;//13 int& ra = ++a; cout << ra << endl;//14 cout << (a = 10) << endl;//10 cout << a << endl;//10 (a = 10) = 20; cout << a << endl;//20 int& ra2 = (a+=30);//ok cout << ra2 << endl;//50 return 0; }