2012年4月6日

C++ Primer Plus读书笔记08

摘要: 2012-03-091、友元类 类(遥控器,Remote)的所有方法可以访问类(TV)的私有,保护成员class TV{ friend class Remote;}class Remote{……}//实现2、友元成员函数 稍复杂,见P5433、类包含:将类对象作为另一个类的成员 如class Student{string name;}; //包含string 类成员name 类嵌套:不创建类成员,只定义类型4、异常abort()函数 用cstdlib头文件 作用:向标准错误流cerr发送abnormal program termination double func(int x,int y){ 阅读全文

posted @ 2012-04-06 17:42 TheBest 阅读(313) 评论(0) 推荐(0) 编辑

C++ Primer Plus读书笔记07

摘要: 2012-03-051、在类声明中声明的结构、类或枚举,作用域为整个类。此时不创建对象;若为private 权限,只能在类中使用被声明的类型;若public,则可在类外面使用被声明的类型2、派生类的构造函数 (1)先创建基类对象→通过成员初始化列表将基类信息传给基类构造函数 (2)若不使用初始化列表,系统将调用基类的默认构造函数 (1)derived::derived(int x,int y):base(x,y) {……} (2)derived::derived(int x,int y) {……}//此法调用基类的默认构造函数 析构的顺序相反,先析构派生类,再析构基类3、基类指针可以指向派生类 阅读全文

posted @ 2012-04-06 17:42 TheBest 阅读(143) 评论(0) 推荐(0) 编辑

C++ Primer Plus读书笔记05

摘要: 2012-03-011、默认参数 int test(int n,int m=4,int j=5) //从右向左添加默认值,不可跳过2、函数重载==函数多态 定义一组名称相同的函数 (1)避免歧义 (2)int test(int n)与int test(int &n)不可共存 (3)void test(const char *bit)可接受const或非const变量 (4)特征标重载(即参数列表重载),而非函数类型重载int test(int a,int b)long test(int a,int b)//错误int test(int a,int b)int test(long a,i 阅读全文

posted @ 2012-04-06 17:39 TheBest 阅读(145) 评论(0) 推荐(0) 编辑

C++ Primer Plus读书笔记06

摘要: 2012-03-041、重载运算符 例如Time类重载+ Time operator +(const Time input){ Time sum; sum.minute=input.minute+minute;;… return sum;}Time Sum(const Time &t){ …. return sum}两者功能一样,但左边是重载,可以实现Time a,b,c,d; a=b+c+d;这样的操作重载后的操作符至少有一个是用户定义的类型(本例为time型),且优先级不变。∴不可重载+表示(double-double) 不可重载的操作符:sizeof,逗号,作用域运算符::,见P 阅读全文

posted @ 2012-04-06 17:39 TheBest 阅读(112) 评论(0) 推荐(0) 编辑

C++ Primer Plus读书笔记04

摘要: 2012-02-251、char ch; cin>>ch; //一次一个字符,忽略空格 cin.get(ch); //不忽略2、 ctrl+z模拟EOF,读到文件为,则cin.eof()==cin.fail()==trueint count=0; char ch;cin.get(ch);while(cin.fail()==false){ cout<<ch; count++; cin.get(ch);}//可一行一行读入,输入ctrl+z结束3 cin.get(ch)返回cin对象,但istream类中提供了可将istream对象转换为bool值的函数, 当cin出现在i 阅读全文

posted @ 2012-04-06 17:38 TheBest 阅读(162) 评论(0) 推荐(0) 编辑

C++ Primer Plus读书笔记03

摘要: 2012-02-241、 结构体的默认访问权限是public,类是private∴ class myClass{void setValue(int value);} //不声明,则为private权限2、 类声明于myclass.h中,类实现于myclass.cpp中(include”myclass.h”)void myclass::setValue(int value){…}3、 快速string ,double转换#include<sstream>string doubleToString(double value){ostringstream ostr; ostr<&l 阅读全文

posted @ 2012-04-06 17:37 TheBest 阅读(152) 评论(0) 推荐(0) 编辑

C++ Primer Plus读书笔记02

摘要: 2012-02-221、 结构体中可以指定占特定位数的成员struct mytest{ int age:4 //用冒号分隔,则变量age占4字节 unsigned char a:4 //同上}2、 union共用体,当数据项使用多种格式(但不会同时使用时),比struct节省空间3、 new过程的3步 S1分配内存 S2调用所new对象的构造函数 S3返回对象的引用4、 int *p1,p2 //创建了一个指针和一个整型变量5、 几种指针分配策略int value=6; int *p_value; p_value=&value; //正确int a=5; int *p=&a; 阅读全文

posted @ 2012-04-06 17:36 TheBest 阅读(143) 评论(0) 推荐(0) 编辑

C++ Primer Plus读书笔记01

摘要: 2012-02-13new分配到堆上,栈上分自动变量(就是局部变量)new 不止是分配内存,而且会调用类的构造函数,同理delete会调用类的析构函数,而malloc则只分配内存,不会进行初始化类成员的工作,同样free也不会调用析构函数2012-02-191 sizeof 返回类型或者变量的长度,依赖于不同环境、编译器 对类型用sizeof,应该加括号,如sizeof(int) 对变量名,可以省略,如sizeof nMyAge;2#include<limits>包含INT_MAX,CHAR_BIT等定义3默认cout以十进制输出 int waist=0x42;//16进制 int 阅读全文

posted @ 2012-04-06 17:35 TheBest 阅读(248) 评论(0) 推荐(0) 编辑

导航