日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

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

编程问答

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

發布時間:2025/3/19 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 昨天电脑问题 补昨日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复习内容 异常与文件操作的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。