Virtual 后的函数=0则为纯虚函数
#include <iostream>
using namespace std;
class FA {
public:
void Func1() {
printf("FA::Func1 \n ");
}
//void Func1(int a) {
//}
//void Func1(int a, int b) {
//}
//void Func1(int a, int b, int c) {
//}
};
class FB:public FA{
public:
void Func1(){
printf("FB::Func1 \n ");
}
};
class IInterface {
public:
virtual void Init() = 0;//带0则为纯虚函数
virtual void Distroy() = 0;
};
class FNewObject:public IInterface{
public:
FNewObject () {}
};
int main() {
//FA::Func1();
//FA::Func1(1);
//FA::Func1(1, 2);
//FA::Func1(1, 2, 3);
//FB A;
//A.Func1();
//FA* p = &A;
//p->Func1();
//A.Func1(1, 2);
//A.Func1(1, 2, 3);
FNewObject Obj;
return 0;
}
上面的代码的Obj实例化会报错,原因就在于带有纯虚函数的类的派生类
必须要重写一遍纯虚函数
如下则不会报错(下面没写实现,记得加实现):
#include <iostream>
using namespace std;
class FA {
public:
void Func1() {
printf("FA::Func1 \n ");
}
//void Func1(int a) {
//}
//void Func1(int a, int b) {
//}
//void Func1(int a, int b, int c) {
//}
};
class FB:public FA{
public:
void Func1(){
printf("FB::Func1 \n ");
}
};
class IInterface {
public:
virtual void Init() = 0;//带0则为纯虚函数
virtual void Distroy() = 0;
};
class FNewObject:public IInterface{
public:
FNewObject () {}
virtual void Init();//带0则为纯虚函数
virtual void Distroy();
};
int main() {
//FA::Func1();
//FA::Func1(1);
//FA::Func1(1, 2);
//FA::Func1(1, 2, 3);
//FB A;
//A.Func1();
//FA* p = &A;
//p->Func1();
//A.Func1(1, 2);
//A.Func1(1, 2, 3);
FNewObject Obj;
return 0;
}