从派生类调用重载函数

时间:2017-07-09 10:11:56

标签: c++ inheritance

我有两个问题: 1.如果基类和派生类具有相同的成员函数 - compute(),并且如果我创建派生类的对象并调用derived_obj.compute(),那么调用哪个类的函数?

2.你能证明输出的合理性:

     #include<iostream.h> 
    #include<iostream.h> 
class Base
{
    int x, y, z; 
    public: 
    Base()
    {
        x = y = z = 0;
    }
    Base(int xx, int yy = 'A', int zz = 'B')
    {
        x = xx;
        y = x + yy;
        z = x + y;
    }
    void Display(void)
    {
        cout<< x << " " << y << " " << z << endl;
    }
};
class Derived : public Base
{
    int x, y; 
    public:
    Derived(int xx = 65, int yy = 66) : Base(xx, yy)
    {
        y = xx; 
        x = yy;
    }
    void Display(void)
    {
        cout<< x << " " << y << " ";
        Display(); 
    }
};
int main()
{
    Derived objD;
    objD.Display();
    return 0; 
}

1 个答案:

答案 0 :(得分:0)

在这种情况下,调用派生类的函数是因为对象objD已经被声明为。