纯虚函数和抽象类
纯虚函数与抽象类
C++中的纯虚函数(或抽象函数)是我们没有实现的虚函数!我们只需声明它!通过声明中赋值0来声明纯虚函数!
1 2 3 4 5 6 7 8 9
| class Test { public: virtual void show() = 0;
};
|
- 纯虚函数:没有函数体的虚函数
- 抽象类:包含纯虚函数的类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| #include<iostream>
using namespace std; class A { private: int a; public: virtual void show()=0; };
int main() {
A a;
A *a1;
A *a2 = new A(); }
|
抽象类只能作为基类来派生新类使用,不能创建抽象类的对象,抽象类的指针和引用->由抽象类派生出来的类的对象!
实现抽象类
抽象类中:在成员函数内可以调用纯虚函数,在构造函数/析构函数内部不能使用纯虚函数。
如果一个类从抽象类派生而来,它必须实现了基类中的所有纯虚函数,才能成为非抽象类。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| #include <iostream>
using namespace std;
class A { public: virtual void f() = 0; void g() { this->f(); } A() {} };
class B : public A { public: void f() { cout << "B::f()" << endl; } };
int main() { B b; b.g(); return 0; }
|
重要点
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| #include <iostream> using namespace std;
class Test { int x;
public: virtual void show() = 0; int getX() { return x; } };
int main(void) { Test t; return 0; }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| #include <iostream> using namespace std;
class Base { int x;
public: virtual void show() = 0; int getX() { return x; } }; class Derived : public Base { public: void show() { cout << "In Derived \n"; } Derived() {} }; int main(void) { Base *bp = new Derived(); bp->show(); return 0; }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| #include <iostream> using namespace std;
class Base { protected: int x;
public: virtual void fun() = 0; Base(int i) { x = i; } };
class Derived : public Base { int y;
public: Derived(int i, int j) : Base(i) { y = j; } void fun() { cout << "x = " << x << ", y = " << y; } };
int main(void) { Derived d(4, 5); d.fun(); return 0; }
|
- 构造函数不能是虚函数,而析构函数可以是虚析构函数。(当基类指针指向派生类对象并删除对象时,我们可能希望调用适当的析构函数。如果析构函数不是虚拟的,则只能调用基类析构函数。)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| #include <iostream> using namespace std;
class Base { public: Base() { cout << "Constructor: Base" << endl; } virtual ~Base() { cout << "Destructor : Base" << endl; } };
class Derived : public Base { public: Derived() { cout << "Constructor: Derived" << endl; } ~Derived() { cout << "Destructor : Derived" << endl; } };
int main() { Base *Var = new Derived(); delete Var; return 0; }
|
输出:
1 2 3 4
| Constructor: Base Constructor: Derived Destructor : Derived Destructor : Base
|
完整示例
抽象类由派生类继承实现
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| #include <iostream> using namespace std;
class Base { int x;
public: virtual void fun() = 0; int getX() { return x; } };
class Derived : public Base { int y;
public: void fun() { cout << "fun() called"; } };
int main(void) { Derived d; d.fun(); return 0; }
|