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_cast
int 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;
}