class Teacher : public People
Teacher * t,tobj; People * p,pobj;
1.使用基類指針操作派生類對象。 p = &tobj;0只能訪問定義于基類的共有成員。 必須將基類指針強制轉(zhuǎn)換為派生類指針,才能訪問派生類的成員。 (Teacher *) p-> ...
2.使用派生類指針操作基類對象 。 t = (Teacher *) &pobj ; 并且如此只能訪問基類中的成員,不能訪問派生類成員!
***********************************
虛函數(shù)(實現(xiàn)多態(tài))
在基類中給方法加上virtual 標(biāo)記,派生類實現(xiàn)該方法。
則使用時,采用基類指針指向具體子類對象的時候,即可以實現(xiàn)多臺。通過指針調(diào)用的將是具體子類的改方法。
純虛函數(shù):
virtual double method const=0; 包含純虛函數(shù)的類稱為 抽象類!
類的析構(gòu)函數(shù)應(yīng)該被設(shè)置為虛析構(gòu)函數(shù)
const Teacher t;//常量對象
void method const(){} //常量函數(shù)
常量對象不能調(diào)用非常量成員函數(shù)
常量函數(shù)不允許調(diào)用非常量成員函數(shù)。
**********
文件寫對象
ofstream outFile("filename",ios::out| ios::binary);
outFile.write( (char *) &st,sizeof(st)); //st是一個類的對象
infile.read((char *)&st,sizeof(st)); //讀取出對象
*******
throw 可以拋出任意類型的異常,比如 int a=0; throw a;
catch(...){ //捕獲任意類型的異常
;
}
catch(int){
//捕獲整型異常
}
********
函數(shù)模板
template <class T>
void sortArray(T b[],int len);
類模板:
template <class T>
class Test{
..
}
所以在類模板外面定義的成員函數(shù)都要以template <class T>開頭
實例化類對象. Test <int> test;
Teacher * t,tobj; People * p,pobj;
1.使用基類指針操作派生類對象。 p = &tobj;0只能訪問定義于基類的共有成員。 必須將基類指針強制轉(zhuǎn)換為派生類指針,才能訪問派生類的成員。 (Teacher *) p-> ...
2.使用派生類指針操作基類對象 。 t = (Teacher *) &pobj ; 并且如此只能訪問基類中的成員,不能訪問派生類成員!
***********************************
虛函數(shù)(實現(xiàn)多態(tài))
在基類中給方法加上virtual 標(biāo)記,派生類實現(xiàn)該方法。
則使用時,采用基類指針指向具體子類對象的時候,即可以實現(xiàn)多臺。通過指針調(diào)用的將是具體子類的改方法。
純虛函數(shù):
virtual double method const=0; 包含純虛函數(shù)的類稱為 抽象類!
類的析構(gòu)函數(shù)應(yīng)該被設(shè)置為虛析構(gòu)函數(shù)
const Teacher t;//常量對象
void method const(){} //常量函數(shù)
常量對象不能調(diào)用非常量成員函數(shù)
常量函數(shù)不允許調(diào)用非常量成員函數(shù)。
**********
文件寫對象
ofstream outFile("filename",ios::out| ios::binary);
outFile.write( (char *) &st,sizeof(st)); //st是一個類的對象
infile.read((char *)&st,sizeof(st)); //讀取出對象
*******
throw 可以拋出任意類型的異常,比如 int a=0; throw a;
catch(...){ //捕獲任意類型的異常
;
}
catch(int){
//捕獲整型異常
}
********
函數(shù)模板
template <class T>
void sortArray(T b[],int len);
類模板:
template <class T>
class Test{
..
}
所以在類模板外面定義的成員函數(shù)都要以template <class T>開頭
實例化類對象. Test <int> test;