虚函数的疑问.
#include<iostream.h>class Base {
public:
Base(int x,int y)
{ a=x; b=y; }
virtual void show() //定义虚函数show()
{ cout<<"Base----------\n"; cout<<a<<" "<<b<<endl;}
private:
int a,b; };
class Derived : public Base {
public:
Derived(int x,int y,int z):Base(x,y){c=z; }
void show() //重新定义虚函数show()
{ Base::show();cout<< "Derived---------\n"; cout<<c<<endl;}
private:
int c;
};
void main()
{
Base mb(60,60),*pc;
Derived mc(10,20,30);
pc=&mb;
pc->show(); //调用基类Base的show()版本
pc=&mc;
pc->show(); //调用派生类Derived的show()版本
}
有什么办法能让红色部分输出为60,60,也就是基类对象的值.
派生类的构造函数把他初始化为10,20了