1、凡是包含純虛函數的類都是抽象類;
2、抽象類不能被實例化;
3、可以使用public方式繼承抽象類,並且實現抽象類中的所有純虛函數;
如下代碼例子:
myclass.h
namespace myclass
{
class MyClass
{
public:
MyClass(){};
static int myget();
virtual int func1()=0;
virtual int func2()=0;
static void myset(char **);
~MyClass(){};
};
class NewClass: public MyClass
{
public:
NewClass(){};
virtual int func1();
virtual int func2();
~NewClass(){};
};
}
myclass.cpp
#include<iostream>
#include"myclass.h"
int myclass::MyClass::myget()
{
return 111;
}
void myclass::MyClass::myset(char ** p)
{
* p=(char*)malloc(10);
char *t="good";
strcpy(*p,t);
return ;
}
int myclass::NewClass::func1()
{
return 1;
}
int myclass::NewClass::func2()
{
return 2;
}
test.cpp
#include<iostream>
#include"myclass.h"
using namespace std;
using namespace myclass;
int main()
{
int x = MyClass::myget();
cout<<"x="<<x<<endl;
char * p;
MyClass::myset(&p);
cout<<"p="<<p<<endl;
free(p);
NewClass *oneclass=new NewClass();
MyClass * twoclass=new NewClass();
cout<<"twoclass->myget()="<<twoclass->myget()<<endl;
return 0;
}