c++,类处方笺word模板下载。我图片1处多了地址符,说明的是一个什么函数?2处也多了地址符,为啥不放函数名前面

2015年计算机二级考试C++过关练习题及答案(3)
来源:考试吧
  点击查看:
  1[判断题]C++语言比C语言对数据类型要求更加严格了。
  参考答案:对
  2[简答题] 为单链表类模板增加一个复制构造函数和赋值运算符(=)。在上题基础上,List类增加一个复制构造函数和赋值运算符(=)。
  参考解析:
  templateList::List(List& l){
  head=new Node(-9999);//现建立头结点
  Node* tempP=l.head-&link,*tempC;
  while(tempP!=NULL){
  tempC=CreatNode(tempP-&info);
  InsertAfter(tempC);
  tempP=tempP-&
  }
  }
  templateList& List::operator=(List& l){
  MakeEmpty();//先释放原来链表的数据结点
  Node* tempP=l.head-&link,*tempC;
  while(tempP!=NULL){
  tempC=CreatNode(tempP-&info);
  InsertAfter(tempC);
  tempP=tempP-&
  }
  return *
  }
  int main(){
  Node * P1;
  List list1,list2;
  int a[10]={20,12,0,-5,9,-18,6,11,5,3},i,j;
  for(i=0;i&10;i++){
  P1=list1.CreatNode(a[i]);
  list1.InsertOrder(P1);
  }
  list1.PrintList();
  cout&&"请输入一个要求删除的整数"&
  cin&&j;
  P1=list1.Find(j);
  if(P1!=NULL){
  P1=list1.DeleteNode(P1);
  delete P1;
  list1.PrintList();
  }
  else cout&&"未找到"&
  cout&&"请输入一个要求插入的整数"&
  cin&&j;
  P1=list1.CreatNode(j);
  list1.InsertOrder(P1);
  list1.PrintList();
  list2=list1;
  list2.PrintList();
  List list3=list1;
  list3.PrintList();
  cout&&"请输入一个要求删除的整数"&
  cin&&j;
  P1=list1.Find(j);
  if(P1!=NULL){
  P1=list1.DeleteNode(P1);
  delete P1;
  list1.PrintList();
  }
  else cout&&"未找到"&
  list2=list3=list1;
  list2.PrintList();
  list3.PrintList();
  list1.MakeEmpty();//清空list1
  list2.MakeEmpty();//清空list1
  list3.MakeEmpty();//清空list1
  return 0;
  }
  3[单选题]一个工作人员可以使用多台计算机,而一台计算机可被多个人使用,则实体工作人员与实体计算机之间的联系是:
  A.一对一B.一对多C.多对多D.多对一
  参考答案:C
  4[单选题] 下列叙述中正确的是( )。
  A.在栈中,栈中元素随栈底指针与栈顶指针的变化而动态变化
  B.在栈中,栈顶指针不变,栈中元素随栈底指针的变化而动态变化
  C.在栈中,栈底指针不变,栈中元素随栈顶指针的变化而动态变化
  D.以上说法都不正确
  参考答案:C
  参考解析:栈是先进后出的数据结构,在整个过程中,栈底指针不变,入栈与出栈操作均由栈顶指针的变化来操作,所以选择c。
  5[单选题]下列叙述中正确的是 (  )。
  A.线性表是线性结构
  B.栈与队列是非线性结构
  C.线性链表是非线性结构
  D.二叉树是线性结构
  参考答案:A
  参考解析:线性表是线性结构;线性链表是线性表的链式存储结构,因此也是线性结构;栈与队列 是特殊的线性表,因此也是线性结构;二叉树是非线性结构。
  6[单选题]
   
  A.0B.1C.2D.3
  参考答案:B
  参考解析:此题实际考查的是变量作用域的屏蔽效应。在C++中,当标识符的作用域发生重叠时,在一个函数中声明的标识符可以屏蔽函数外声明的标识符或全局标识符。声明类A的全局对象时,构造函数首先调用int函数,但由于静态成员变量a被构造函数内部的形参a所屏蔽所以a++改变的是形参a的值,对静态成员变量a没有影响。
  7[单选题]在下面的4个关键字中用来说明虚函数的是(  )。
  A.virtualB.publicC.protectedD.private
  参考答案:A
  参考解析:C++中用virtual关键字声明虚函数。而public、protected、private为成员访问限定符。
  8[单选题]
   
  参考答案:B
  9[单选题]有如下程序:
  #include
  classBase{
  public:
  Base(intx=O){cout&
  };
  classDerived:publicBase{
  public:
  Derived(intx=O){cout&
  private:
  B
  };
  intmain(){
  Derivedd(1);
  return0;
  }
  程序执行后的输出结果是(  )。
  A.100B.000C.010D.001
  参考答案:D
  参考解析:本题考查的知识点是豢的构造。建立一个类的对象时,构造函数的执行顺序如下:①执行基类的构造函数,调用顺序按照各个基类被继承时声明的顺序(自左向右);②执行成员对象的构造函数,调用顺序按照各个成员对象在类中声明的顺序(自上而下);③执行自身的构造函数。本题Derived类继承于Base类,所以首先会构造基类Base,但Derived类的构造函数没有初始化列表,所以将调用Base类的默认构造函数,输出一个0。接下来由于它的成员中还定义了一个Base类的对象,两构造函数也没有显示初始化这个对象,所以再次调用Base类的默认构造函数输出一个0。最后构造自身,因为主函数中传入了构造参数1,所以构造自身时输出了一个1。故最终输出结果为001。
  10[单选题] 若已定义:
  inta[]={0,1,2,3,4,5,6,7,8,9},*p=a,i;其中0≤i≤9,则对a数组元素不正确的引用是(  )。
  A.a[p-a]B.*(&a[i])C.p[i]D.a[10]
  参考答案:D
  参考解析:通常,引用一个数组元素可以用下标法,如a[p-a]形式,或指针法,如*(&a[i])的形式。本题中a[9]=9,a[10]显然超出了数组范围,数组的下标是从0开始的。
  11[判断题]使用class定义的类,其默认的访问权限是公有的,使用struct定义的类,其默认的访问权限是私有的。
  参考答案:错
  12[单选题]需求分析阶段的任务是(  )。
  A.软件开发方法B.软件开发工具C.软件开发费用D.软件系统功能
  参考答案:D
  参考解析:需求分析是软件定义时期的最后一个阶段,它的基本任务就是详细调查现实世界要处理的对象,充分了解原系统的工作概况,明确用户的各种需求,然后在这些基础上确定新系统的功能。
  13[单选题]有如下程序段:
  int *p, a=10, b=1;
  p=&a;
  a=*p+b;
  执行该程序段后,a的值是(  )。
  A.12B.11C.10D.编译出错
  参考答案:B
  14[单选题]在模块化程序设计中,按功能划分模块的原则是(  )。
  A.各模块的功能尽量单一,且各模块之间的联系尽量的少
  B.各模块的功能尽量单一,且各模块之间的联系尽量紧密
  C.各模块应包括尽量多的功能
  D.各模块应包括尽量多的输入输出操作
  参考答案:A
  参考解析:在模块化程序设计中.按功能划分模块的原则是:要求各模块的功能尽量单一,且各模块之间的联系尽量的少。
  15[单选题] 数据库设计的根本目标是要解决(  )。
  A.数据共享问题
  B.数据安全问题
  C.大量数据存储问题
  D.简化数据维护
  参考答案:A
  参考解析:从数据库的概念中可以看到,所谓数据库是指长期存储在计算机内的、有组织的、可共享的数据集合。因此进行数据库设计的根本目标还是为了解决数据共享问题。
  16[单选题]下列关于this指针的叙述中,正确的是(  )
  A.任何与类相关的函数都有this指针
  B.类的成员函数都有this指针
  C.类的友元函数都有this指针
  D.类的非静态成员函数才有this指针
  参考答案:D
  17[简答题]使用VC++6.0打开考生文件夹下的源程序文件1.cpp,该程序运行时有错,请改正其中的错误,使程序正常运行,并使程序输出的结果为:
  sizeof(S1)=5
  sizeof(s2)=10
  sizeof(s3)=1
  注意:不要改动main函数,不能增加或删除行,也不能更改程序的结构,错误的语句在//******error******的下面。
  (1)不能删除assert()语句。
  (2)只能修改后面的数字。
  提示:assert函数如果为假,则会产生一个中断异常。
  试题程序:
  #include
  #include
  voidmain()
  {
  char*s1="abc":
  //********error********
  assert(sizeof(s1)==3):
  cout&&"sizeof(s1)=5"&
  chars2[10]="ab":
  //********error********
  assert(sizeof(s2)==2);
  cout&&"sizeof(s2)=10"&
  chars3=23:
  //********error********
  assert(sizeof(s3)==4);
  cout&&"sizeof(s3)=1"&
  }
  参考解析:
  (1)应改为“assert(sizeof(s1)==4);”。
  (2)应改为“assert(sizeof(s2)==1O);”。
  (3)应改为“assert(sizeof(s3)==1);”。
  【解析】assert函数如果为假,则会产生一个中断异常。所以要让它正常执行输出结果,那么assert函数只有为真,才不会产生中断异常。“assert(sizeof(s1)==3);”语句中“sizeof(s1)==3”为假,这会使程序产生异常,不能正常执行。因为str1的值为abc,但是它还有一个尾符,它的长度应该是4,而不是3,所以在第1处修改为“assert(sizeof(s1)==4);”。str2[10]说明,str2的空间为10,在第2处修改为“assert(sizeof(s2)==10);”。charstr3=23;语句说明23是字符,所占存储空间为1,所以修改为“assert(sizeof(s3)==1);”。
  18[填空题]数据模型分为格式化模型和非格式化模型,则关系模型属于_______ 模型。
  【答案】非格式化
  【解析】数据模型分为格式化模型和非格式化模型,关系模型属于非格式化模型。
  19[填空题]继承的方式有公有继承、私有继承和 【13】 3种。
  参考解析:保护继承1
  相关推荐:
免责声明:本站部分内容、图片、文字、视频等来自于互联网,仅供大家学习与交流。相关内容如涉嫌侵犯您的知识产权或其他合法权益,请向本站发送有效通知,我们会及时处理。反馈邮箱&&&&。
学生服务号
在线咨询,奖学金返现,名师点评,等你来互动2015年1月 C/C++大版内专家分月排行榜第二2012年3月 C/C++大版内专家分月排行榜第二2011年11月 C/C++大版内专家分月排行榜第二2010年6月 C/C++大版内专家分月排行榜第二2010年5月 C/C++大版内专家分月排行榜第二
2011年4月 C/C++大版内专家分月排行榜第三2011年2月 C/C++大版内专家分月排行榜第三2010年8月 C/C++大版内专家分月排行榜第三
2012年11月 Linux/Unix社区大版内专家分月排行榜第二2011年8月 Linux/Unix社区大版内专家分月排行榜第二2008年10月 C/C++大版内专家分月排行榜第二
2012年8月 Linux/Unix社区大版内专家分月排行榜第三
2015年1月 C/C++大版内专家分月排行榜第二2012年3月 C/C++大版内专家分月排行榜第二2011年11月 C/C++大版内专家分月排行榜第二2010年6月 C/C++大版内专家分月排行榜第二2010年5月 C/C++大版内专家分月排行榜第二
2011年4月 C/C++大版内专家分月排行榜第三2011年2月 C/C++大版内专家分月排行榜第三2010年8月 C/C++大版内专家分月排行榜第三
本帖子已过去太久远了,不再提供回复功能。文章数:355
评论数:24
访问量:82715
注册日期:
阅读量:1297
阅读量:3317
阅读量:452727
阅读量:1137184
51CTO推荐博文
【本文谢绝转载】C++类型转换&&&&&&&& 类型转换1static_cast&&&&&&&& static_cast的局限性&&&&&&&& 类型转换2 强制类型转换,reinterpret_cast&&&&&&&& 类型转换3dynamic_cast 类的类型转换,演示:自上而下&&&&&&&& static_cast& reinterpret_cast 转换的对比&&&&&&&& const_cast,将常量类型转换为变量,经典案例&&&&&&&& 类型转换4const_cast,修改无法修改的内存,直接宕掉&&&&&&&& 总结异常&&&&&&&&& 异常将严格按照类型匹配&&&&&&&& 1,基本语法&&&&&&&& 2,发送异常,不去接收,程序会宕掉&&&&&&&& 函数接收到异常,继续往外抛:&&&&&&&& 异常可以跨函数:&&&&&&&& 异常:1栈解旋,可以执行类的析构函数&&&&&&&& 异常接口说明1:指定抛出异常的种类&&&&&&&& 异常接口说明2: 不允许抛出异常&&&&&&&& 异常接口说明3:不限制抛出异常&&&&&&&& 异常类型生命周期 1,传统的函数报错方式:&&&&&&&& 异常类型生命周期 2,普通的异常抛出&&&&&&&& 异常类型生命周期 3,指针类型&&&&&&&& 异常类型生命周期 3,类异常&&&&&&&& 这个e是谁抛出的?&&&&&&&& 【结论】把抛出的匿名对象,拷贝给e对象&&&&&&&& 【证明】把抛出的匿名对象,拷贝给e对象&&&&&&&& 结论2: 使用引用的话 会使用throw时候的那个对象&&&&&&&& 结论3: 指针可以和引用/元素写在一块 但是引用/元素不能写在一块&&&&&&&& 结论3: 指针可以和引用/元素写在一块 但是引用/元素不能写在一块&&&&&&&& 需要显式调用delete&&&&&&&& 结论4: 类对象时, 使用引用比较合适&&&&&&&& 【异常的层次结构?老方法】&&&&&&&& 【异常的层次结构?推荐方法】类的多态实现&&&&&&&& C++标准程序库异常&&&&&&&& 继承C++编制异常库:实现函数异常&标准输入输出IO&&&&&&&& 标准输入&&&&&&&&&&&&&&&&&& 1,基本输入输出&&&&&&&&&&&&&&&&&& cin.get()从缓冲区读取,输出到屏幕:(存在IO阻塞)&&&&&&&&&&&&&&&&&& cin.get()如果缓冲区有数据,就自动读取&&&&&&&&&&&&&&&&&& cin.getline读取一行&&&&&&&&&&&&&&&&&& cin.ignore函数作用是跳过输入流中n个字符,&&&&&&&&&&&&&&&&&& cin.peek()查看缓冲区有没有内容&&&&&&&&&&&&&&&&&& cin.putback把收到的字符返回去,可以再读一次&&&&&&&& 标准输出&&&&&&&&&&&&&&&&&& cout.put()输出单个字符&&&&&&&&&&&&&&&&&& cout.write标准输出&&&&&&&&&&&&&&&&&& cout的格式化输出 1&&&&&&&&&&&&&&&&&& cout格式输出2&&&&&&&&&&&&&&&&&& cout格式输出3文件IO&&&&&&&& 1,文件写入&&&&&&&& 文件写,文件读操作&&&&&&&& 文件追加写入fname,ios::app&&&&&&&& 文件二进制的读写:&&&&&&&&作业练习&&&&&&&&1 编程实现以下数据输入/输出:&&&(1)以左对齐方式输出整数,域宽为12。&&&(2)以八进制、十进制、十六进制输入/输出整数。&&&(3)实现浮点数的指数格式和定点格式的输入/输出,并指定精度。&&&(4)把字符串读入字符型数组变量中,从键盘输入,要求输入串的空格也全部读入,以回车符结束。&&&(5)将以上要求用流成员函数和操作符各做一遍。&&&2编写一程序,将两个文件合并成一个文件。3编写一程序,统计一篇英文文章中单词的个数与行数。4编写一程序,将C++源程序每行前加上行号与一个空格。4.5编写一程序,输出 ASCII码值从20到127的ASCII码字符表,格式为每行10个。---------------------------------------------------------------类型转换1static_castchunli@http://.~/c++$&cat&main.cpp&
#include&&iostream&
using&namespace&
int&main()
double&pi&=&3.1415926;
int&num1&=&(int)
int&num2&=&static_cast&int&(pi); //C++风格1,静态类型转换,编译器会做类型检查
int&num3&=&
//C语言中&隐式类型转换的地方均可以使用static_cast进行类型转换
cout&&&&num1&&&&
cout&&&&num2&&&&
cout&&&&num3&&&&
chunli@http://.~/c++$&g++&-g&-o&run&main.cpp&&&&./run&
chunli@http://.~/c++$类型转换1static_cast的局限性chunli@http://.~/c++$&cat&main.cpp&
#include&&iostream&
using&namespace&
int&main()
char&p1[]&=&"Hello&World!";
int&&*p2&=&NULL;
p2&=&static_cast&int&*&(p1);
chunli@http://.~/c++$&g++&-Wwrite-strings&-g&-o&run&main.cpp&&&&./run&
main.cpp:&In&function&‘int&main()’:
main.cpp:8:28:&error:&invalid&static_cast&from&type&‘char&[13]’&to&type&‘int*’
&&p2&=&static_cast&int&*&(p1);
&&&&&&&&&&&&&&&&&&&&&&&&&&&&^
chunli@http://.~/c++$类型转换2在不同类型之间强制类型转换reinterpret_cast不能将这个作为普通数据类型的转换chunli@http://.~/c++$&cat&main.cpp&
#include&&iostream&
using&namespace&
int&main()
char&p1[]&=&"Hello&World!";
int&&*p2&=&NULL;
p2&=&reinterpret_cast&&int&*&(p1);
cout&&&&*p2&&&&
chunli@http://.~/c++$&g++&-Wwrite-strings&-g&-o&run&main.cpp&&&&./run&
chunli@http://.~/c++$类型转换3 dynamic_cast 类的类型转换演示自上而下chunli@http://.~/c++$&cat&main.cpp&
#include&&iostream&
using&namespace&
class&Animal
virtual&void&cry()&=&0;
class&Dog&:&public&Animal
virtual&void&cry()
cout&&&&"汪汪"&&&&
void&doHome()
cout&&&&"看家"&&&&
class&Cat&:&public&Animal
virtual&void&cry()
cout&&&&"喵喵"&&&&
void&doThing()
cout&&&&"抓老鼠"&&&&
void&playObj(Animal&*base)
base-&cry();&//&1有继承&2虚函数重写&3&父类指针&指向子类对象&&==&多态
//能识别子类对象
//&dynamic_cast&运行时类型识别&&RIIT
Dog&*pDog&=&dynamic_cast&Dog&*&(base);
if&(pDog&!=&NULL)
pDog-&doHome();&//让够&做自己&特有的工作&
Cat&*pCat&=&dynamic_cast&Cat&*&(base); //父类对象&===&&子类对象&
//向下转型&&
//把老子&转成&小子&
if&(pCat&!=&NULL)
pCat-&doThing();&&//让够&做自己&特有的工作&
int&main()
Animal&*pBase&=&NULL;
playObj(&d1);
playObj(&c1);
chunli@http://.~/c++$&g++&&-g&-o&run&main.cpp&&&&./run&
chunli@http://.~/c++$static_cast &reinterpret_cast 转换的对比把大树转换成动物会报错因为编译器会对static_cast做检查chunli@http://.~/c++$&cat&main.cpp&
#include&&iostream&
using&namespace&
class&Tree&{};
class&Animal
virtual&void&cry()&=&0;
class&Dog&:&public&Animal
virtual&void&cry()
cout&&&&"汪汪"&&&&
void&doHome()
cout&&&&"看家"&&&&
class&Cat&:&public&Animal
virtual&void&cry()
cout&&&&"喵喵"&&&&
void&doThing()
cout&&&&"抓老鼠"&&&&
int&main()
Animal&*pBase&=&&d1;
pBase&=&static_cast&Animal&*&(&d1);&
//让C++编译在编译的时候进行&类型检查&
pBase&=&reinterpret_cast&Animal&*&(&d1);& //强制类型转换&
pBase&=&static_cast&Animal&*&(&t1);&
//&C++编译器会做类型检查
pBase&=&reinterpret_cast&Animal&*&(&t1);&& //reinterpret_cast&重新解释&....强制类转换的味道
chunli@http://.~/c++$&g++&&-g&-o&run&main.cpp&&&&./run&
main.cpp:&In&function&‘int&main()’:
main.cpp:46:35:&error:&invalid&static_cast&from&type&‘Tree*’&to&type&‘Animal*’
&&pBase&=&static_cast&Animal&*&(&t1);&&&//&C++编译器会做类型检查
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&^
chunli@http://.~/c++$static_cast &reinterpret_cast 转换的对比屏蔽 static_cast编译就通过chunli@http://.~/c++$&cat&main.cpp&
#include&&iostream&
using&namespace&
class&Tree&{};
class&Animal
virtual&void&cry()&=&0;
class&Dog&:&public&Animal
virtual&void&cry()
cout&&&&"汪汪"&&&&
void&doHome()
cout&&&&"看家"&&&&
class&Cat&:&public&Animal
virtual&void&cry()
cout&&&&"喵喵"&&&&
void&doThing()
cout&&&&"抓老鼠"&&&&
int&main()
Animal&*pBase&=&&d1;
pBase&=&static_cast&Animal&*&(&d1);&
//让C++编译在编译的时候进行&类型检查&
pBase&=&reinterpret_cast&Animal&*&(&d1);& //强制类型转换&
//pBase&=&static_cast&Animal&*&(&t1);&
//&C++编译器会做类型检查
pBase&=&reinterpret_cast&Animal&*&(&t1);&& //reinterpret_cast&重新解释&....强制类转换的味道
chunli@http://.~/c++$&g++&&-g&-o&run&main.cpp&&&&./run&
chunli@http://.~/c++$类型转换4 const_cast将常量类型转换为变量经典案例chunli@http://.~/c++$&cat&main.cpp&
#include&&iostream&
using&namespace&
void&fun(const&char&*p)
char&*p2&=&const_cast&char&*&(p);
p2[0]&=&'A';
int&main()
char&p1[]&=&"Hello&World!";
cout&&&&p1&&&&
chunli@http://.~/c++$&g++&&-g&-o&run&main.cpp&&&&./run&
Aello&World!
chunli@http://.~/c++$类型转换4 const_cast修改无法修改的内存直接宕掉chunli@http://.~/c++$&cat&main.cpp&
#include&&iostream&
using&namespace&
void&fun(const&char&*p)
char&*p2&=&const_cast&char&*&(p);
p2[0]&=&'A';
int&main()
const&char&&*p1&=&"Hello&World!";
cout&&&&p1&&&&
chunli@http://.~/c++$&g++&&-g&-o&run&main.cpp&&&&./run&
Segmentation&fault&(core&dumped)
chunli@http://.~/c++$总结结论1程序员要清除的知道: 要转的变量类型转换前是什么类型类型转换后是什么类型。转换后有什么后果。结论2一般情况下不建议进行类型转换避免进行类型转换。异常1异常是一种程序控制机制与函数机制独立和互补  函数是一种以栈结构展开的上下函数衔接的程序控制系统,异常是另一种控制结构,它依附于栈结构,却可以同时设置多个异常类型作为网捕条件,从而以类型匹配在栈机制中跳跃回馈.2异常设计目的& &栈机制是一种高度节律性控制机制,面向对象编程却要求对象之间有方向、有目的的控制传动,从一开始异常就是冲着改变程序控制结构以适应面向对象程序更有效地工作这个主题而不是仅为了进行错误处理。异常设计出来之后却发现在错误处理方面获得了最大的好处异常将严格按照类型匹配异常的基本思想1C++的异常处理机制使得异常的引发和异常的处理不必在同一个函数中这样底层的函数可以着重解决具体问题而不必过多的考虑异常的处理。上层调用者可以再适当的位置设计对不同类型异常的处理。2异常是专门针对抽象编程中的一系列错误处理的C++中不能借助函数机制因为栈结构的本质是先进后出依次访问无法进行跳跃但错误处理的特征却是遇到错误信息就想要转到若干级之上进行重新尝试如图1基本语法1 若有异常则通过throw操作创建一个异常对象并抛掷。2 将可能抛出异常的程序段嵌在try块之中。控制通过正常的顺序执行到达try语句然后执行try块内的保护段。3 如果在保护段执行期间没有引起异常那么跟在try块后的catch子句就不执行。程序从try块后跟随的最后一个catch子句后面的语句继续执行下去。4 catch子句按其在try块后出现的顺序被检查。匹配的catch子句将捕获并处理异常或继续抛掷异常。5 如果匹配的处理器未找到则运行函数terminate将被自动调用其缺省功能是调用abort终止程序。6处理不了的异常可以在catch的最后一个分支使用throw语法向上扔。&chunli@http://.~/c++$&cat&main.cpp&
#include&&iostream&
using&namespace&
void&divide(int&x,&int&y)
if&(y&==&0)
throw&x;&//抛出&int类型&异常
cout&&&&"divide运算结果:"&&&&x/y&&&
int&main()
divide(10,&2);
divide(100,&0);
catch&(int&e)
cout&&&&e&&&&"除数是零"&&&&
catch&(&...&)&&
cout&&&&&"其他未知类型异常&"&&&
chunli@http://.~/c++$&g++&&-g&-o&run&main.cpp&&&&./run&
divide运算结果:5
100除数是零
chunli@http://.~/c++$2发送异常不去接收程序会宕掉chunli@http://.~/c++$&cat&main.cpp&
#include&&iostream&
using&namespace&
void&divide(int&x,&int&y)
if&(y&==&0)
throw&x;&//抛出&int类型&异常
cout&&&&"divide运算结果:"&&&&x/y&&&
int&main()
divide(100,&0);
chunli@http://.~/c++$&g++&&-g&-o&run&main.cpp&&&&./run&
terminate&called&after&throwing&an&instance&of&'int'
Aborted&(core&dumped)
chunli@http://.~/c++$函数接收到异常继续往外抛chunli@http://.~/c++$&cat&main.cpp&
#include&&iostream&
using&namespace&
void&divide(int&x,&int&y)
if&(y&==&0)
throw&x;&//抛出&int类型&异常
cout&&&&"divide运算结果:"&&&&x/y&&&
void&myDivide(int&x,&int&y)
divide(x,&y);
catch&(...)
cout&&&&"我接受了&divide的异常&但是我没有处理&我向上抛出"&&&&
int&main()
myDivide(100,&0);
catch&(int&e)
cout&&&&e&&&&"除数是0"&&&&
catch&(&...&)&&//
cout&&&&&"其他未知类型异常&"&&&
chunli@http://.~/c++$&g++&&-g&-o&run&main.cpp&&&&./run&
我接受了&divide的异常&但是我没有处理&我向上抛出
100除数是0
chunli@http://.~/c++$异常可以跨函数hunli@http://.~/c++$&cat&main.cpp&
#include&&iostream&
using&namespace&
void&divide(int&x,&int&y)
if&(y&==&0)
throw&x;&//抛出&int类型&异常
cout&&&&"divide运算结果:"&&&&x/y&&&
void&myDivide(int&x,&int&y)
divide(x,&y);
int&main()
myDivide(100,&0);
catch&(int&e)
cout&&&&"除数是"&&&e&&&&"&被除数是0"&&&&
catch&(&...&)&&//
cout&&&&&"其他未知类型异常&"&&&
chunli@http://.~/c++$&g++&&-g&-o&run&main.cpp&&&&./run&
除数是100&被除数是0
chunli@http://.~/c++$异常1栈解旋可以执行类的析构函数chunli@http://.~/c++$&cat&main.cpp&
#include&&iostream&
using&namespace&
class&Test
Test(int&a=0,&int&b=0)
this-&a&=&a;
this-&b&=&b;
cout&&&&"构造函数do&\n";
cout&&&&"析构函数do&\n";
void&myDivide()&
Test&t1(1,&2),&t2(3,&4);
cout&&&&"myDivide&...要发生异常\n"&;
cout&&&&"这句话没有机会执行\n"&;
int&main()
myDivide();
catch&(int&e)
cout&&&&"除数是"&&&e&&&&"&被除数是0"&&&&
catch&(&...&)&&//
cout&&&&&"其他未知类型异常&"&&&
chunli@http://.~/c++$&g++&&-g&-o&run&main.cpp&&&&./run&
构造函数do&
构造函数do&
myDivide&...要发生异常
析构函数do&
析构函数do&
除数是1&被除数是0
chunli@http://.~/c++$异常接口说明1指定抛出异常的种类chunli@http://.~/c++$&cat&main.cpp&
#include&&iostream&
using&namespace&
void&fun1()&throw(int&,char,char&*)
void&fun2()&throw()
void&fun3()&
throw&3.14;
int&main()
catch&(int)
cout&&&"int&类型"&&&&
catch&(double)
cout&&&"double&类型"&&&&
catch&(&...&)&&
cout&&&&&"其他未知类型异常&"&&&
chunli@http://.~/c++$&g++&&-g&-o&run&main.cpp&&&&./run&
chunli@http://.~/c++$异常接口说明2 不允许抛出异常chunli@http://.~/c++$&cat&main.cpp&
#include&&iostream&
using&namespace&
void&fun1()&throw(int&,char,char&*)
void&fun2()&throw()
void&fun3()&
throw&3.14;
int&main()
catch&(int)
cout&&&"int&类型"&&&&
catch&(double)
cout&&&"double&类型"&&&&
catch&(&...&)&&
cout&&&&&"其他未知类型异常&"&&&
chunli@http://.~/c++$&g++&&-g&-o&run&main.cpp&&&&./run&
terminate&called&after&throwing&an&instance&of&'int'
Aborted&(core&dumped)
chunli@http://.~/c++$异常接口说明3不限制抛出异常chunli@http://.~/c++$&cat&main.cpp&
#include&&iostream&
using&namespace&
void&fun1()&throw(int&,char,char&*)
void&fun2()&throw()
void&fun3()&
throw&3.14;
int&main()
catch&(int)
cout&&&"int&类型"&&&&
catch&(double)
cout&&&"double&类型"&&&&
catch&(&...&)&&
cout&&&&&"其他未知类型异常&"&&&
chunli@http://.~/c++$&g++&&-g&-o&run&main.cpp&&&&./run&
double&类型
chunli@http://.~/c++$异常类型生命周期 1传统的函数报错方式chunli@http://.~/c++$&cat&main.cpp&
#include&&iostream&
using&namespace&
//传统的错误处理机制,假设字符串首字母是a就报错
int&my_strcpy(char&*to,&char&*from)
if&(from&==&NULL)
if&(to&==&NULL)
if&(*from&==&'a')
cout&&&&"处理类型异常&\n";
while&(*from&!=&'\0')
*to&=&'\0';
int&main()
int&ret&=&0;
char&buf1[]&=&"abcdefg";
char&buf2[1024]&=&{0};
& ret&=&my_strcpy(buf2,&buf1);
if&(ret&!=&0)
switch(ret)
cout&&&&"源buf出错!\n";
cout&&&"目的buf出错!\n";
cout&&&&"copy过程出错!\n";
cout&&&"未知错误!\n";
cout&&&&"buf2:"&&&&buf2;
return&&0;
chunli@http://.~/c++$&g++&&-g&-o&run&main.cpp&&&&./run&
处理类型异常&
copy过程出错!
buf2:chunli@http://.~/c++$异常类型生命周期 2普通的异常抛出chunli@http://.~/c++$&cat&main.cpp&
#include&&iostream&
using&namespace&
void&my_strcpy(char&*to,&char&*from)
if&(from&==&NULL)
if&(to&==&NULL)
throw&1.1;
if&(*from&==&'a')
cout&&&&"处理类型异常&\n";
throw&'A';
while&(*from&!=&'\0')
*to&=&'\0';
int&main()
int&ret&=&0;
char&buf1[]&=&"abcdefg";
char&buf2[1024]&=&{0};
my_strcpy(buf2,&buf1);
catch&(int&e)&//e可以写&也可以不写
cout&&&&e&&&&"int类型异常"&&&&
catch(char&&e)
cout&&&&"char类型异常"&&&&
catch(char&*e)
cout&&&&e&&&&"char*&类型异常"&&&&
catch(...)
cout&&&"&.............&"&&&&
return&&0;
chunli@http://.~/c++$&g++&&-g&-o&run&main.cpp&&&&./run&
处理类型异常&
char类型异常
chunli@http://.~/c++$异常类型生命周期 3指针类型chunli@http://.~/c++$&cat&main.cpp&
#include&&iostream&
using&namespace&
void&my_strcpy(char&*to,&char&*from)
if&(from&==&NULL)
throw&"哈哈1处异常";
if&(to&==&NULL)
throw&"哈哈2处异常";
if&(*from&==&'a')
throw&"哈哈3处异常";
int&main()
char&p1[]&=&"apple";
char&p2[10]&=&{0};
my_strcpy(p2,p1);
catch(const&char&*e)
cout&&&&e&&&&&
catch(...)
cout&&&".............&"&&&&
return&&0;
chunli@http://.~/c++$&g++&&-g&-o&run&main.cpp&&&&./run&
哈哈3处异常
chunli@http://.~/c++$异常类型生命周期 3类异常chunli@http://.~/c++$&cat&main.cpp&
#include&&iostream&
using&namespace&
class&a{};
class&b{};
class&c{};
void&my_strcpy(char&*to,&char&*from)
if&(from&==&NULL)
throw&a();//抛出匿名对象
if&(to&==&NULL)
throw&b();
if&(*from&==&'a')
throw&c();
int&main()
char&p1[]&=&"apple";
char&p2[10]&=&{0};
my_strcpy(p2,p1);
catch(a&e)
cout&&&&"a&type&error&"&&&&&
catch(b&e)
cout&&&&"b&type&error&"&&&&&
catch(c&e)
cout&&&&"c&type&error&"&&&&&
catch(...)
cout&&&".............&"&&&&
return&&0;
chunli@http://.~/c++$&g++&&-g&-o&run&main.cpp&&&&./run&
c&type&error&
chunli@http://.~/c++$问题引出catch(a e)catch(b e)catch(c e)这个e是谁抛出的是把匿名的值拷给e 还是引用给e【复杂思考】catch(c *e)catch(c &e)【结论】把抛出的匿名对象拷贝给e对象【证明】结论1: 如果 接受异常的时候 使用一个异常变量,则copy构造异常变量.chunli@http://.~/c++$&cat&main.cpp&
#include&&iostream&
using&namespace&
cout&&&&"构造函数&\n";
c(const&c&&obj)
cout&&&&"拷贝函数&\n";
cout&&&&"析构函数&\n";
void&my_strcpy()
throw&c();
int&main()
my_strcpy();
catch(c&e)
cout&&&&"c&type&error&"&&&&&
catch(...)
cout&&&".............&"&&&&
return&&0;
chunli@http://.~/c++$&g++&&-g&-o&run&main.cpp&&&&./run&
c&type&error&
chunli@http://.~/c++$结论2: 使用引用的话 会使用throw时候的那个对象chunli@http://.~/c++$&cat&main.cpp&
#include&&iostream&
using&namespace&
cout&&&&"构造函数&\n";
c(const&c&&obj)
cout&&&&"拷贝函数&\n";
cout&&&&"析构函数&\n";
void&my_strcpy()
throw&c();
int&main()
my_strcpy();
catch(c&&e)
cout&&&&"c&type&error&"&&&&&
catch(...)
cout&&&".............&"&&&&
return&&0;
chunli@http://.~/c++$&g++&&-g&-o&run&main.cpp&&&&./run&
c&type&error&
chunli@http://.~/c++$结论3: 指针可以和引用/元素写在一块 但是引用/元素不能写在一块chunli@http://.~/c++$&cat&main.cpp&
#include&&iostream&
using&namespace&
cout&&&&"构造函数&\n";
c(const&c&&obj)
cout&&&&"拷贝函数&\n";
cout&&&&"析构函数&\n";
void&my_strcpy()
throw&c();
int&main()
my_strcpy();
catch(c&e)
cout&&&&"c&type&error&"&&&&&
catch(c&&e)
cout&&&&"c&type&error&"&&&&&
catch(...)
cout&&&".............&"&&&&
return&&0;
chunli@http://.~/c++$&g++&&-g&-o&run&main.cpp&&&&./run&
main.cpp:&In&function&‘int&main()’:
main.cpp:33:2:&warning:&exception&of&type&‘c’&will&be&caught&[enabled&by&default]
&&catch(c&&e)
main.cpp:29:2:&warning:&&&&by&earlier&handler&for&‘c’&[enabled&by&default]
&&catch(c&e)
c&type&error&
chunli@http://.~/c++$结论3: 指针可以和引用/元素写在一块 但是引用/元素不能写在一块chunli@http://.~/c++$&cat&main.cpp&
#include&&iostream&
using&namespace&
cout&&&&"构造函数&\n";
c(const&c&&obj)
cout&&&&"拷贝函数&\n";
cout&&&&"析构函数&\n";
void&my_strcpy()
throw&new&c();
int&main()
my_strcpy();
catch(c&e)
cout&&&&"c&type&error&"&&&&&
catch(c&*e)
cout&&&&"c*&type&error&"&&&&&
catch(...)
cout&&&".............&"&&&&
return&&0;
chunli@http://.~/c++$&g++&&-g&-o&run&main.cpp&&&&./run&
c*&type&error&
chunli@http://.~/c++$&
出现了野指针需要显式调用deletechunli@http://.~/c++$&cat&main.cpp&
#include&&iostream&
using&namespace&
cout&&&&"构造函数&\n";
c(const&c&&obj)
cout&&&&"拷贝函数&\n";
cout&&&&"析构函数&\n";
void&my_strcpy()
throw&new&c();
int&main()
my_strcpy();
catch(c&e)
cout&&&&"c&type&error&"&&&&&
catch(c&*e)
cout&&&&"c*&type&error&"&&&&&
catch(...)
cout&&&".............&"&&&&
return&&0;
chunli@http://.~/c++$&g++&&-g&-o&run&main.cpp&&&&./run&
c*&type&error&
chunli@http://.~/c++$【综合以上关于抛出类】结论4: 类对象时, 使用引用比较合适【异常的层次结构?老方法】chunli@http://.~/c++$&cat&main.cpp&
#include&&iostream&
#include&&stdio.h&
using&namespace&
class&MyArray
MyArray(int&len);
~MyArray();
int&&&operator[](int&index);
int&getLen();
class&eSize
eSize(int&size)
virtual&void&printErr()
cout&&&&"size:"&&&&m_size&&&&"&";
protected:
class&eNegative&:&public&eSize
eNegative(int&size)&:&eSize(size)
virtual&void&printErr()
cout&&&&"eNegative&类型&size:"&&&&m_size&&&&"&";
class&eZero&:&public&eSize
eZero(int&size)&:&eSize(size)
virtual&void&printErr()
cout&&&&"eZero&类型&size:"&&&&m_size&&&&"&";
class&eTooBig&:&public&eSize
eTooBig(int&size)&:&eSize(size)
virtual&void&printErr()
cout&&&&"eTooBig&类型&size:"&&&&m_size&&&&"&";
class&eTooSmall&:&public&eSize
eTooSmall(int&size)&:&eSize(size)
virtual&void&printErr()
cout&&&&"eTooSmall&类型&size:"&&&&m_size&&&&"&";
MyArray::MyArray(int&len)
if&(len&&&&0)
throw&eNegative(len);
else&if&(len&==&0)
throw&eZero(len);
else&if&(len&&&1000)
throw&eTooBig(len);
else&if&(len&&&3)
throw&eTooSmall(len);
m_space&=&new&int[len];
MyArray::~MyArray()
if&(m_space&!=&NULL)
delete&[]&m_
m_space&=&NULL;
m_len&=&0;
int&&&MyArray::operator[](int&index)
return&m_space[index];
int&MyArray::getLen()
int&main()
MyArray&a(-5);
for&(int&i=0;&i&a.getLen();&i++)
a[i]&=&i+1;
printf("%d&",&a[i]);
catch(MyArray::eNegative&e)
cout&&&&"eNegative&类型异常"&&&&
catch(MyArray::eZero&e)
cout&&&&"eZero&类型异常"&&&&
catch(MyArray::eTooBig&e)
cout&&&&"eTooBig&类型异常"&&&&
catch(MyArray::eTooSmall&e)
cout&&&&"eTooSmall&类型异常"&&&&
catch&(...)
return&0&;
chunli@http://.~/c++$&g++&&-g&-o&run&main.cpp&&&&./run&
eNegative&类型异常
chunli@http://.~/c++$【异常的层次结构?推荐方法】类的多态实现chunli@http://.~/c++$&cat&main.cpp&
#include&&iostream&
#include&&stdio.h&
using&namespace&
class&MyArray
MyArray(int&len);
~MyArray();
int&&&operator[](int&index);
int&getLen();
class&eSize
eSize(int&size)
virtual&void&printErr()
cout&&&&"size:"&&&&m_size&&&&"&\n";
protected:
class&eNegative&:&public&eSize
eNegative(int&size)&:&eSize(size)
virtual&void&printErr()
cout&&&&"eNegative&类型&size:"&&&&m_size&&&&"&\n";
class&eZero&:&public&eSize
eZero(int&size)&:&eSize(size)
virtual&void&printErr()
cout&&&&"eZero&类型&size:"&&&&m_size&&&&"&\n";
class&eTooBig&:&public&eSize
eTooBig(int&size)&:&eSize(size)
virtual&void&printErr()
cout&&&&"eTooBig&类型&size:"&&&&m_size&&&&"&\n";
class&eTooSmall&:&public&eSize
eTooSmall(int&size)&:&eSize(size)
virtual&void&printErr()
cout&&&&"eTooSmall&类型&size:"&&&&m_size&&&&"&\n";
MyArray::MyArray(int&len)
if&(len&&&&0)
throw&eNegative(len);
else&if&(len&==&0)
throw&eZero(len);
else&if&(len&&&1000)
throw&eTooBig(len);
else&if&(len&&&3)
throw&eTooSmall(len);
m_space&=&new&int[len];
MyArray::~MyArray()
if&(m_space&!=&NULL)
delete&[]&m_
m_space&=&NULL;
m_len&=&0;
int&&&MyArray::operator[](int&index)
return&m_space[index];
int&MyArray::getLen()
int&main()
MyArray&a(-5);
for&(int&i=0;&i&a.getLen();&i++)
a[i]&=&i+1;
printf("%d&",&a[i]);
catch(MyArray::eSize&&e)
//cout&&&&&"len的大小:&"&&&&e.eSize();
e.printErr();
catch&(...)
return&0&;
chunli@http://.~/c++$&g++&&-g&-o&run&main.cpp&&&&./run&
eNegative&类型&size:-5&
chunli@http://.~/c++$C++标准程序库异常见图演示chunli@http://.~/c++$&cat&main.cpp&
#include&"iostream"
using&namespace&
#include&&stdexcept&&
#include&"string"
class&Teacher
Teacher(int&age)
if&(age&&&100)
throw&out_of_range("年龄太大");
this-&age&=&
int&&main()
Teacher&t1(102);
catch&(out_of_range&e)
cout&&&&e.what()&&&&
chunli@http://.~/c++$&g++&&-g&-o&run&main.cpp&&&&./run&
chunli@http://.~/c++$继承C++编制异常库实现函数异常chunli@http://.~/c++$&cat&main.cpp&
#include&"iostream"
using&namespace&
#include&&stdexcept&&
#include&"string"
class&MyException&:&public&exception
MyException(const&char&*p)
this-&m_p&=&p;
virtual&const&char&*&what()
cout&&&&"MyException:&类型&"&&&&m_p&&&&
return&m_p;
const&char&*m_p;
void&testMyExcept()
throw&MyException("函数异常");
int&&main()
testMyExcept();
catch&(MyException&&&e)
catch&(...)
cout&&&&"未知&类型&"&&&&
chunli@http://.~/c++$&g++&&-g&-o&run&main.cpp&&&&./run&
MyException:&类型&函数异常
chunli@http://.~/c++$IO流I/O流的概念和流类库的结构程序的输入指的是从输入文件将数据传送给程序程序的输出指的是从程序将数据传送给输出文件。C++输入输出包含以下三个方面的内容&&& 对系统指定的标准设备的输入和输出。即从键盘输入数据输出到显示器屏幕。这种输入输出称为标准的输入输出简称标准I/O。&&& 以外存磁盘文件为对象进行输入和输出即从磁盘文件输入数据数据输出到磁盘文件。以外存文件为对象的输入输出称为文件的输入输出简称文件I/O。&&& 对内存中指定的空间进行输入和输出。通常指定一个字符数组作为存储空间(实际上可以利用该空间存储任何信息)。这种输入和输出称为字符串输入输出简称串I/O。&C++的I/O对C的发展--类型安全和可扩展性&&&&&&&& 在C语言中用printf和scanf进行输入输出往往不能保证所输入输出的数据是可靠的安全的。在C++的输入输出中编译系统对数据类型进行严格的检查凡是类型不正确的数据都不可能通过编译。因此C++的I/O操作是类型安全(type safe)的。C++的I/O操作是可扩展的不仅可以用来输入输出标准类型的数据也可以用于用户自定义类型的数据。&&&&&&&& C++通过I/O类库来实现丰富的I/O功能。这样使C++的输人输出明显地优于C 语言中的printf和scanf但是也为之付出了代价C++的I/O系统变得比较复杂要掌握许多细节。&&&&&&&& C++编译系统提供了用于输入输出的iostream类库。iostream这个单词是由3个部分组成的即i-o-stream意为输入输出流。在iostream类库中包含许多用于输入输出的类。常用的见表ios是抽象基类由它派生出istream类和ostream类两个类名中第1个字母i和o分别代表输入(input)和输出(output)。 istream类支持输入操作ostream类支持输出操作iostream类支持输入输出操作。iostream类是从istream类和ostream类通过多重继承而派生的类。其继承层次见上图表示。C++对文件的输入输出需要用ifstrcam和ofstream类两个类名中第1个字母i和o分别代表输入和输出第2个字母f代表文件 (file)。ifstream支持对文件的输入操作ofstream支持对文件的输出操作。类ifstream继承了类istream类ofstream继承了类ostream类fstream继承了类iostream。见图I/O类库中还有其他一些类但是对于一般用户来说以上这些已能满足需要了。&与iostream类库有关的头文件iostream类库中不同的类的声明被放在不同的头文件中用户在自己的程序中用#include命令包含了有关的头文件就相当于在本程序中声明了所需 要用到的类。可以换―种说法头文件是程序与类库的接口iostream类库的接口分别由不同的头文件来实现。常用的有iostream & & &包含了对输入输出流进行操作所需的基本信息。fstream & & &用于用户管理的文件的I/O操作。strstream & & &用于字符串流I/O。stdiostream & & &用于混合使用C和C & & + +的I/O机制时例如想将C程序转变为C++程序。iomanip & & &在使用格式化I/O时应包含此头文件。在iostream头文件中定义的流对象在 iostream 头文件中定义的类有 iosistreamostreamiostreamistream _withassign ostream_withassigniostream_withassign 等。&在iostream头文件中不仅定义了有关的类还定义了4种流对象&对象含义对应设备对应的类c语言中相应的标准文件cin标准输入流键盘istream_withassignstdincout标准输出流屏幕ostream_withassignstdoutcerr标准错误流屏幕ostream_withassignstderrclog标准错误流屏幕ostream_withassignstderr&在iostream头文件中定义以上4个流对象用以下的形式以cout为例& & ostream cout ( stdout);在定义cout为ostream流类对象时把标准输出设备stdout作为参数这样它就与标准输出设备(显示器)联系起来如果有& & cout &&3;就会在显示器的屏幕上输出3。&在iostream头文件中重载运算符“&&”和“&&”本来在C++中是被定义为左位移运算符和右位移运算符的由于在iostream头文件中对它们进行了重载使它们能用作标准类型数据的输入和输出运算符。所以在用它们的程序中必须用#include命令把iostream包含到程序中。& & #include &iostream&&&a表示将数据放入a对象中。&&a表示将a对象中存储的数据拿出。标准I/O流标准I/O对象:cincoutcerrclogcout流对象cont是console output的缩写意为在控制台终端显示器的输出。强调几点。1) cout不是C++预定义的关键字它是ostream流类的对象在iostream中定义。 顾名思义流是流动的数据cout流是流向显示器的数据。cout流中的数据是用流插入运算符“&&”顺序加入的。如果有& & cout&&"I "&&"study C++"&&"very hard. && “wang bao ming ";按顺序将字符串"I ", "study C++ ", "very hard."插人到cout流中cout就将它们送到显示器在显示器上输出字符串"I study C++ very hard."。cout流是容纳数据的载体它并不是一个运算符。人们关心的是cout流中的内容也就是向显示器输出什么。2)用“ccmt&&”输出基本类型的数据时可以不必考虑数据是什么类型系统会判断数据的类型并根据其类型选择调用与之匹配的运算符重载函数。这个过程都是自动的用户不必干预。如果在C语言中用prinf函数输出不同类型的数据必须分别指定相应的输出格式符十分麻烦而且容易出错。C++的I/O机制对用户来说显然是方便而安全的。3) cout流在内存中对应开辟了一个缓冲区用来存放流中的数据当向cout流插人一个endl时不论缓冲区是否已满都立即输出流中所有数据然后插入一个换行符并刷新流清空缓冲区。注意如果插人一个换行符”\n“如cout&&a&&"\n"则只输出和换行而不刷新cout 流(但并不是所有编译系统都体现出这一区别。4) 在iostream中只对"&&"和"&&"运算符用于标准类型数据的输入输出进行了重载但未对用户声明的类型数据的输入输出进行重载。如果用户声明了新的类型并希望用"&&"和"&&"运算符对其进行输入输出按照重运算符重载来做。cerr流对象cerr流对象是标准错误流cerr流已被指定为与显示器关联。cerr的作用是向标准错误设备(standard error device)输出有关出错信息。cerr与标准输出流cout的作用和用法差不多。但有一点不同cout流通常是传送到显示器输出但也可以被重定向输出到磁盘文件而cerr流中的信息只能在显示器输出。当调试程序时往往不希望程序运行时的出错信息被送到其他文件而要求在显示器上及时输出这时应该用cerr。cerr流中的信息是用户根据需要指定的。clog流对象clog流对象也是标准错误流它是console log的缩写。它的作用和cerr相同都是在终端显示器上显示出错信息。区别cerr是不经过缓冲区直接向显示器上输出有关信息而clog中的信息存放在缓冲区中缓冲区满后或遇endl时向显示器输出。&缓冲区的概念:人们在输入输出时有一些特殊的要求如在输出实数时规定字段宽度只保留两位小数数据向左或向右对齐等。C++提供了在输入输出流中使用的控制符(有的书中称为操纵符)用流对象的成员函数控制输出格式除了可以用控制符来控制输出格式外还可以通过调用流对象cout中用于控制输出格式的成员函数来控制输出格式。用于控制输出格式的常用的成员函数如下流成员函数setf和控制符setiosflags括号中的参数表示格式状态它是通过格式标志来指定的。格式标志在类ios中被定义为枚举值。因此在引用这些格式标志时要在前面加上类名ios和域运算符“::”。文件流类和文件流对象输入输出是以系统指定的标准设备输入设备为键盘输出设备为显示器为对象的。在实际应用中常以磁盘文件作为对象。即从磁盘文件读取数据将数据输出到磁盘文件。和文件有关系的输入输出类主要在fstream.h这个头文件中被定义在这个头文件中主要被定义了三个类由这三个类控制对文件的各种输入输出操作他们分别是ifstream、ofstream、fstream其中fstream类是由iostream类派生而来他们之间的继承关系见下图所示。C++文件的打开与关闭打开文件所谓打开(open)文件是一种形象的说法如同打开房门就可以进入房间活动一样。打开文件是指在文件读写之前做必要的准备工作包括1为文件流对象和指定的磁盘文件建立关联以便使文件流流向指定的磁盘文件。2指定文件的工作方式如该文件是作为输入文件还是输出文件是ASCII文件还是二进制文件等。以上工作可以通过两种不同的方法实现。&1) 调用文件流的成员函数open。如&&&& //定义ofstream类(输出文件流类)对象outfile&&&outfile.open("f1.dat",ios::out);& //使文件流与f1.dat文件建立关联第2行是调用输出文件流的成员函数open打开磁盘文件f1.dat并指定它为输出文件文件流对象outfile将向磁盘文件f1.dat输出数据。ios::out是I/O模式的一种表示以输出方式打开一个文件。或者简单地说此时f1.dat是一个输出文件接收从内存输出的数据。&调用成员函数open的一般形式为&&& 文件流对象.open(磁盘文件名, 输入输出方式);磁盘文件名可以包括路径如"c:\new\\f1.dat"如缺省路径则默认为当前目录下的文件。&2) 在定义文件流对象时指定参数在声明文件流类时定义了带参数的构造函数其中包含了打开磁盘文件的功能。因此可以在定义文件流对象时指定参数调用文件流类的构造函数来实现打开文件的功能。如&&&ostream outfile("f1.dat",ios::out); 一般多用此形式比较方便。作用与open函数相同。输入输出方式是在ios类中定义的它们是枚举常量有多种选择案例2自学扩展思路ofstream类的默认构造函数原形为ofstream::ofstream(constchar *filename, intmode = ios::out,&int penprot = filebuf::openprot);&?filename  要打开的文件名?mode    要打开文件的方式?prot    打开文件的属性  其中mode和openprot这两个参数的可选项表见下表mode属性表ios::app以追加的方式打开文件ios::ate文件打开后定位到文件尾ios:app就包含有此属性ios::binary以二进制方式打开文件缺省的方式是文本方式。两种方式的区别见前文ios::in文件以输入方式打开ios::out文件以输出方式打开ios::trunc如果文件存在把文件长度设为0&可以用“|”把以上属性连接起来如ios::out|ios::binary。openprot属性表属性含义0普通文件打开访问1只读文件2隐含文件4系统文件可以用“或”或者“+”把以上属性连接起来 如3或1|2就是以只读和隐含属性打开文件。#include &fstream&
& &int main()&
&&&&&&&& ofstream &myfile("c:\\1.txt",ios::out|ios::trunc,0);
&&&&&&&& &myfile&&"Hello&"&&endl&&"网址"&&"@http://.n";
&&&&&&&& myfile.close()
&&&&&&&& system("pause");
&}&文件使用完后可以使用close成员函数关闭文件。ios::app为追加模式在使用追加模式的时候同时进行文件状态的判断是一个比较好的习惯。#include &iostream&
&#include &fstream&
&int main()&
&&&&&&&& ofstream &myfile("c:\\1.txt",ios::app,0);
&&&&&&&& if(!myfile)//或者写成myfile.fail()
&&&&&&&& {
&&&&&&&&&&&&&&&& cout&&"文件打开失败目标文件状态可能为只读";
&&&&&&&&&&&&&&&& &system("pause");
&&&&&&&&&&&&&&&& exit(1);
&&&&&&&& }
&&&&&&&& myfile&&"Hello"&&endl&&"网址"&&" &@http://. "&&
&&&&&&&& myfile.close();
&}&1基本输入输出chunli@http://.~/c++$&cat&main.cpp&
#include&"iostream"
using&namespace&
int&&main()
char mybuf[1024];
cin&&&&myI
cin&&&&myL
cin&&&&&//&遇见空格停止接受&数据&
cout&&&&"int="&&&&myInt&&&&
cout&&&&"long="&&&&myLong&&&&
cout&&&&"buf="&&&&mybuf&&&&
chunli@http://.~/c++$&g++&&-g&-o&run&main.cpp&&&&./run&
hello&world
int=123456
chunli@http://.~/c++$cin.get() 从缓冲区读取输出到屏幕存在IO阻塞chunli@http://.~/c++$&cat&main.cpp&
#include&&stdio.h&
#include&"iostream"
using&namespace&
int&&main()
while&(&(ch=cin.get()&)!=&EOF&)
ctrl&+&d结束输入
chunli@http://.~/c++$&g++&&-g&-o&run&main.cpp&&&&./run&
hello&world!
hello&world!
chunli@http://.~/c++$cin.get() 如果缓冲区有数据就自动读取chunli@http://.~/c++$&cat&main.cpp&
#include&&stdio.h&
#include&"iostream"
using&namespace&
int&&main()
char&a,&b,&c;
cout&&&&"cin.get(a)&如果缓冲区没有数据,则程序阻塞&\n";
cin.get(a);
cin.get(b);
cin.get(c);
cout&&&&a&&&&b&&&&c&&&&"因为缓冲区有数据,程序不会阻塞\n";
cin.get(a).get(b).get(c);
cout&&&&a&&&&b&&&&c&&&&;
chunli@http://.~/c++$&g++&&-g&-o&run&main.cpp&&&&./run&
cin.get(a)&如果缓冲区没有数据,则程序阻塞&
Hello&Linux!
Hel因为缓冲区有数据,程序不会阻塞
chunli@http://.~/c++$cin.getline 读取一行chunli@http://.~/c++$&cat&main.cpp&
#include&&stdio.h&
#include&"iostream"
using&namespace&
int&&main()
char&buf1[256];
char&buf2[256];
cout&&&&"请输入一个字符串&含有多个空格&aa&bb&cc&dd\n";
cin&&&&buf1;
cin.getline(buf2,&256);
cout&&&&"buf1:=&"&&&&buf1&&&&"buf2:=&"&&&&buf2&&&&&
chunli@http://.~/c++$&g++&&-g&-o&run&main.cpp&&&&./run&
请输入一个字符串&含有多个空格&aa&bb&cc&dd
aa&bb&cc&dd&ee&
buf1:=&aabuf2:=&&bb&cc&dd&ee&
chunli@http://.~/c++$cin.ignore函数作用是跳过输入流中n个字符或在遇到指定的终止字符时提前结束此时跳过包括终止字符在内的若干字符chunli@http://.~/c++$&cat&main.cpp&
#include&&stdio.h&
#include&"iostream"
using&namespace&
int&&main()
char&buf1[256];
char&buf2[256];
cout&&&&"请输入一个字符串&含有多个空格aa&&bbccdd\n";
cin&&&&buf1;
cout&&&&"buf1:"&&&&buf1&&&&
cin.ignore(3);
cin.getline(buf2,&256);
cout&&&&"buf2:"&&&&buf2&&&&&
chunli@http://.~/c++$&g++&&-g&-o&run&main.cpp&&&&./run&
请输入一个字符串&含有多个空格aa&&bbccdd
buf2:cdefg
chunli@http://.~/c++$cin.peek()查看缓冲区有没有内容chunli@http://.~/c++$&cat&main.cpp&
#include&&stdio.h&
#include&"iostream"
using&namespace&
int&&main()
cin.ignore(2);
int&myint&=&cin.peek();
cout&&&&"ASSIC="&&&&myint&&&&&
chunli@http://.~/c++$&g++&&-g&-o&run&main.cpp&&&&./run&
chunli@http://.~/c++$cin.putback 把收到的字符返回去可以再读一次chunli@http://.~/c++$&cat&main.cpp&
#include&&stdio.h&
#include&"iostream"
using&namespace&
int&&main()
cout&&&&"Please,&enter&a&number&or&a&word:&";
char&c&=&std::cin.get();
if&(&(c&&=&'0')&&&&(c&&=&'9')&)&//输入的整数和字符串&分开处理
int&n;&//整数不可能&中间有空格&使用cin&&&n
cin.putback&(c);
cout&&&&"You&entered&a&number:&"&&&&n&&&&'\n';
cin.putback&(c);
getline&(cin,&str);& //字符串&中间可能有空格&使用&cin.getline();
cout&&&&"You&entered&a&word:&"&&&&str&&&&'\n';
chunli@http://.~/c++$&g++&&-g&-o&run&main.cpp&&&&./run&
Please,&enter&a&number&or&a&word:&Hello&Linux!&&
You&entered&a&word:&Hello&Linux!
chunli@http://.~/c++$标准输出coutcout.put()输出单个字符chunli@http://.~/c++$&cat&main.cpp&
#include&&stdio.h&
#include&"iostream"
using&namespace&
int&&main()
cout&&&&"Hello"&&&&
cout.put('H').put('E').put('L').put('L').put('O').put('\n');
chunli@http://.~/c++$&g++&&-g&-o&run&main.cpp&&&&./run&
chunli@http://.~/c++$cout.write 标准输出chunli@http://.~/c++$&cat&main.cpp&
#include&&stdio.h&
#include&&string.h&
#include&"iostream"
using&namespace&
int&&main()
const&char&*p&=&"http://.";
cout.write(p,strlen(p))&&&&
cout.write(p,strlen(p)&-&10)&&&&
cout.write(p,strlen(p)&+&5)&&&&
chunli@http://.~/c++$&g++&&-g&-o&run&main.cpp&&&&./run&
http://.blog
chunli@http://.~/c++$cout 的格式化输出 1chunli@http://.~/c++$&cat&main.cpp&
#include&&iomanip&
#include&&stdio.h&
#include&"iostream"
using&namespace&
int&&main()
//使用类成员函数
cout&&&&"&start&";
cout.width(30);
//设置显示的宽度
cout.fill('*');
//设置宽度填充
cout.setf(ios::showbase);&//是显示进制&&#include&&iomanip&
//cout.setf(ios::internal);&//设置数值的符号左右对齐
cout&&&&hex&&&&123&&&&"&End&\n";
//使用控制符
cout& &&&"&Start&"&
&&&setw(30)&
&&&setfill('*')&
&&&setiosflags(ios::showbase)&//基数
&&&setiosflags(ios::internal)
&&&"&End&"
chunli@http://.~/c++$&g++&&-g&-o&run&main.cpp&&&&./run&
&start&**************************0x7b&End&
&Start&0x**************************7b&End&
chunli@http://.~/c++$cout格式输出2chunli@http://.~/c++$&cat&main.cpp&
#include&&iomanip&
#include&&stdio.h&
#include&"iostream"
using&namespace&
int&&main()
cout&&"input&a:";
cout&&"dec:"&&dec&&a&&&//以十进制形式输出整数
cout&&"hex:"&&hex&&a&&&//以十六进制形式输出整数a
cout&&"oct:"&&setbase(8)&&a&&&//以八进制形式输出整数a
const&char&*pt="China";&//pt指向字符串"China"
cout&&setw(10)&&pt&&&//指定域宽为,输出字符串
cout&&setfill('*')&&setw(10)&&pt&&&//指定域宽,输出字符串,空白处以'*'填充
double&pi=22.0/7.0;&//计算pi值
//按指数形式输出,8位小数
cout&&setiosflags(ios::scientific)&&setprecision(8);
cout&&"pi="&&pi&&&//输出pi值
cout&&"pi="&&setprecision(4)&&pi&&&//改为位小数
cout&&"pi="&&setiosflags(ios::fixed)&&pi&&&//改为小数形式输出
chunli@http://.~/c++$&g++&&-g&-o&run&main.cpp&&&&./run&
input&a:17
&&&&&China
*****China
pi=3.1429e+00
chunli@http://.~/c++$cout格式输出3chunli@http://.~/c++$&cat&main.cpp&
#include&&iomanip&
#include&&stdio.h&
#include&"iostream"
using&namespace&
int&&main()
double&a=123.456,b=3.14159,c=-3214.67;
cout&&setiosflags(ios::fixed)&&setiosflags(ios::right)&&setprecision(2);
cout&&setw(10)&&a&&
cout&&setw(10)&&b&&
cout&&setw(10)&&c&&
chunli@http://.~/c++$&g++&&-g&-o&run&main.cpp&&&&./run&
&&&&123.46
&&&&&&3.14
&&-3214.67
chunli@http://.~/c++$文件IO1文件写入chunli@http://.~/c++$&cat&main.cpp&
#include&"fstream"
#include&"iostream"
using&namespace&
int&&main()
const&char&*fname&=&"chunli.txt";
ofstream&fout(fname);&//建一个&输出流对象&和文件关联;&&
if&(!fout)
cout&&&&"打开文件失败"&&&&
return&-1;
fout&&&&"hello....111"&&&&
fout&&&&"hello....222"&&&&
fout&&&&"hello....333"&&&&
fout.close();
chunli@http://.~/c++$&g++&&-g&-o&run&main.cpp&&&&./run&
chunli@http://.~/c++$&cat&chunli.txt&
hello....111
hello....222
hello....333
chunli@http://.~/c++$文件写文件读操作chunli@http://.~/c++$&cat&main.cpp&
#include&"fstream"
#include&"iostream"
using&namespace&
int&&main()
const&char&*fname&=&"chunli.txt";
ofstream&fout(fname);&//建一个&输出流对象&和文件关联;&&
if&(!fout)
cout&&&&"打开文件失败"&&&&
return&-1;
fout&&&&"hello....111"&&&&
fout&&&&"hello....222"&&&&
fout&&&&"hello....333"&&&&
fout.close();
ifstream&fin(fname);&//建立一个输入流对象&和文件关联
while&(fin.get(ch))
cout&&&ch&;
fin.close();
chunli@http://.~/c++$&g++&&-g&-o&run&main.cpp&&&&./run&
hello....111
hello....222
hello....333
chunli@http://.~/c++$文件追加写入 fname,ios::appchunli@http://.~/c++$&cat&main.cpp&
#include&"fstream"
#include&"iostream"
using&namespace&
int&&main()
const&char&*fname&=&"chunli.txt";
ofstream&fout(fname,ios::app);&//建一个&输出流对象&和文件关联;&&
if&(!fout)
cout&&&&"打开文件失败"&&&&
return&-1;
fout&&&&"hello....111"&&&&
fout&&&&"hello....222"&&&&
fout&&&&"hello....333"&&&&
fout.close();
ifstream&fin(fname);&//建立一个输入流对象&和文件关联
while&(fin.get(ch))
cout&&&ch&;
fin.close();
chunli@http://.~/c++$&g++&&-g&-o&run&main.cpp&&&&./run&
hello....111
hello....222
hello....333
chunli@http://.~/c++$&g++&&-g&-o&run&main.cpp&&&&./run&
hello....111
hello....222
hello....333
hello....111
hello....222
hello....333
chunli@http://.~/c++$文件二进制的读写chunli@http://.~/c++$&cat&main.cpp&
#include&"string.h"
#include&"fstream"
#include&"iostream"
using&namespace&
class&Teacher
strcpy(name,&"");
Teacher(int&_age,&const&char&*_name)
strcpy(name,&_name);
void&printT()
cout&&&&"age="&&&&age&&&&"&name="&&&&name&&&
char&name[32];
int&&main()
const&char*&fname&=&"chunli.dat";
ofstream&fout(fname,&ios::binary);&//建一个&输出流对象&和文件关联;&&
if&(!fout)
cout&&&&"打开文件失败"&&&&
return&&-1;
Teacher&t1(31,&"C++之父");
Teacher&t2(32,&"Linux之父");
fout.write((char&*)&t1,&sizeof(Teacher));
fout.write((char&*)&t2,&sizeof(Teacher));
fout.close();
ifstream&fin(fname);&//建立一个输入流对象&和文件关联
fin.read(&(char*)&tmp,sizeof(Teacher)&); tmp.printT();
fin.read(&(char*)&tmp,sizeof(Teacher)&); tmp.printT();
fin.close();
chunli@http://.~/c++$&g++&&-g&-o&run&main.cpp&&&&./run&
age=31&name=C++之父
age=32&name=Linux之父
chunli@http://.~/c++$作业练习1 编程实现以下数据输入/输出& & (1)以左对齐方式输出整数,域宽为12。& & (2)以八进制、十进制、十六进制输入/输出整数。& & (3)实现浮点数的指数格式和定点格式的输入/输出,并指定精度。& & (4)把字符串读入字符型数组变量中,从键盘输入,要求输入串的空格也全部读入,以回车符结束。& & (5)将以上要求用流成员函数和操作符各做一遍。2编写一程序将两个文件合并成一个文件。3编写一程序统计一篇英文文章中单词的个数与行数。4编写一程序将C++源程序每行前加上行号与一个空格。4.5编写一程序输出 ASCII码值从20到127的ASCII码字符表格式为每行10个。参考答案第一题ios类成员函数实现#include&iostream&
#include&iomanip&
using&namespace&
int&main(){
long&a=234;
double&b=;
char&c[100];
cout.fill('*');
cout.flags(ios_base::left);
cout.width(12);
cout.fill('*');
cout.flags(ios::right);
cout.width(12);
cout.flags(ios.hex);
cout&&234&&'\t';
cout.flags(ios.dec);
cout&&234&&'\t';
cout.flags(ios.oct);
cout&&234&&
cout.flags(ios::scientific);
cout&&b&&'\t';
cout.flags(ios::fixed);
cin.get(c,99);
操作符实现
#include&iostream&
#include&iomanip&
using&namespace&
int&main(){
long&a=234;
double&b=;
char&c[100];
cout&&setfill('*');
cout&&left&&setw(12)&&a&&
cout&&right&&setw(12)&&a&&
cout&&hex&&a&&'\t'&&dec&&a&&'\t'&&oct&&a&&
cout&&scientific&&b&&'\t'&&fixed&&b&&
}第二题#include&iostream&
#include&fstream&
using&namespace&
int&main(){
char&c[1000];
ifstream&ifile1("D:\\1.cpp");
ifstream&ifile2("D:\\2.cpp");
ofstream&ofile("D:\\3.cpp");
while(!ifile1.eof()){
ifile1.getline(c,999);
ofile&&c&&
while(!ifile2.eof()){
ifile2.getline(c,999);
ofile&&c&&
ifile1.close();
ifile2.close();
ofile.close();
}第三题#include&iostream&
#include&fstream&
using&namespace&
bool&isalph(char);
int&main(){
ifstream&ifile("C:\\daily.doc");
char&text[1000];
bool&inword=
int&rows=0,words=0;
while(!ifile.eof()){
ifile.getline(text,999);
while(text[i]!=0){
if(!isalph(text[i]))
else&if(isalph(text[i])&&&&inword==false){
cout&&"rows=&"&&rows&&
cout&&"words=&"&&words&&
ifile.close&();
bool&isalph(char&c){
return&((c&='A'&&&&c&='Z')&||&(c&='a'&&&&c&='z'));
}第四题#include&iostream&
#include&fstream&
using&namespace&
int&main(){
char&c[1000];
ifstream&ifile("D:\\1.cpp");
ofstream&ofile("D:\\2.cpp");
while(!ifile.eof()){
ofile&&i++&&":&";
ifile.getline(c,999);
ofile&&c&&
ifile.close();
ofile.close();
}第五题#include&iostream&
using&namespace&
int&main(){
for(i=32;i&127;i++){
cout&&char(i)&&"&";
if(l%10==0)cout&&
}本文出自 “” 博客,谢绝转载!
了这篇文章
类别:┆阅读(0)┆评论(0)

我要回帖

更多关于 处方笺模板 的文章

 

随机推荐