zoukankan      html  css  js  c++  java
  • C++多态

    ● 编译时的多态性
    编译时的多态性是通过重载来实现的。对于非虚的成员来说,系统在编译时,根据传递的参数、返回的类型等信息决定实现何种操作。
    ● 运行时的多态性
    运行时的多态性就是指直到系统运行时,才根据实际情况决定实现何种操作。C#中,运行时的多态性通过虚成员实现。

    ● 特点
    编译时的多态性为我们提供了运行速度快的特点,而运行时的多态性则带来了高度灵活和抽象的特点。

    #include"cstdio"
    #include"cstdlib"
    #include"algorithm"
    #include"iostream"
    #include"sstream"
    #include"cstring"
    #include"string"
    #include"algorithm"
    using namespace std;
    class Father{
    public:
        Father(){
            cout<<this<<":"<<"create Father object"<<endl;
        }
        virtual ~Father(){
            cout<<"out of class Father"<<endl;
        }
        void fun(){
            cout<<"this is a common fun"<<endl;
        }
        virtual void DoSomething(){
            cout<<"do something in class Fahter"<<endl;
        }
    };
    class Son:public Father{
    public:
        Son(){
            cout<<this<<":"<<"create Son object"<<endl;
        }
        ~Son(){
            cout<<"out of the class Son"<<endl;
        }
        void DoSomething(){
            cout<<"do something in class Son"<<endl;
        }
    };
    int main()
    {
        Son *f=new Son();
        f->DoSomething();
        f->fun();
        delete f;
    }

    运行结果:

    0x9cca008:create Father object
    0x9cca008:create Son object
    do something in class Son
    this is a common fun
    out of the class Son
    out of class Father

  • 相关阅读:
    一行代码搞定图片缩放、旋转、加水印
    如何学习 Webpack
    Webpack 概念
    Webpack 入门
    asp.net core教程 (一)
    asp.net core教程 (二)
    ap.net core 教程(三)
    Grafana 安装配置
    zabbix-3.0.x LTS源码安装配置
    MariaDB Security
  • 原文地址:https://www.cnblogs.com/mypsq/p/4871257.html
Copyright © 2011-2022 走看看