• 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

  • 相关阅读:
    轻松构建微服务之分布式事物
    Java和操作系统交互细节
    网络内核之TCP是如何发送和接收消息的
    最全的微服务知识科普
    【译】TCP Implementation in Linux
    轻松构建微服务之分库分表
    OSX的一些基本知识
    Delphi中使用比较少的一些语法
    热烈祝贺我的博客开通
    Windows 服务快捷启动命令
  • 原文地址:https://www.cnblogs.com/mypsq/p/4871257.html
Copyright © 2020-2023  润新知