欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程资源 > 编程问答 >内容正文

编程问答

昨天电脑问题 补昨日8-3复习内容 异常与文件操作

发布时间:2025/3/19 编程问答 37 豆豆
生活随笔 收集整理的这篇文章主要介绍了 昨天电脑问题 补昨日8-3复习内容 异常与文件操作 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

1.类型转换

c 方式 强制类型转换过于粗暴  各种类型间可以随意转换 编译器难以判断正确性

#include <iostream>#include <windows.h>using namespace std;class A { private:int m_a; public:void seta(int a);A();};void A::seta(int a) {m_a = a; }A::A() {m_a = 0; }class B :public A { private:int m_b; public:void setb(int b);B();};void B::setb(int b) {m_b = b; }B::B() {m_b = 0; }//c语言中的无敌强制类型转换 //1.1c++中的普通类型转换static_cast 是在编译时期转换的 运行时无法检测类型 //1.2c++中的强制类型转换reinterpret_cast //1.3c++中的常类型转换const_castint main() {/*1.1*//*int a = 1;double b = 2.1;a = (int)b;a = static_cast<int>(b);//可以用于普通数据类型转换A aa;B bb;aa = bb;aa = static_cast<A>(bb);//可以用于派生类数据类型转换A *pa = new A;B *pb = new B;pa = pb;pa = static_cast<A *>(pb);//可以用于派生类指针类型转换pb = static_cast<B *>(pa);int *cha = new int;double *chb = new double;cha = (int *)chb;//cha = static_cast<int *>(chb);//不可用于普通指针类型的转换*//*1.2*//*int a = 1;char b = 'a';int *pa = &a;char *pb = &b;pa = reinterpret_cast<int *>(pb);pb = reinterpret_cast<char *>(0x100);*//*1.3*/const int a = 1;int *pa = const_cast<int *>(&a); //const_cast作用就是去除原来的const,下面两个一样const int &b = 1;int &pb = const_cast<int &>(b); //但是这里的int & 是常引用 用常量初始化 相当于 定义了一个变量pb = 2;cout << "b is : " << b << endl;const int c = 1;int &pc = const_cast<int &>(c); /*这里的常 变量 用常数初始化 是将其写在符号表中 数值不可更改 (和#define差不多 只是作用域不同 #define 作用域是那一行向下 常变量 相当于局部变量*/pc = 2;cout << "c is : " << c << endl;system("pause");return 0; }

2.异常处理机制

发生错误时 就抛出问题 让接收的 进行处理

/*简单了解一下 异常处理机制*/ /* 一层抛出 */#include <iostream>#include <windows.h>using namespace std;double Div(double a, double b) {if (b == 0){throw 'a'; //除数为0 抛出异常 //抛出 整型数值 catch里面就写 int//抛出 char 字符 catch里面就写 char}return a / b; }int main() {double a, b;cout << "please input two numbers: " << endl;cin >> a >> b;try{cout << Div(a, b) << endl;}catch (int){cout << "(int) second number zero is error " << endl;}catch (char){cout << "(char) second number zero is error " << endl;}system("pause");return 0; }/* 两层 或者 多重抛出 */#include <iostream>#include <windows.h>using namespace std;double Div(double a, double b) {if (b == 0){throw 'a'; //除数为0 抛出异常 //抛出 整型数值 catch里面就写 int//抛出 char 字符 catch里面就写 char}return a / b; }double f(double x, double y) {try{Div(x , y);}catch (char){throw 0;} }int main() {double a, b;cout << "please input two numbers: " << endl;cin >> a >> b;try{cout << f(a, b) << endl;}catch (int){cout << "(int) second number zero is error " << endl;}catch (char){cout << "(char) second number zero is error " << endl;}system("pause");return 0; }/* 注意如果函数声明的时候 后面接throw() 括号里有什么类型 就只可以抛出什么类型的异常 没有就无法抛出异常 看到相应代码要认识 */ #include <iostream>#include <windows.h>using namespace std;double Div(double a, double b) throw();double f(double x, double y) {try{Div(x, y);}catch (char){throw 0;} }int main() {double a, b;cout << "please input two numbers: " << endl;cin >> a >> b;try{cout << f(a, b) << endl;}catch (int){cout << "(int) second number zero is error " << endl;}catch (char){cout << "(char) second number zero is error " << endl;}system("pause");return 0; }double Div(double a, double b) throw() {if (b == 0){throw 'a'; //除数为0 抛出异常 //抛出 整型数值 catch里面就写 int//抛出 char 字符 catch里面就写 char}return a / b; }/* 抛出异常 的过程中 创建了类对象 在对应的括号结束时 会自动析构(相当于函数体结束) */ #include <iostream>#include <windows.h>using namespace std;class Test { private:int m_a; public:Test();~Test(); };Test::Test() {cout << "Test constructor ok~ " << endl; }Test::~Test() {cout << "Test destructor ok~ " << endl; }double Div(double a, double b) {if (b == 0){Test t2;cout << " throw under here to do (here is t2)" << endl;throw 'a'; //除数为0 抛出异常 //抛出 整型数值 catch里面就写 int//抛出 char 字符 catch里面就写 char}return a / b; }int main() {double a, b;cout << "please input two numbers: " << endl;cin >> a >> b;try{Test t1;cout << Div(a, b) << endl;cout << "Div(a, b) func over (here is Test t1)" << endl;}catch (int){cout << "(int) second number zero is error " << endl;}catch (char){cout << "(char) second number zero is error " << endl;}system("pause");return 0; } /* 如果抛出 类对象 */#include <iostream>#include <windows.h>using namespace std;class Test { private:int m_a; public:Test();~Test(); };Test::Test() {cout << "Test constructor ok~ " << endl; }Test::~Test() {cout << "Test destructor ok~ " << endl; }double Div(double a, double b) {if (b == 0){Test t2;cout << " throw under here to do (here is t2)" << endl;throw Test(); //除数为0 抛出异常 异常类型为 Test }return a / b; }int main() {double a, b;cout << "please input two numbers: " << endl;cin >> a >> b;try{Test t1;cout << Div(a, b) << endl;cout << "Div(a, b) func over (here is Test t1)" << endl;}catch (Test){cout << "(Test) second number zero is error " << endl; //用 Test 接收 的话 最后会析构这个test 因为这里 的catch(test)中的test 调用了拷贝构造函数} // catch (Test &) // { // cout << "(Test &) second number zero is error " << endl; // 一般建议用test & 进行捕获 方便简单 不需要析构 // } // catch (Test *t) // { // cout << "(Test *t) second number zero is error " << endl; // test * 捕获的话 最后要记得删除 指针 // delete t; // // }system("pause");return 0; }

3.异常类

异常是类 – 创建自己的异常类
异常派生
异常中的数据:数据成员
按引用传递异常
在异常中使用虚函数
案例:设计一个数组类 MyArray,重载[]操作,
数组初始化时,对数组的个数进行有效检查 
index<0 抛出异常eNegative  
index = 0 抛出异常 eZero  
    3)index>1000抛出异常eTooBig 
    4)index<10 抛出异常eTooSmall 
    5)eSize类是以上类的父类,实现有参数构造、并定义virtual void printErr()输出错误。

#include <iostream>#include <windows.h>using namespace std;class Myarray { private:int m_len;char *m_data; public:Myarray(int l);~Myarray();class esize{protected:char *msg;public:esize(char *m) : msg(m){}virtual void printerror() = 0;};class enegative : public esize{public:enegative() : esize("enegative exception"){}void printerror(){cout << msg << endl;}};class ezero : public esize{public:ezero() : esize("ezero exception"){}void printerror(){cout << msg << endl;}};class Toosmall : public esize{public:Toosmall() : esize("Toosmall exception"){}void printerror(){cout << msg << endl;}};class Toobig : public esize{public:Toobig() : esize("Toobig exception"){}void printerror(){cout << msg << endl;}};};Myarray::Myarray(int l) {if (l < 0){throw enegative();}else if (l == 0){throw ezero();}else if (l > 0 && l <= 10){throw Toosmall();}else if (l > 1000 ){throw Toobig();}m_len = l;m_data = new char[m_len + 1]; }Myarray::~Myarray() {cout << "destuct Myarray ok~ " << endl; }int main() {try{Myarray a1(-1);Myarray a2(10);Myarray a3(0);Myarray a4(1001);}catch (Myarray::enegative &e) /*上面说过的用 & 去接收; 这里因为是类中类 所以加上域限制*/{e.printerror();}catch (Myarray::Toosmall &e){e.printerror();}catch (Myarray::Toobig &e){e.printerror();}system("pause");return 0; }

4.创建自己的异常类

#include <iostream>#include <Exception>using namespace std;class Myexception :public exception { private:char *Errmsg; public:Myexception(char *s) : Errmsg(s){}virtual const char * what() const throw(){return Errmsg;} };double Div(double x , double y) {if (0 == y){throw Myexception("xxxxx");}return x / y; }int main() {double a = 2.5, b = 0;try{cout << Div(a , b) << endl;}catch (Myexception &a){cout << a.what() << endl;}system("pause");return 0; }

5.文件操作

#include <iostream>#include <iomanip> #include <windows.h>using namespace std;int main() {int a = 1314;cout << "十六进制 1314 : " << hex << a << endl;cout << "十进制 1314 : " << dec << a << endl;cout << "八进制 1314 : " << oct << a << endl;cout << "setbase(16) 1314 : " << setbase(16) << a << endl;system("pause");return 0; }/* 打开文件 输入程序 */ #include <iostream>#include <fstream> #include <windows.h>using namespace std;int main() {char buf[32] = {0};ifstream ifs("hello.txt",ios::in); //创建文件对象 打开方式为 读出 从文件读出 到 程序//ifs >> buf;//cout << "read from hello.txt :" << buf << endl;char ch;while ((ch = ifs.get()) != EOF){cout << ch;}cout << endl;system("pause");return 0; }/* 打开第一个文件 将其中内容 复制到 另一个文件中 */#include <iostream>#include <fstream> #include <windows.h>using namespace std;int main() {ifstream ifs("hello.txt", ios::in); //读取文件char buf[32] = {0};ifs.read(buf , 32);ofstream ofs("hello1.txt", ios::out); //创建文件对象 打开方式为 写入 从程序 写入到 文件 app方式是追加//char buf[32] = "xxxxxxxxxx";//ofs << buf;ofs.write(buf , 32);ofs.close();system("pause");return 0; }

 

总结

以上是生活随笔为你收集整理的昨天电脑问题 补昨日8-3复习内容 异常与文件操作的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。