C ++派生类中的虚函数
C++中的虚函数用于创建基类指针和任何派生类的调用方法的列表,甚至不知道派生类对象的种类。虚函数在运行时解析得较晚。
如果基类中的虚函数声明为成员函数一次,则它在从该基类派生的每个类中均变为虚函数。因此,在声明虚拟基类函数的重新定义版本时,在派生类中不必使用关键字virtual。
范例程式码
#include<iostream>
using namespace std;
class B {
public:
virtual void s() {
cout<<" In Base \n";
}
};
class D: public B {
public:
void s() {
cout<<"In Derived \n";
}
};
int main(void) {
D d; // An object of class D
B *b= &d; // A pointer of type B* pointing to d
b->s(); // prints"D::s() called"
return 0;
}输出结果
In Derived
热门推荐
10 病人祝福语老师寄语简短
11 新娘生孩子祝福语简短
12 婆婆66岁祝福语简短
13 今天孩子高考祝福语简短
14 送彩票生日祝福语简短
15 分别祝福语简短情话英文
16 画室揭牌仪式祝福语简短
17 女友妈妈生日祝福语简短
18 离别赠言离职祝福语简短