纯虚函数是没有在基类中定义的虚函数。
形式为:
virtual return_type func_name(parameter_list)=0;
抽象类是指至少包含一个纯虚函数的类。
测试代码:
#include<iostream>
using namespace std;
class number{
protected:
int val;
public:
number(int x){val=x;}
virtual void show()=0;
};
class hextype:public number{
public:
hextype(int x):number(x){}
void show()
{
cout<<hex<<val<<endl;
}
};
class dectype:public number{
public:
dectype(int x):number(x){}
void show()
{
cout<<val<<endl;
}
};
int main()
{
dectype b(20);
b.show();
hextype a(20);
a.show();
return 0;
}
测试结果:
20
14
Press any key to continue