下列代码中, s1-> draw(); 和 s2-> draw(); 输出不同结果的主要原因是 ( ) 。
class Shape {
public :
virtual void draw () {
cout << " 绘制图形 " << endl;
}
virtual ~Shape () {}
};
class Circle : public Shape {
public :
void draw () override {
cout << " 绘制圆形 " << endl;
}
};
class Rectangle : public Shape {
public :
void draw () override {
cout << " 绘制矩形 " << endl;
}
};
int main () {
Shape * s1 = new Circle ();
Shape * s2 = new Rectangle ();
s1 -> draw ();
s2 -> draw ();
delete s1 ;
delete s2 ;
return 0 ;
}
draw() 是普通成员函数
Shape 中的 draw() 被声明为虚函数
Circle 和 Rectangle 中使⽤了 public 继承
指针变量名不同