zoukankan      html  css  js  c++  java
  • C++虚函数、赋值兼容原则

     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 }
    当你坚持做一件完全正确的事情,有可能在很长一段时间内,你的价值都是零。
  • 相关阅读:
    画板
    多线程
    Runtime
    今日头条UI搭建
    支付宝UI界面搭建
    控制器的创建
    UIWindow简单介绍
    UIApplication
    UIPickerView的使用
    代理、通知、KVO
  • 原文地址:https://www.cnblogs.com/lweleven/p/virtualfunc.html
Copyright © 2011-2022 走看看