#include <iostream>
class AbstractBase{
public:
explicit AbstractBase(int b): b_(b){}
virtual int GetMember()=0;
int b_;
};
class Derived: public AbstractBase{
public:
explicit Derived(int d, int b): AbstractBase(b), d_(d){}
int d_;
virtual int GetMember() {return d_;}
};
int main()
{
/*父类是抽象类,若父类中有纯虚函数时,子类必须重写,否则子类创建对象失败
*/
Derived d(2,3);
return 0;
}
// 和上面等价,用到C++11中类成员就地初始化和继承构造
class AbstractBase{
public:
explicit AbstractBase(int b): b_(b){}
virtual int GetMember()=0;
int b_;
};
class Derived: public AbstractBase{
public:
int d_ = 2;
virtual int GetMember() {return d_;}
using AbstractBase::AbstractBase;
};
int main()
{
Derived d(2);
return 0;
}
C++ 抽象类
最新推荐文章于 2024-06-22 15:18:50 发布