• C++ 继承


    1.继承使用的场合:写一个class,这个class和以前写过的class相比,只是增加一些成员或者对原来的成员的行为有修改,则可以继承原来的class,语法不展开说,通过代码写写几句总结。

    #include    <iostream>
    using namespace std;
    
    class  A {
    public:
        virtual void  f(void) { cout << "A'f(void)!" << endl; }
        virtual void vf() { cout << " A'vf()  running !" << endl; }
    };
    
    class  B : public  A {
    public:
        void  f(int) { cout << "B'f(int)!" << endl; }
        virtual void  vf() { cout << " B'vf()  running !" << endl; }
    };
    
    int  main() {
        B  b;
        A  &a = b;
    
        A  *pa;
        B  *pb;
    
        a.f();                   //A'f(void)!
        pa = &b;
    
        pa->f();                 //A'f(void)!
        b.f(3);                  //B'f(int)!
    
        pa = new  A;
        pa->vf();                //A'vf()  running !
        delete  pa;
        pa = NULL;
        delete  pa;
    
        pa = new  B;
        pa->vf();                 //B'vf()  running !
        delete  pa;
    
        pb = new B;
        pb->vf();                 //B'vf()  running !                
    
        pb->A::f();               //A'f(void)!  
         pb->B::f(3);             //B'f(int)!
    
        delete  pb;
    
      return 0;
    }

    基类指针创建子类对象,同名同参函数,优先调用子类函数。

    2.句柄类

    以前的博文写得过于繁琐,几天前重新梳理了一下句柄类的 实现考虑 动机,以一个简单的句柄类来说明。

    #include <iostream>
    using namespace std;
    
    class A {
    protected:
        int len;
        virtual A* clone() { return new A(*this); }
    private:
        int HandleA;
        friend class Handle;
    public:
        A() :HandleA(0) { }
        A(int a) { HandleA = a; }
        A(istream& is) { read(is); };
    
        virtual istream& read(istream& is);
    
        virtual int sum() { cout << "A'sum()" << endl; return HandleA + 100; }
        virtual ~A() {};
    
    };
    
    istream& A::read(istream& is) {
        is >> HandleA;
        return is;
    }
    
    
    
    class B : public A {
    public:
        B() :HandleB(24) { }
        B(int b) { HandleB = b; }
        B(istream& is) { read(is); }
    
        istream& read(istream& is);
    
        int sum() { cout << "B'sum()" << endl; return HandleB + 1000; }
    protected:
        B* clone() { return new B(*this); }    //private & protected both OK!
    private:
    
        int HandleB;
    };
    
    istream& B::read(istream& is) {
        is >> HandleB;
        return is;
    }
    
    class Handle {
    public:
        Handle() :pa(0) {}
        istream& read(istream& is);
    
        Handle(Handle& f) { if (f.pa) pa = f.pa->clone(); else pa = 0; }
        Handle& operator= (Handle& f) {
            if (&f != this) {
                delete pa;
                if (f.pa) pa = f.pa->clone();
                else pa = 0;
            }
            return *this;
        }
        int sum() {
            if (pa) return pa->sum();
            else { cout << "Handle's sum" << endl; pa = 0; return 42; }
        }
        ~Handle() { delete pa; }
    
    private:
        A* pa;
    };
    istream& Handle::read(istream& is) {
        delete pa;
        char ch;
        is >> ch;
        if (ch == 'a' || ch == 'A') {
            pa = new A(is);
        }
        else if (ch == 'b' || ch == 'B') {
            pa = new B(is);
        }
        else {
            pa = 0;
        }
        return is;
    }
    
    int main()
    {
        Handle f;
    
        cout << "Input A or B?" << endl;
        f.read(cin);
        Handle g(f);
        Handle h = f;
    
        cout << f.sum() << endl;
        cout << g.sum() << endl;
        cout << h.sum() << endl;
    
            return 0;    
    }
  • 相关阅读:
    boost json序列化
    boost serialize序列化
    lambda详解
    未知的生成错误““clr-namespace: test”mapping URI 无效
    无法解析的外部符号 "public: static void __cdecl std::_String_base::_Xran(void)" (?_Xran@_String_base@std@@SAXXZ)"
    LINK : fatal error LNK1123: 转换到 COFF 期间失败: 文件无效或损坏
    事实证明:软件层次的操作可以毁掉硬件
    下载随书源码的好地方
    一道简单的题目(东财)
    VS2013破解
  • 原文地址:https://www.cnblogs.com/hanxinle/p/5528546.html
Copyright © 2020-2023  润新知