2017-07-09 67 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已被宣佈爲這樣叫。

相關問題