zoukankan      html  css  js  c++  java
  • 多态 子类函数重写

    1 #include<iostream>
      2 #include<string>
      3 using namespace std;
      4 class boss{
      5 public:
      6 int fight(){
      7         int ret = 10;
      8                 cout << "boss::fight():ret=" << ret << endl;
      9                 return ret;
     10         }
     11 };
     12 class master{
     13 public:
     14         virtual int eight(){
     15                 int ret = 8;
     16                 cout << "master::eight():ret=" << ret << endl;
     17                 return ret;
     18         }
     19 };
     20 class newmaster:public master{
     21 public:
     22         int eight(){
     23                 int ret = master::eight()*2;  
     24                 cout << "newmaster:eight():" << ret << endl;
     25                 return ret;
     26         }
     27 };
     28 void pk(master*master,boss*boss){
     29         int k = master->eight();
    30         int b = boss->fight();
     31         if(k < b){
     32                 cout << "boss big" << endl;
     33         }else{
     34                 cout << "master big" << endl;
     35         }
     36 }
     37 int main(){
     38         boss b;
     39         newmaster n;
     40         pk(&n,&b);
     41         return 0;
     42 }

    打印结果:

    master::eight():ret=8// 这行是第23的master::eight()产生的。
    newmaster:eight():16
    boss::fight():ret=10
    master big

    //多态时候会将父 类被重写的函数也执行                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 

  • 相关阅读:
    【译】常用网络端口号列表
    使用Simian进行重复代码检测
    使用GCOV进行代码覆盖率统计
    AFL Fuzz安装及完成一次简单的模糊测试
    数据可视化概述
    完成下方的 which_date() 函数,并返回某一起始时间后特定一段时间的日期
    linux用户不在sudoers文件中
    linux /lib64/libc.so.6: version `GLIBC_2.17′ not found
    web api 2.0 上传文件超过4M时,出现404错误
    Centos7 编译安装 Nginx Mariadb Asp.net Core2 (实测 笔记 Centos 7.7 + Openssl 1.1.1d + Mariadb 10.3.7 + Nginx 1.16.1 + Asp.net. Core 2 )
  • 原文地址:https://www.cnblogs.com/DXGG-Bond/p/11963046.html
Copyright © 2011-2022 走看看