zoukankan      html  css  js  c++  java
  • c++学习-多态性

    强制转换父类对象为子类

    #include<iostream>
    #include<string>
    #include <typeinfo>  
    using namespace std;
    
    
    class father{
    
    public:
        void smart(){}
        virtual ~father(){}
    };
    
    class son : public father
    {
    public:
        void say()
        {
            cout << "say" << endl;
        }
    };
    
    void main()
    {
    
        father *p;
        p= new son;
        dynamic_cast<son*>(p)->say();
        
    }

    多重继承

    #include<iostream>
    #include<string>
    #include <typeinfo>  
    using namespace std;
    
    
    class father{
    
    public:
         father(){ cout << "father cons" << endl; }
         virtual ~father(){ cout << "father des" << endl; }
         virtual void say()
         {
             cout << "father say" << endl;
         }
    };
    
    class mother{
    
    public:
        mother(){ cout << "mother cons" << endl; }
        virtual ~mother(){ cout << "mother des" << endl; }
        virtual void run()
        {
            cout << "mother run" << endl;
        }
    };
    
    
    class son : public mother, public father
    {
    public:
        void run()
        {
            cout << "son run" << endl;
        }
    
        son(){ cout << "son cons" << endl; }
        ~son(){ cout << "son des" << endl; }
    
    };
    
    void main()
    {
    
        mother *p;
        
        p = new son;
        p->run();
    
        delete p;
    
    
        //dynamic_cast<son*>(p)->say();
        
    }

    同时访问 父类和母类中的函数:

    模拟抽象类

    #include<iostream>
    #include<string>
    #include <typeinfo>  
    using namespace std;
    
    
    class human{
    public:
        virtual void say(){}
        virtual void run(){}
        virtual ~human(){ cout << "human des" << endl; }
        human(){ cout << "human cons" << endl; }
    };
    
    class father:virtual public human{ //虚基类
    
    public:
         father(){ cout << "father cons" << endl; }
         ~father(){ cout << "father des" << endl; }
         void say()
         {
             cout << "father say" << endl;
         }
    };
    
    class mother :virtual public human{
    
    public:
        mother(){ cout << "mother cons" << endl; }
        ~mother(){ cout << "mother des" << endl; }
        void run()
        {
            cout << "mother run" << endl;
        }
    };
    
    
    class son : public mother, public father
    {
    public:
        
        son(){ cout << "son cons" << endl; }
        ~son(){ cout << "son des" << endl; }
    };
    
    void main()
    {
    
        human *p;
    
        p = new son;
    
        delete p;
    
        
    }

    抽象类:

    #include<iostream>
    #include<string>
    #include <typeinfo>  
    using namespace std;
    
    
    class human{
    public:
        virtual void say() = 0;//纯虚函数
        virtual void run() = 0;
        virtual ~human(){ cout << "human des" << endl; }
        human(){ cout << "human cons" << endl; }
    };
    
    class father:virtual public human{ //虚基类
    
    public:
         father(){ cout << "father cons" << endl; }
         ~father(){ cout << "father des" << endl; }
         void say()
         {
             cout << "father say" << endl;
         }
    };
    
    class mother :virtual public human{
    
    public:
        mother(){ cout << "mother cons" << endl; }
        ~mother(){ cout << "mother des" << endl; }
        void run()
        {
            cout << "mother run" << endl;
        }
    };
    
    
    class son : public mother, public father
    {
    public:
        
        son(){ cout << "son cons" << endl; }
        ~son(){ cout << "son des" << endl; }
    };
    
    void main()
    {
        human *p;
    
        p = new son;
        p->say();
        delete p;
    
        
    }

    抽象类实例:

    #include<iostream>
    #include<string>
    #include <typeinfo>  
    using namespace std;
    
    class alpha{
    public:
        virtual void ee(){};
        virtual ~alpha(){ cout << "alpha des" << endl; };
        alpha(){ cout << "alpha cons" << endl; };
    };
    
    class A: public alpha{
    public:
        void ee(){ cout <<"A"<< endl; }
        A(){ cout <<"A cons"<< endl; };
        ~A(){ cout << "A des" << endl; };
    
    };
    
    class B : public A{
    public:
        void ee(){ cout << "B" << endl; }
        ~B(){}
    };
    
    
    void main()
    {
        alpha *p;
    
        p = new A;
        p->ee();
    
        p = new B;
        p->ee();
    
        delete p;
        
    }
  • 相关阅读:
    Spring总结九:事务管理机制
    Spring总结七:AOP动态代理的实现
    Spring总结六:AOP(面向切面编程)
    Nginx静态网站的部署
    Spring总结五:小结 使用spring访问servlet
    javascript 的dateObj.getTime() 在为C#的获取方式
    操作JavaScript数组
    判断是否是对象的原型
    JavaScript判断对象 是什么类型的.
    Javascript中类型: undefined, number ,string ,object ,boolean
  • 原文地址:https://www.cnblogs.com/siqi/p/4621375.html
Copyright © 2011-2022 走看看