1、初步认识虚函数和纯虚函数
1.1、虚函数
虚函数 是在基类中使用关键字 virtual
声明的函数。在派生类中重新定义基类中定义的虚函数时,会告诉编译器不要静态链接到该函数。
我们想要的是在程序中任意点可以根据所调用的对象类型来选择调用的函数,这种操作被称为动态链接,或后期绑定。
1.2、纯虚函数
您可能想要在基类中定义虚函数,以便在派生类中重新定义该函数更好地适用于对象,但是您在基类中又不能对虚函数给出有意义的实现,这个时候就会用到纯虚函数。
我们可以把基类中的虚函数 area()
改写如下:
class Shape {
protected:
int width, height;
public:
Shape( int a=0, int b=0)
{
width = a;
height = b;
}
// pure virtual function
virtual int area() = 0;
};
2、纯虚函数与抽象类
C++中的纯虚函数(或抽象函数)是我们没有实现的虚函数!我们只需声明它,通过声明中赋值0来声明纯虚函数!示例如下:
class Test
{
// Data members of class
public:
// Pure Virtual Function
virtual void show() = 0;
/* Other members */
};
- 纯虚函数:没有函数体的虚函数
- 抽象类:包含纯虚函数的类,不能创建对象
#include<iostream>
using namespace std;
class A
{
private:
int a;
public:
virtual void show()=0; // 纯虚函数
};
int main()
{
/*
* 1. 抽象类只能作为基类来派生新类使用
* 2. 抽象类的指针和引用->由抽象类派生出来的类的对象!
*/
A a; // error 抽象类,不能创建对象
A *a1; // ok 可以定义抽象类的指针
A *a2 = new A(); // error, A是抽象类,不能创建对象
}
3、实现抽象类
- 在抽象类中:成员函数内可以调用纯虚函数,在构造函数和析构函数内部不能使用纯虚函数。
- 如果一个类从抽象类派生而来,它必须实现了基类中的所有纯虚函数,才能成为非抽象类。
#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;
}
//输出结果如下:
B:f()
重要知识点
- 纯虚函数使一个类变成抽象类
#include<iostream>
using namespace std;
/**
* @brief 抽象类至少包含一个纯虚函数
*/
class Test
{
int x;
public:
virtual void show() = 0;
int getX() { return x; }
};
int main(void)
{
Test t; //error! 不能创建抽象类的对象
return 0;
}
- 抽象类类型的指针和引用
#include<iostream>
using namespace std;
/**
* @brief 抽象类至少包含一个纯虚函数
*/
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 b; //error! 不能创建抽象类的对象
//Base *b = new Base(); error!
Base *bp = new Derived(); // 抽象类的指针和引用 -> 由抽象类派生出来的类的对象
bp->show();
return 0;
}
-
需要注意的是:
Base *bp
只是创建了一个Base
类型的指针,并没有赋值所以不会出错;Base *bp = new Derived()
右边创建了派生类(非抽象类)的对象,所以也不会报错;Base *b = new Base()
右边创建了抽象类的对象,所以会报错!
-
如果我们不在派生类中覆盖所有纯虚函数,那么派生类也会变成抽象类。
#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() { }
};
int main(void)
{
Derived d; //error! 派生类没有实现纯虚函数,那么派生类也会变为抽象类,不能创建抽象类的对象
return 0;
}
- 抽象类可以有构造函数
#include<iostream>
using namespace std;
// An abstract class with constructor
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;
}
输出:
x = 4, y = 5
- 构造函数不能是虚函数,而析构函数可以是虚析构函数。
#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;
}
//输出
Constructor: Base
Constructor: Derived
Destructor : Derived
Destructor : Base
当基类指针指向派生类对象并删除对象时,我们可能希望调用适当的析构函数。如果析构函数不是虚拟的,则只能调用基类析构函数。
故,当去掉上述程序基类Base
中的virtual
时,输出结果如下:
//输出
Constructor: Base
Constructor: Derived
Destructor : Base
- 抽象类由派生类继承实现!
#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"; } // 实现了fun()函数
};
int main(void)
{
Derived d;
d.fun();
return 0;
}
//输出
fun() called