zl程序教程

您现在的位置是:首页 >  后端

当前栏目

C++ 多态

C++ 多态
2023-09-14 09:12:06 时间

C++ 多态
多态按字面的意思就是多种形态。当类之间存在层次结构,并且类之间是通过继承关联时,就会用到多态。

C++ 多态意味着调用成员函数时,会根据调用函数的对象的类型来执行不同的函数。

 

 1 #include <iostream>
 2 
 3 /* run this program using the console pauser or add your own getch, system("pause") or input loop */
 4 using namespace std;
 5 int fac(int);
 6 int main(int argc, char** argv) {
 7     int i;
 8     for(i=1;i<=5;i++)
 9     cout <<i<<"!="<<fac(i)<<endl;
10     return 0;
11 }
12 
13 int fac(int n)
14 {
15     static int f=1;
16     f=f*n;
17     return f;
18 }