Inheritance 从派生对象使用基类的方法
当派生对象从代码中选择合适的方法时,我感到困惑:Inheritance 从派生对象使用基类的方法,inheritance,Inheritance,当派生对象从代码中选择合适的方法时,我感到困惑: class Shape { public: virtual void draw() { cout << "Shape::draw\n"; } void erase() { cout << "Shape::erase \n"; } void redraw(){ erase(); draw(); } }; class Circle:public Shape{ public: virt
class Shape {
public:
virtual void draw()
{
cout << "Shape::draw\n";
}
void erase()
{
cout << "Shape::erase \n";
}
void redraw(){
erase(); draw();
}
};
class Circle:public Shape{
public:
virtual void draw()
{
cout << "Circle::draw\n";
}
void erase(){
cout << "Circle::erase\n";
}
};
void main(){
Circle c;
Shape s1 = c;
Shape& s2 = c;
Shape* s3 = &c;
c.redraw();
s1.redraw();
s2.redraw();
s3.redraw();
}
类形状{
公众:
虚空绘制()
{
cout erase不是虚拟的
。我想这会破坏动态调度。谢谢你的回答。我知道。但我的问题是机器(计算机)如何选择此功能erase不是虚拟的
。我想这会破坏动态调度。谢谢你的回答。我知道。但我的问题是机器(计算机)如何选择此功能