Commit 70a987cf authored by Administrator's avatar Administrator
Browse files

add more use case in polymorphism

parent 941ec765
...@@ -125,6 +125,11 @@ int main() { ...@@ -125,6 +125,11 @@ int main() {
cout << "----- rec.area() -----" << endl; cout << "----- rec.area() -----" << endl;
cout << rec.area() << endl; cout << rec.area() << endl;
// 可以在通过派生类的变量rec,调用基类的方法
// 调用方法是<派生类变量>.<基类类名>::<基类方法>
cout << "----- rec.Shape::area() -----" << endl;
cout << rec.Shape::area() << endl;
// 我们想要的是在程序中任意点可以根据所调用的对象类型来选择调用的函数 // 我们想要的是在程序中任意点可以根据所调用的对象类型来选择调用的函数
// 这种操作被称为动态链接,或后期绑定。 // 这种操作被称为动态链接,或后期绑定。
cout << "----- shape->virtualArea() -----" << endl; cout << "----- shape->virtualArea() -----" << endl;
...@@ -147,6 +152,11 @@ int main() { ...@@ -147,6 +152,11 @@ int main() {
cout << "----- tri.area() -----" << endl; cout << "----- tri.area() -----" << endl;
cout << tri.area() << endl; cout << tri.area() << endl;
// 可以在通过派生类的变量tri,调用基类的方法
// 调用方法是<派生类变量>.<基类类名>::<基类方法>
cout << "----- tri.Shape::area() -----" << endl;
cout << tri.Shape::area() << endl;
// 我们想要的是在程序中任意点可以根据所调用的对象类型来选择调用的函数 // 我们想要的是在程序中任意点可以根据所调用的对象类型来选择调用的函数
// 这种操作被称为动态链接,或后期绑定。 // 这种操作被称为动态链接,或后期绑定。
cout << "----- shape->virtualArea() -----" << endl; cout << "----- shape->virtualArea() -----" << endl;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment