码迷,mamicode.com
首页 > 编程语言 > 详细

C++ 继承

时间:2016-05-25 22:01:45      阅读:216      评论:0      收藏:0      [点我收藏+]

标签:

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;    
}

 

C++ 继承

标签:

原文地址:http://www.cnblogs.com/hanxinle/p/5528546.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!