C++面向对象的三大特征——多态(静态多态)

 1     #include <iostream>
 2     using namespace std;
 3 
 4     //宏多态;a,b可以不同类型
 5     #define  sum(a,b)  ((a) + (b))
 6 
 7     class Base
 8     {
 9         public:
10             void Print() //不需要定义为虚函数
11             {
12                cout << "base Print() functions" << endl;
13             }
14             void Print(int val) //重载,根据参数列表不同实现函数多态
15             {
16                 cout << "base Print(int) functions" << endl;
17             }
18     };
19 
20     class child : public Base
21     {
22         public:
23             void Print() //不需要定义为虚函数
24             {
25                 cout << "child Print() functions" << endl;
26             }
27             void Print(int val) //重载,根据参数列表不同实现函数多态
28             {
29                 cout << "child Print(int) functions" << endl;
30             }
31             
32     };
33 
34     template<typename T>
35     void func(T &p)
36     {
37        p.Print();
38        p.Print(1);
39     }
40 
41     int main()
42     {
43         Base base ;
44         child ch;
45         int a = 23, b = 19;
46         double fa = 13.32, fb = 29.36;
47         func(base);
48         func(ch);
49 
50         cout << sum(a,b) << endl;
51         cout << sum(fa,fb) << endl;
52         return 0;
53     }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值