#include"aa.h"
#include<iostream.h>
#include<stdio.h>
#include<string.h>
class aa
{
public:
virtual void test()
{cout<<"aa"<<endl;
}
virtual ~aa()
{cout<<"destroy aa "<<endl;
}
};
class bb: public aa
{
public:
virtual void test()
{cout<<"bb"<<endl;
}
~bb()
{cout<<"destroy bb"<<endl;
}
};
void fun(aa & a)//具有多态
{
a.test();
}
void fun(aa *a)//具有多态
{
a->test();
delete a;//如果没有虚析构函数,就这里的子类就没有得到释放,只能释放父类的。
}
int main()
{
bb *b=new bb();
fun(b);
return 0;
}
#include<iostream.h>
#include<stdio.h>
#include<string.h>
class aa
{
public:
virtual void test()
{cout<<"aa"<<endl;
}
virtual ~aa()
{cout<<"destroy aa "<<endl;
}
};
class bb: public aa
{
public:
virtual void test()
{cout<<"bb"<<endl;
}
~bb()
{cout<<"destroy bb"<<endl;
}
};
void fun(aa & a)//具有多态
{
a.test();
}
void fun(aa *a)//具有多态
{
a->test();
delete a;//如果没有虚析构函数,就这里的子类就没有得到释放,只能释放父类的。
}
int main()
{
bb *b=new bb();
fun(b);
return 0;
}