1—————————————————
#include <iostream>
using namespace std;
class Base
{
public:
Base(int data):_ma(data)
{
cout<<"Base()"<<endl;
}
virtual ~Base()
{
cout<<"~Base()"<<endl;
}
virtual void Show(int i= 10)//虚函数
{
cout<<"Base::Show(), i="<<i<<endl;
}
private:
int _ma;
};
class Derive : public Base
{
public:
Derive(int data1, int data2):Base(data1),_mb(data2)
{
cout<<"Derive()"<<endl;
}
~Derive()
{
cout<<"~Derive()"<<endl;
};
virtual void Show(int i= 20)//虚函数
{
cout<<"Derive::Show(), i="<<i<<endl;
}
private:
int _mb;
};
int main()
{
Base *p = new Derive(10,10);
p->Show();
delete p;
return 0;
}
p->Show();函数有默认的参数,在调用过程中没有传递参数。编译期进行形参压栈,已经将基类的Show()函数的10压进栈了,调用派生类中的Show时,不再对其进行初始化。若要对虚函数中的值进行赋值,不要在基类虚函数的参数列表中赋默认值。
2.———————————————————————
#include <iostream>
using namespace