Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
Menu
Open sidebar
Clark Lin
StudyCpp
Commits
70a987cf
Commit
70a987cf
authored
Nov 22, 2022
by
Administrator
Browse files
add more use case in polymorphism
parent
941ec765
Changes
1
Hide whitespace changes
Inline
Side-by-side
02.OOP/Polymorphism.cpp
View file @
70a987cf
...
@@ -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
;
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment