-
子類最好也加virtual
查看全部 -
解決方式:在父類的析構(gòu)函數(shù)加virtual,虛析構(gòu)函數(shù)
查看全部 -
delete后面是父類的指針,則只會(huì)執(zhí)行父類的析構(gòu)函數(shù);
后面是子類的指針,則會(huì)執(zhí)行子類和父類的析構(gòu)函數(shù)。
因此在delete父類指針的時(shí)候,會(huì)造成內(nèi)存泄露。
查看全部 -
面向?qū)ο蟮娜筇卣鳎悍庋b、多態(tài)、繼承
查看全部 -
靜態(tài)多態(tài)(早綁定):?
動(dòng)態(tài)多態(tài)(晚綁定):
查看全部 -
在多態(tài)中,出現(xiàn)基類與派生類同名的虛函數(shù),可以父類對(duì)象實(shí)例化子類,調(diào)用子類成員函數(shù)。
查看全部 -
多態(tài)(靜態(tài)多態(tài),動(dòng)態(tài)多態(tài))
靜態(tài)多態(tài)(早綁定):就是函數(shù)名稱相同的兩個(gè)函數(shù),形成了重載,但是里面的參數(shù)個(gè)數(shù)不同或者說是類型不同,在主函數(shù)中調(diào)用函數(shù)的時(shí)候,會(huì)根據(jù)參數(shù)類型或者是參數(shù)的個(gè)數(shù),系統(tǒng)自動(dòng)調(diào)用函數(shù)來執(zhí)行
動(dòng)態(tài)多態(tài)(晚綁定):a為父類,b,c都繼承了a類,a,b,c類中有共同的函數(shù),在main函數(shù)中實(shí)例化一個(gè)a的對(duì)象,分別指向b,c兩個(gè)類,這時(shí)候如果想要用實(shí)例化a的對(duì)象通過相同的函數(shù)分別指向b,c中的 與a相同的函數(shù)的時(shí)候,這是需要在a類的里面相同的函數(shù)前面加上virtual。
查看全部 -
VIRTUAL只需要加在父類里邊(析構(gòu)函數(shù)和同名成員函數(shù))就好,析構(gòu)函數(shù)前邊加是為了防止沒有釋放子類對(duì)象的內(nèi)存導(dǎo)致內(nèi)存泄露,同名成員函數(shù)前加是為了父類實(shí)例化的對(duì)象指針能夠指向子類數(shù)據(jù)成員。
如果我們沒有在子類當(dāng)中定義同名的虛函數(shù),那么在子類虛函數(shù)表中就會(huì)寫上父類的虛函數(shù)的函數(shù)入口地址;如果我們?cè)谧宇惍?dāng)中也定義了虛函數(shù),那么在子類的虛函數(shù)表中我們就會(huì)把原來的父類的虛函數(shù)的函數(shù)地址覆蓋一下,覆蓋成子類的虛函數(shù)的函數(shù)地址,這種情況就稱之為函數(shù)的覆蓋。
查看全部 -
#include"Exception.h"
#include<iostream>
using namespace std;
void Exception::printException()
{
cout <<"Exception --> printException()"<< endl;
}
查看全部 -
#ifndef EXCEPTION_H
#define EXCEPTION_H
class Exception
{
public:
virtual void printException();
virtual ~Exception(){} //虛析構(gòu)函數(shù)
};
#endif
查看全部 -
#include "IndexException.h"
#include<iostream>
using namespace std;
void IndexException::printException()
{
cout <<"提示:下標(biāo)越界"<< endl;
}
查看全部 -
#ifndef INDEXEXCEPTION_H
#define INDEXEXCEPTION_H
#include "Exception.h"
class IndexException : public Exception
{
public:
virtual void printException();
};
#endif
查看全部 -
#include<iostream>
#include"stdlib.h"
#include "IndexException.h"
using namespace std;
// void test()
// {
// ? ? throw 10;//拋出異常 10
// }
// int main()
// {
// ? ? try
// ? ? {
// ? ? ? ? test();
// ? ? }
// ? ? catch(int)
// ? ? {
// ? ? ? ? cout <<"exception"<< endl;
// ? ? }
// ? ? system("pause");
// ? ? return 0;
// }
// void test()
// {
// ? ? throw 0.1; //拋出異常 10
// }
// int main()
// {
// ? ? try
// ? ? {
// ? ? ? ? test();
// ? ? }
// ? ? catch(double&e)
// ? ? {
// ? ? ? ? cout << e << endl;
// ? ? }
// ? ? system("pause");
// ? ? return 0;
// }
// void test()
// {
// ? ? throw IndexException();
// }
// int main()
// {
// ? ? try
// ? ? {
// ? ? ? ? test();
// ? ? }
// ? ? catch(IndexException &e)
// ? ? {
// ? ? ? ? e.printException();
// ? ? }
// ? ? system("pause");
// ? ? return 0;
// }
// void test()
// {
// ? ? throw IndexException();
// }
// int main()
// {
// ? ? try
// ? ? {
// ? ? ? ? test();
// ? ? }
// ? ? catch(Exception&e)
// ? ? {
// ? ? ? ? e.printException();
// ? ? }
// ? ? system("pause");
// ? ? return 0;
// }
void test()
{
throw IndexException();
}
int main()
{
try
{
test();
}
catch(...)
{
cout <<"Exception"<< endl;
}
system("pause");
return 0;
}
查看全部 -
#include <iostream>
#include "Bird.h"
using namespace std;
void Bird::foraging()
{
cout << "Bird --> foraging()" << endl;
}
void Bird::takeoff()
{
cout << "Bird --> takeoff()" << endl;
}
void Bird::land()
{
cout << "Bird --> land()" << endl;
}
查看全部 -
#ifndef Bird_H
#define Bird_H
#include "Flyable.h"
#include <string>
using namespace std;
class Bird:public Flyable //公有繼承了Flyable
{
public:
void foraging();//對(duì)于Bird類來說,其具有一個(gè)特有的成員函數(shù)foraging(覓食)
virtual void takeoff(); //實(shí)現(xiàn)了Flyable中的虛函數(shù)takeoff和land
virtual void land();
};
#endif
查看全部
舉報(bào)