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

  • 相关阅读:
    JS 笔记
    html笔记 横向两列布局
    jsp HTTP Status 405
    有效范围为request的bean
    jsp:session对象存储数据
    sql笔记
    StringBuffer的用法
    VB学习笔记
    html 笔记
    Linux 笔记
  • 原文地址:https://www.cnblogs.com/mypsq/p/4871257.html
Copyright © 2011-2022 走看看