C++捷径之七--结束

2024-01-07 21:18
文章标签 c++ 结束 之七 捷径

本文主要是介绍C++捷径之七--结束,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

// 异常处理 ,try,throw,catch
try{
  cout<<"Inside try block/n";
  throw
99 ;
  cout<<"This will not execute";
// throw 后,所以此句不执行
 }
 catch(int i){
  cout<<"Caught an exception--value is: "<<i<<endl;
// 得到 throw 扔出的值 99
 }
//throw 扔出的值 , catch 得到的值类型匹配
// 被扔出值后,程序就跳到 catch 语句进行处理。处理完后继续执行 catch 后面的语句。
class Myexception{
public:
 char str_what[80];
 Myexception() { *str_what=0; }
 Myexception(char *s) { strcpy(str_what,s); }
};// 定义一个类 Myexception
// 主函数中引用
 try{
  cout<<"Enter numerator and denominator: ";
  cin>>a>>b;
  if(!b)  throw Myexception("Cannot divide by zero!");
  else  cout<<"Quotient is: "<<a/b<<endl;
 }

 catch(Myexception e){
  cout<<e.str_what<<endl; }
// 由此处引用前面的扔出值
// 当有两个类,一个是另一个的派生类,此时,尽管扔出值可能是派生类,但是 catch 时定义的 // 基类也能接受,所以如果同时定义两个接受的类型,在前面的 catch 语句执行,将跳过后面的
catch(...){ }// 必须位于所有 catch 语句的后面。
// 另一种定义 throw 的类型确定
void Xhandler(int test) throw(int, char ,double)
{
 if(test==0)  throw test;
 if(test==1)  throw 'a';
 if(test==2)  throw 123.23;
}
// 用于判断是否分配空间成功
try{
 p=new int[32];//include <new>
 }
 catch(bad_alloc xa){
 cout<<"Allocation failure./n";
 return 1;
 }// 最后需要 delete []p; 释放空间
// 另一种判断方法
p=new(nothrow) int[32];
 if(!p){
 cout<<"Allocation failure./n";
 return 1;
 }
// 类中定义带有异常处理得 new
void *three_d::operator new(size_t size)
{
 void *p;
 
 cout<<"Allocating three_d object./n";
 p=malloc(size);
 if(!p){
  bad_alloc ba;
  throw ba;
 }
 return p;
}
//I/O 系统
ostream &operator<<( ostream &stream, three_d obj )
{
 stream << obj.x << ",";
 stream << obj.y << ",";
 stream << obj.z << "/n";
 return stream;
}// 重新释义 iostream 库中的的输出运算符 ostream<<
// 可以在类中定义友元
friend ostream &operator<<( ostream &stream, three_d obj);
istream &operator>>( istream &stream, three_d &obj)
{
 cout<<"Enter x,y,z values: ";
 stream >> obj.x >> obj.y >> obj.z;
 return stream;
}
// 重新释义 iostream 库中的的输出运算符 istream>>
// 明格式化 I/O
cout.setf( ios::showpos ); // 显示 +-
cout.setf( ios::scientific );
// 科学输入法
ios::fmtflags f;// 标准 IO 库中的 ios
cout.precision(2);
 cout.width(10);
 cout.fill('#');
#include <iomanip>
stream.setf(ios::right);
 stream<<setw(10)<<setfill('$');
// 写入一个文件 --12
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
 ofstream out("test "); 

//
创建 test 文件 . ofstream out("test .txt "); 创建的即为 test.txt 文本文件
 if(!out)
 {
  cout<<"cannot open file./n";
  return 1;
 }

 out<<10<<" "<<123.23<<endl;
 out<<"this is a short text file.";
//test 文件中写入 10,123.23,this is a short text file
 out.close();

  return 0;
}
int main()
{
 char ch;
 int i;
 float f;
 char str[80];
 ifstream in("test");
 if(!in)
 {
  cout<<"cannot open file./n";
  return 1;
 }
 in>>i;
 in>>f;
 in>>ch;
 in>>str;
// test 文件中读数据到 i,f,ch,str
  cout<<i<<" "<<f<<" "<<ch<<endl;
 cout<<str<<endl;

in.close();
 return 0;
}
//get() 出文件内容 --14
#include <iostream>
#include <fstream>
using namespace std;
int main(int argc, char *argv[])
{
 char ch;
 
 argc=2; argv[1]="test";
 if(argc!=2)
 {
  cout<<"Usage: PR <filename>/n";
  return 1;
 }
 ifstream in(argv[1], ios::in | ios::binary);
 if(!in)
 {
  cout<<"cannot open file./n";
  return 1;
 }
 while(in)
 {
  in.get(ch);
  if(in)
   cout<<ch;
 }
in.close();
 return 0;
}
 //put() 入文件内容 --15
#include <iostream>
#include <fstream>
using namespace std;
int main(int argc, char *argv[])
{
 char *p="hello there";
 ofstream out("test", ios::out|ios::binary);
 if(!out)
 {
  cout<<"cannot open file./n";
  return 1;
 }
 while(*p)
  out.put(*p++);
 return 0;
}
//read() write()--16
#include <iostream>
#include <fstream>
using namespace std;
int main(int argc, char *argv[])
{
 int n[5]={1,2,3,4,5};
 register int i;
 ofstream out("test", ios::out|ios::binary);
 if(!out)
 {
  cout<<"cannot open file./n";
  return 1;
 }
 out.write( (char *)&n, sizeof n );
 out.close();
 for(i=0; i<5; i++)
  n[i]=0;
 ifstream in("test", ios::in|ios::binary );
 if(!in)
 {
  cout<<"cannot open file./n";
  return 1;
 }
 in.read( (char *)&n, sizeof n );
 for(i=0; i<5; i++)
  cout<<n[i]<<" ";
 in.close();
 return 0;
}

 

这篇关于C++捷径之七--结束的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/581255

相关文章

c++ 类成员变量默认初始值的实现

《c++类成员变量默认初始值的实现》本文主要介绍了c++类成员变量默认初始值,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧... 目录C++类成员变量初始化c++类的变量的初始化在C++中,如果使用类成员变量时未给定其初始值,那么它将被

C++中NULL与nullptr的区别小结

《C++中NULL与nullptr的区别小结》本文介绍了C++编程中NULL与nullptr的区别,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编... 目录C++98空值——NULLC++11空值——nullptr区别对比示例 C++98空值——NUL

C++ Log4cpp跨平台日志库的使用小结

《C++Log4cpp跨平台日志库的使用小结》Log4cpp是c++类库,本文详细介绍了C++日志库log4cpp的使用方法,及设置日志输出格式和优先级,具有一定的参考价值,感兴趣的可以了解一下... 目录一、介绍1. log4cpp的日志方式2.设置日志输出的格式3. 设置日志的输出优先级二、Window

从入门到精通C++11 <chrono> 库特性

《从入门到精通C++11<chrono>库特性》chrono库是C++11中一个非常强大和实用的库,它为时间处理提供了丰富的功能和类型安全的接口,通过本文的介绍,我们了解了chrono库的基本概念... 目录一、引言1.1 为什么需要<chrono>库1.2<chrono>库的基本概念二、时间段(Durat

C++20管道运算符的实现示例

《C++20管道运算符的实现示例》本文简要介绍C++20管道运算符的使用与实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧... 目录标准库的管道运算符使用自己实现类似的管道运算符我们不打算介绍太多,因为它实际属于c++20最为重要的

Visual Studio 2022 编译C++20代码的图文步骤

《VisualStudio2022编译C++20代码的图文步骤》在VisualStudio中启用C++20import功能,需设置语言标准为ISOC++20,开启扫描源查找模块依赖及实验性标... 默认创建Visual Studio桌面控制台项目代码包含C++20的import方法。右键项目的属性:

c++中的set容器介绍及操作大全

《c++中的set容器介绍及操作大全》:本文主要介绍c++中的set容器介绍及操作大全,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录​​一、核心特性​​️ ​​二、基本操作​​​​1. 初始化与赋值​​​​2. 增删查操作​​​​3. 遍历方

解析C++11 static_assert及与Boost库的关联从入门到精通

《解析C++11static_assert及与Boost库的关联从入门到精通》static_assert是C++中强大的编译时验证工具,它能够在编译阶段拦截不符合预期的类型或值,增强代码的健壮性,通... 目录一、背景知识:传统断言方法的局限性1.1 assert宏1.2 #error指令1.3 第三方解决

C++11委托构造函数和继承构造函数的实现

《C++11委托构造函数和继承构造函数的实现》C++引入了委托构造函数和继承构造函数这两个重要的特性,本文主要介绍了C++11委托构造函数和继承构造函数的实现,具有一定的参考价值,感兴趣的可以了解一下... 目录引言一、委托构造函数1.1 委托构造函数的定义与作用1.2 委托构造函数的语法1.3 委托构造函

C++11作用域枚举(Scoped Enums)的实现示例

《C++11作用域枚举(ScopedEnums)的实现示例》枚举类型是一种非常实用的工具,C++11标准引入了作用域枚举,也称为强类型枚举,本文主要介绍了C++11作用域枚举(ScopedEnums... 目录一、引言二、传统枚举类型的局限性2.1 命名空间污染2.2 整型提升问题2.3 类型转换问题三、C