码迷,mamicode.com
首页 > 编程语言 > 详细

C++虚函数、赋值兼容原则

时间:2015-07-08 07:07:33      阅读:156      评论:0      收藏:0      [点我收藏+]

标签:

 1 #include <iostream.h>
 2 
 3 class A
 4 {
 5 public:
 6     void f1()
 7     { cout << "a" << endl; }
 8     virtual void f2()
 9     { cout << "b" << endl; }
10 };
11 
12 class B : public A
13 {
14 public:
15     void f1()
16     { cout << "c" << endl; }
17     virtual void f2()
18     { cout << "d" << endl; }
19 };
20 
21 void main()
22 {
23     A a;
24     B b;
25     a.f1();
26     a.f2();
27     b.f1();
28     cout << "--------------------" << endl;
29 
30     A *pa = &b;
31     pa->f1();//a
32     pa->f2();//d
33     cout << "-------------------" << endl;
34 
35     A *fa = (A *)&b;
36     fa->f1();//a
37     fa->f2();//d
38     cout << "-------------------" << endl;
39 }

 

C++虚函数、赋值兼容原则

标签:

原文地址:http://www.cnblogs.com/lweleven/p/virtualfunc.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!