如何从派生类方法调用基类方法?

时间:2013-11-27 22:53:23

标签: c++

class base
{
    int a, b;
public:
    bool valid();
    {
        bool ok = false;
        if (a > 5 && a < 10 && b > 2 && b < 8)
            ok = true;
            return ok;
    }
};

class derived : public base
{
    int a;
public:
    bool valid();
    {
        bool ok = false;
        if (a < 8 && a > 15 && // call base's class valid method;
            ok = true;
            return ok;
    }
};

如何在派生类有效方法中调用基类有效方法?

2 个答案:

答案 0 :(得分:0)

base::valid()班级代码中的任意位置使用derived

答案 1 :(得分:0)

class derived:public base
{

    int a;
    public:
        bool valid();
        {
              bool ok= false;
              if(a<8 && a>15 && base::valid()) //call base's class valid method;
                 ^^^^^^^^^^^
                  side note: think about changing this condition
                             probably a>8 && a<15
                  ok = true; 
              return ok;

        }

};