c++常识要点
2015-05-01 10:22
162 查看
1 指针、常量、的理解
char * const cp : 定义一个指向字符的指针常数
const char* p : 定义一个指向字符常数的指针
char const* p : 等同于const char* p
2 子类对象调用父类的同名函数。举例如下:
class A {
void f1(){};
};
class B : public A {
void f1(){};
}
void main() {
B b;
b.A::f1();//此处调用A的f1()函数
}
3 .c++ primer ed4th 指出,包含头文件时,如果是标准库的文件应该使用尖括号,如果是自定义的文件,应该使用双引号。原文如下:
4 教程中指出的语句命名空间,在测试时没有起作用。原文如下:
statement names defined within the condition of a statement, such as anif, for, orwhile.
测试例子如下:
for(int i = 0; i < 5; i++) {
cout << i << endl;
}
cout << "integral:" << i << endl;
//out:
//0
//1
//2
//3
//4
//integral:5
5.枚举类型enum的变量enumeration只能被枚举成员enumerator赋值赋值或者其它的同类型对象enumeration赋值。
6.类定义由类接口interface(在public区)和类实现implementation (在private区)组成。数据成员放在类实现区,方法成员部分放在类实现区,部分放在类接口区
7.c++ primer 4th说可以这样定义 size_type类型:
但是我试过了没有成功,cl编译器报了一堆看不懂的错误。只能使用string::size_type
更正:已经成功使用,需要使用using声明:using std::vector<string>;
8 使用string和unsigned long 分别初始化bitset类模板的差别示例。
string bstr = "101101111";
unsigned int ulong = 367;//等于 101101111
bitset<8> b1(bstr);
bitset<8> b2(ulong);
cout << "b1:" << b1 << endl;
cout << "b2:" << b2 << endl;
//输出:
//10110111
//01101111
9 使用数组初始化向量的示例
const size_t arr_size = 6;
int int_arr[arr_size] = {0, 1, 2, 3, 4, 5};
// ivec has 6 elements: each a copy of the corresponding element in int_arr
vector<int> ivec(int_arr, int_arr + arr_size);
10 const类型对象的初始化
const对象的初始化值要求是确定的值
比如
int i = 50;
const int ic = i;//初始化值50是确定
但是如下就是错误的
int i;
const int ic = i//此时,初始化值不知道是多少,i=50的作用域是【i=50】这一行到函数体末尾。
...
i = 50;
11 使用cout输出二进制
int short_value = 32767;
cout <<hex<< bitset<sizeof(int)*8>(short_value) << endl; //bitset<size_t>(),类模板构造函数
待续
char * const cp : 定义一个指向字符的指针常数
const char* p : 定义一个指向字符常数的指针
char const* p : 等同于const char* p
2 子类对象调用父类的同名函数。举例如下:
class A {
void f1(){};
};
class B : public A {
void f1(){};
}
void main() {
B b;
b.A::f1();//此处调用A的f1()函数
}
3 .c++ primer ed4th 指出,包含头文件时,如果是标准库的文件应该使用尖括号,如果是自定义的文件,应该使用双引号。原文如下:
Headers for the standard library are enclosed in angle brackets (< >). Nonstandard headers are enclosed in double quotes (" "). |
statement names defined within the condition of a statement, such as anif, for, orwhile.
测试例子如下:
for(int i = 0; i < 5; i++) {
cout << i << endl;
}
cout << "integral:" << i << endl;
//out:
//0
//1
//2
//3
//4
//integral:5
5.枚举类型enum的变量enumeration只能被枚举成员enumerator赋值赋值或者其它的同类型对象enumeration赋值。
6.类定义由类接口interface(在public区)和类实现implementation (在private区)组成。数据成员放在类实现区,方法成员部分放在类实现区,部分放在类接口区
7.c++ primer 4th说可以这样定义 size_type类型:
vector<string>::size_type
但是我试过了没有成功,cl编译器报了一堆看不懂的错误。只能使用string::size_type
更正:已经成功使用,需要使用using声明:using std::vector<string>;
8 使用string和unsigned long 分别初始化bitset类模板的差别示例。
string bstr = "101101111";
unsigned int ulong = 367;//等于 101101111
bitset<8> b1(bstr);
bitset<8> b2(ulong);
cout << "b1:" << b1 << endl;
cout << "b2:" << b2 << endl;
//输出:
//10110111
//01101111
9 使用数组初始化向量的示例
const size_t arr_size = 6;
int int_arr[arr_size] = {0, 1, 2, 3, 4, 5};
// ivec has 6 elements: each a copy of the corresponding element in int_arr
vector<int> ivec(int_arr, int_arr + arr_size);
10 const类型对象的初始化
const对象的初始化值要求是确定的值
比如
int i = 50;
const int ic = i;//初始化值50是确定
但是如下就是错误的
int i;
const int ic = i//此时,初始化值不知道是多少,i=50的作用域是【i=50】这一行到函数体末尾。
...
i = 50;
11 使用cout输出二进制
int short_value = 32767;
cout <<hex<< bitset<sizeof(int)*8>(short_value) << endl; //bitset<size_t>(),类模板构造函数
待续
相关文章推荐
- java与C++之间进行SOCKET通讯要点简要解析
- C/C++知识要点6——定义只能在堆/栈上生成对象的类
- c++复习要点总结z之十二——STL string
- c++复习要点总结之三 ——构造函数
- C++知识要点
- 编写可移植C/C++程序的要点
- C++要点(四)-函数模板和类模板
- C/C++代码检视要点
- 漫谈C++:良好的编程习惯与编程要点
- 面试题--C++常识面试题
- c++复习注意要点
- C++常识之——多态与重载
- java与C++之间进行SOCKET通讯要点简要解析
- java与C++之间进行SOCKET通讯要点简要解析
- java与C++之间进行SOCKET通讯要点简要解析
- C++知识要点
- c++头文件的一点常识
- 编写可移植C/C++程序要点
- 你最需要注意的11条要点for C++
- c++ 内存管理:9、malloc/free的使用要点 new/delete的使用要点