本例子说明以下问题:
虚函数的定义,调用(外部函数的指针或引用,成员函数),构造函数和析构函数中调用虚函数,虚析构函数
赋值兼容,构造函数的重载,构造函数的初始化列表
见代码:
#include<iostream>
using namespace std;
class Base
{
public:
Base():m(1),n(m+1){Display();} //构造函数中调用虚函数时自己的虚函数
Base(int x,int y):m(x),n(y){}
void setxy(int x,int y){m=x+1;n=y+1;}
virtual void Display(){cout<<"m*n="<< m*n<<endl;}
void fun(){cout<<"成员函数访问虚函数:";Display();}
virtual ~Base(){cout<<"析构函数base"<<endl;}
private:
int m;
int n;
};
class Subclass : public Base
{
public:
Subclass(int x,int y,int r1):Base(x,y),r(r1){Display();} //继承构造函数
void setxyr(int x,int y,int r){setxy(x,y);r=r;} //继承成员函数
void Display(){cout<<"sub--->"<<r*r<<",";Base::Display();}
~Subclass(){cout<<"析构函数Subclass"<<endl;}
private:
int r;
};
void print(Base & p)
{
cout<< "外部函数中使用引用调用虚函数:";
p.Display();
}
void print2(Base *p)
{
cout<< "外部函数中使用指针调用虚函数:";
p->Display();
}
int main()
{
Base *bp;
Subclass *sp;
Base b1; //构造函数
Base b(3,4); //构造函数,有虚函数
bp = &b;
Subclass s(3,4,2); //构造函数,有虚函数
sp = &s;
b1.setxy(7,8);
s.setxy(4,5);
s.setxyr(4,5,2);
b1 = s; //赋值兼容
bp = sp;
print(b1); //外部函数中的虚函数,指向定义对象
print(s);
print2(bp);
print2(sp);
b1.fun(); //成员函数访问虚函数
s.fun();
b1.Display();
b.Display();
s.Display();
delete bp; //虚析构函数,指向基类的指针,调用适当的析构函数,先调用派生类
system("pause");
}版权声明:本文为博主原创文章,未经博主允许不得转载。
原文:http://blog.csdn.net/tfygg/article/details/47776911