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
aecbb540
Commit
aecbb540
authored
Nov 21, 2022
by
Administrator
Browse files
add polymorphism
parent
faae3e40
Changes
1
Hide whitespace changes
Inline
Side-by-side
02.OOP/Polymorphism.cpp
0 → 100644
View file @
aecbb540
#include <iostream>
using
namespace
std
;
class
Shape
{
protected:
int
width
,
height
;
public:
Shape
(
int
a
,
int
b
)
{
width
=
a
;
height
=
b
;
}
int
area
()
{
cout
<<
"Parent class area :"
<<
endl
;
return
0
;
}
// 虚函数 是在基类中使用关键字 virtual 声明的函数。
virtual
int
virtualArea
()
{
cout
<<
"Triangle class area :"
<<
endl
;
return
(
width
*
height
/
2
);
}
// 您可能想要在基类中定义虚函数,以便在派生类中重新定义该函数更好地适用于对象
// 但是您在基类中又不能对虚函数给出有意义的实现,这个时候就会用到纯虚函数。
virtual
int
pureVirtualArea
()
=
0
;
};
class
Rectangle
:
public
Shape
{
public:
Rectangle
(
int
a
,
int
b
)
:
Shape
(
a
,
b
)
{
}
int
area
()
{
cout
<<
"Rectangle class area :"
<<
endl
;
return
(
width
*
height
);
}
// 在派生类中重新定义基类中定义的虚函数时,会告诉编译器不要静态链接到该函数。
int
virtualArea
()
{
cout
<<
"Rectangle class area :"
<<
endl
;
return
(
width
*
height
);
}
// 纯虚函数在派生类中的具体实现。
int
pureVirtualArea
()
{
cout
<<
"Rectangle class area :"
<<
endl
;
return
(
width
*
height
);
}
};
class
Triangle
:
public
Shape
{
public:
Triangle
(
int
a
,
int
b
)
:
Shape
(
a
,
b
)
{
}
int
area
()
{
cout
<<
"Triangle class area :"
<<
endl
;
return
(
width
*
height
/
2
);
}
// 在派生类中重新定义基类中定义的虚函数时,会告诉编译器不要静态链接到该函数。
int
virtualArea
()
{
cout
<<
"Triangle class area :"
<<
endl
;
return
(
width
*
height
/
2
);
}
// 纯虚函数在派生类中的具体实现。
int
pureVirtualArea
()
{
cout
<<
"Triangle class area :"
<<
endl
;
return
(
width
*
height
/
2
);
}
};
// 程序的主函数
int
main
()
{
Shape
*
shape
;
Rectangle
rec
(
10
,
7
);
Triangle
tri
(
10
,
5
);
// 存储矩形的地址
shape
=
&
rec
;
// 调用矩形的求面积函数 area
// shape是基类的指针,使用基类指针调用函数 area() ,执行的是基类Shape的方法。
// 这就是所谓的静态多态,或静态链接调用函数。
cout
<<
"----- shape->area() -----"
<<
endl
;
cout
<<
shape
->
area
()
<<
endl
;
// 调用矩形的求面积函数 area
// 如果使用变量,执行的是派生类Rectangle的方法。
cout
<<
"----- rec.area() -----"
<<
endl
;
cout
<<
rec
.
area
()
<<
endl
;
// 我们想要的是在程序中任意点可以根据所调用的对象类型来选择调用的函数
// 这种操作被称为动态链接,或后期绑定。
cout
<<
"----- shape->virtualArea() -----"
<<
endl
;
cout
<<
shape
->
virtualArea
()
<<
endl
;
// 通过纯虚函数的实现,来执行派生类的方法
cout
<<
"----- shape->pureVirtualArea() -----"
<<
endl
;
cout
<<
shape
->
pureVirtualArea
()
<<
endl
;
// 存储三角形的地址
shape
=
&
tri
;
// 调用三角形的求面积函数 area
// shape是基类的指针,使用基类指针调用函数 area() ,执行的是基类Shape的方法。
// 这就是所谓的静态多态,或静态链接调用函数。
cout
<<
"----- shape->area() -----"
<<
endl
;
cout
<<
shape
->
area
()
<<
endl
;
// 调用三角形的求面积函数 area
// 如果使用变量,执行的是派生类Triangle的方法。
cout
<<
"----- tri.area() -----"
<<
endl
;
cout
<<
tri
.
area
()
<<
endl
;
// 我们想要的是在程序中任意点可以根据所调用的对象类型来选择调用的函数
// 这种操作被称为动态链接,或后期绑定。
cout
<<
"----- shape->virtualArea() -----"
<<
endl
;
cout
<<
shape
->
virtualArea
()
<<
endl
;
// 通过纯虚函数的实现,来执行派生类的方法
cout
<<
"----- shape->pureVirtualArea() -----"
<<
endl
;
cout
<<
shape
->
pureVirtualArea
()
<<
endl
;
return
0
;
}
\ No newline at end of file
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