昨天电脑问题 补昨日8-3复习内容 异常与文件操作
生活随笔
收集整理的這篇文章主要介紹了
昨天电脑问题 补昨日8-3复习内容 异常与文件操作
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
1.類型轉(zhuǎn)換
c 方式 強(qiáng)制類型轉(zhuǎn)換過于粗暴 ?各種類型間可以隨意轉(zhuǎn)換 編譯器難以判斷正確性
#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語言中的無敵強(qiáng)制類型轉(zhuǎn)換 //1.1c++中的普通類型轉(zhuǎn)換static_cast 是在編譯時(shí)期轉(zhuǎn)換的 運(yùn)行時(shí)無法檢測(cè)類型 //1.2c++中的強(qiáng)制類型轉(zhuǎn)換reinterpret_cast //1.3c++中的常類型轉(zhuǎn)換const_castint main() {/*1.1*//*int a = 1;double b = 2.1;a = (int)b;a = static_cast<int>(b);//可以用于普通數(shù)據(jù)類型轉(zhuǎn)換A aa;B bb;aa = bb;aa = static_cast<A>(bb);//可以用于派生類數(shù)據(jù)類型轉(zhuǎn)換A *pa = new A;B *pb = new B;pa = pb;pa = static_cast<A *>(pb);//可以用于派生類指針類型轉(zhuǎn)換pb = static_cast<B *>(pa);int *cha = new int;double *chb = new double;cha = (int *)chb;//cha = static_cast<int *>(chb);//不可用于普通指針類型的轉(zhuǎn)換*//*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,下面兩個(gè)一樣const int &b = 1;int &pb = const_cast<int &>(b); //但是這里的int & 是常引用 用常量初始化 相當(dāng)于 定義了一個(gè)變量pb = 2;cout << "b is : " << b << endl;const int c = 1;int &pc = const_cast<int &>(c); /*這里的常 變量 用常數(shù)初始化 是將其寫在符號(hào)表中 數(shù)值不可更改 (和#define差不多 只是作用域不同 #define 作用域是那一行向下 常變量 相當(dāng)于局部變量*/pc = 2;cout << "c is : " << c << endl;system("pause");return 0; }2.異常處理機(jī)制
發(fā)生錯(cuò)誤時(shí) 就拋出問題 讓接收的 進(jìn)行處理
/*簡單了解一下 異常處理機(jī)制*/ /* 一層拋出 */#include <iostream>#include <windows.h>using namespace std;double Div(double a, double b) {if (b == 0){throw 'a'; //除數(shù)為0 拋出異常 //拋出 整型數(shù)值 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'; //除數(shù)為0 拋出異常 //拋出 整型數(shù)值 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; }/* 注意如果函數(shù)聲明的時(shí)候 后面接throw() 括號(hào)里有什么類型 就只可以拋出什么類型的異常 沒有就無法拋出異常 看到相應(yīng)代碼要認(rèn)識(shí) */ #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'; //除數(shù)為0 拋出異常 //拋出 整型數(shù)值 catch里面就寫 int//拋出 char 字符 catch里面就寫 char}return a / b; }/* 拋出異常 的過程中 創(chuàng)建了類對(duì)象 在對(duì)應(yīng)的括號(hào)結(jié)束時(shí) 會(huì)自動(dòng)析構(gòu)(相當(dāng)于函數(shù)體結(jié)束) */ #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'; //除數(shù)為0 拋出異常 //拋出 整型數(shù)值 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; } /* 如果拋出 類對(duì)象 */#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(); //除數(shù)為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 接收 的話 最后會(huì)析構(gòu)這個(gè)test 因?yàn)檫@里 的catch(test)中的test 調(diào)用了拷貝構(gòu)造函數(shù)} // catch (Test &) // { // cout << "(Test &) second number zero is error " << endl; // 一般建議用test & 進(jìn)行捕獲 方便簡單 不需要析構(gòu) // } // catch (Test *t) // { // cout << "(Test *t) second number zero is error " << endl; // test * 捕獲的話 最后要記得刪除 指針 // delete t; // // }system("pause");return 0; }3.異常類
異常是類?–?創(chuàng)建自己的異常類
異常派生
異常中的數(shù)據(jù):數(shù)據(jù)成員
按引用傳遞異常
在異常中使用虛函數(shù)
案例:設(shè)計(jì)一個(gè)數(shù)組類?MyArray,重載[]操作,
數(shù)組初始化時(shí),對(duì)數(shù)組的個(gè)數(shù)進(jìn)行有效檢查?
index<0 拋出異常eNegative ?
index = 0 拋出異常?eZero ?
????3)index>1000拋出異常eTooBig?
????4)index<10 拋出異常eTooSmall?
????5)eSize類是以上類的父類,實(shí)現(xiàn)有參數(shù)構(gòu)造、并定義virtual void printErr()輸出錯(cuò)誤。
4.創(chuàng)建自己的異常類
#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 << "十六進(jìn)制 1314 : " << hex << a << endl;cout << "十進(jìn)制 1314 : " << dec << a << endl;cout << "八進(jìn)制 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); //創(chuàng)建文件對(duì)象 打開方式為 讀出 從文件讀出 到 程序//ifs >> buf;//cout << "read from hello.txt :" << buf << endl;char ch;while ((ch = ifs.get()) != EOF){cout << ch;}cout << endl;system("pause");return 0; }/* 打開第一個(gè)文件 將其中內(nèi)容 復(fù)制到 另一個(gè)文件中 */#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); //創(chuàng)建文件對(duì)象 打開方式為 寫入 從程序 寫入到 文件 app方式是追加//char buf[32] = "xxxxxxxxxx";//ofs << buf;ofs.write(buf , 32);ofs.close();system("pause");return 0; }?
總結(jié)
以上是生活随笔為你收集整理的昨天电脑问题 补昨日8-3复习内容 异常与文件操作的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 有关TCP/UDP
- 下一篇: x shell 配置 和相关注意点(vm