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

  • 相关阅读:
    java 8 , merge()
    2026 11 12
    koda java
    Linq实现between拓展
    WinForm开发----关闭window窗体最好的办法
    ASP.NET中指定自定义HTTP响应标头
    使用Zxing.net实现asp.net mvc二维码功能
    实现asp.net mvc页面二级缓存,提高访问性能
    队列应用
    Ubuntu 12.04使用uginx+fastcgi-mono-server2部署asp.net 网站
  • 原文地址:https://www.cnblogs.com/mypsq/p/4871257.html
Copyright © 2011-2022 走看看