昨天电脑问题 补昨日8-3复习内容 异常与文件操作
生活随笔
收集整理的這篇文章主要介紹了
昨天电脑问题 补昨日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()輸出錯誤。
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复习内容 异常与文件操作的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 有关TCP/UDP
- 下一篇: x shell 配置 和相关注意点(vm