C++的一个问题--多态的?
#include <iostream>
using namespace std;
class eng
{
public:
virtual void work()
{
cout << "working" << endl;
}
void eat()
{
cout << "eat" << endl;
}
};
class softeng : public eng
{
void eat()
{
cout << "softeng eat" << endl;
}
};
class hardeng : public eng
{
void work()
{
cout << "hardeng work" << endl;
}
};
int main()
{
eng *work1 = new softeng();
//output 1
work1->work();
work1->eat();
eng *work2 = new hardeng();
//output2
work2->work();
work2->eat();
eng work3 = *(new softeng());
//output3
work3.work();
work3.eat();
eng work4 = *(new softeng());
//output4
work4.work();
work4.eat();
return 0;
}
能否帮我解释一下output3的输出为什么是working和eat,eng work3 这块内存怎么看,谢谢