静态联编与动态联编重载与覆盖的区别

 1 #include<iostream>
 2 
 3 using namespace std;
 4 
 5 class A
 6 {
 7 public:
 8 
 9     void print(int num)
10     {
11         cout << num << endl;
12     }
13 
14     void show()
15     {
16         cout << "A show" << endl;
17     }
18 
19     void printX(int num,double db)
20     {
21         cout << num << "  " << db << endl;
22     }
23 };
24 
25 
26 class B : public A
27 {
28 public:
29 
30     void print(char ch)
31     {
32         cout << ch << endl;
33     }
34 
35     void show()
36     {
37         cout << "B show" << endl;
38     }
39 
40     void printX(int num)
41     {
42         cout << num << endl;
43     }
44 
45     void printX(int num,char *s)
46     {
47         cout << num << endl;
48     }
49 };
50 
51 
52 
53 
54 void main()
55 {
56     
57     B b1 ;
58 
59     /*
60     b1.print(48);// 编号为48的字符为0
61     b1.show();
62 
63     b1.A::print(48);
64     b1.A::show();
65     */
66 
67 
68     b1.printX(1,89.2);// 不行  重载在类的继承中是拒绝的
69     b1.A::printX(1,89.2);// 继承的时候一旦重名 必须指定在父类还是在子类,子类会覆盖父类
70     b1.printX(1,"ABc");// 这样可以  不在同一个类没有重载  继承只有覆盖  同一个类可以实现重载
71     
72     cin.get();
73 }
74 /*
75     父类的成员函数名与子类的成员函数名重名时,可以,子类覆盖父类  与参数无关
76     子类无法直接引用父类已经被覆盖的成员函数,可以通过访问父类对象b1.A::show();
77     子类内部有重名的函数,会有重载
78 */

 

posted on 2015-06-09 15:50  Dragon-wuxl  阅读(258)  评论(0)    收藏  举报

导航