2017-07-09 70 views
-2

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

2.Could你证明这个输出:

 #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; 
} 
+0

“compute()”和“derived_obj”在哪里? –

+0

在你的主体上创建一个Derived对象,并调用Derived.Display成员函数..这里没有秘密。 –

+0

而在答案的标题中,“重载”应改为“重写” –

回答

0

在这种情况下,派生类的函数,因为对象objD已被宣布为这样叫。

相关问题