當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


C++ Function Overriding用法及代碼示例


A Functions是一個語句塊,它們通過接受一些輸入並產生特定的輸出來共同執行特定的任務。C++ 中的函數重寫被稱為在其派生類中以相同的簽名(即返回類型和參數)重新定義基類函數。它屬於以下類別運行時多態性。

Real-Life 函數重寫示例

這個概念最好的Real-life例子是印度憲法。印度借鑒了政府機構的政治準則、結構、程序、權力和義務,規定了其他國家公民的基本權利、指導原則和義務,並自行實施;使其成為世界上最大的憲法。

另一個發展real-life 的例子可能是 RBI(印度儲備銀行)與其他國有銀行(如 SBI、PNB、ICICI 等)之間的關係。其中 RBI 行使相同的監管職能,其他銀行則按原樣遵循。

Real-Life Example of Function Overriding

函數重寫

用法:

class Parent{

  access_modifier:

    // overridden function

    return_type name_of_the_function(){}

  };

}

  class child : public Parent {

    access_modifier:

      // overriding function

      return_type name_of_the_function(){}

    };

  }

例子:

C++


// C++ program to demonstrate function overriding
#include <iostream>
using namespace std;
class Parent {
public:
    void GeeksforGeeks_Print()
    {
        cout << "Base Function" << endl;
    }
};
class Child : public Parent {
public:
    void GeeksforGeeks_Print()
    {
        cout << "Derived Function" << endl;
    }
};
int main()
{
    Child Child_Derived;
    Child_Derived.GeeksforGeeks_Print();
    return 0;
}
輸出
Derived Function

函數重寫的變化

1.從派生類調用重寫函數

C++


// C++ program to demonstrate function overriding
// by calling the overridden function
// of a member function from the child class
#include <iostream>
using namespace std;
class Parent {
public:
    void GeeksforGeeks_Print()
    {
        cout << "Base Function" << endl;
    }
};
class Child : public Parent {
public:
    void GeeksforGeeks_Print()
    {
        cout << "Derived Function" << endl;
        // call of overridden function
        Parent::GeeksforGeeks_Print();
    }
};
int main()
{
    Child Child_Derived;
    Child_Derived.GeeksforGeeks_Print();
    return 0;
}
輸出
Derived Function
Base Function
Call Overridden Function From Derived Class

的輸出從派生類調用重寫函數

2. 使用指針調用重寫函數

C++


// C++ program to access overridden function using pointer
// of Base type that points to an object of Derived class
#include <iostream>
using namespace std;
class Parent {
public:
    void GeeksforGeeks()
    {
        cout << "Base Function" << endl;
    }
};
class Child : public Parent {
public:
    void GeeksforGeeks()
    {
        cout << "Derived Function" << endl;
    }
};
int main()
{
    Child Child_Derived;
    // pointer of Parent type that points to derived1
    Parent* ptr = &Child_Derived;
    // call function of Base class using ptr
    ptr->GeeksforGeeks();
    return 0;
}
輸出
Base Function

3. 重寫函數對基類的訪問

C++


// C++ program to access overridden function
// in main() using the scope resolution operator ::
#include <iostream>
using namespace std;
class Parent {
public:
    void GeeksforGeeks()
    {
        cout << "Base Function" << endl;
    }
};
class Child : public Parent {
public:
    void GeeksforGeeks()
    {
        cout << "Derived Function" << endl;
    }
};
int main()
{
     Child Child_Derived;
     Child_Derived.GeeksforGeeks();
    // access GeeksforGeeks() function of the Base class
     Child_Derived.Parent::GeeksforGeeks();
    return 0;
}
輸出
Derived Function
Base Function
Access of Overridden Function to the Base Class

重寫函數對基類的訪問

4. 訪問重寫函數

C++


// C++ Program Demonstrating
// Accessing of Overridden Function
#include <iostream>
using namespace std;
// defining of the Parent class
class Parent
{
public:
    // defining the overridden function
    void GeeksforGeeks_Print()
    {
        cout << "I am the Parent class function" << endl;
    }
};
// defining of the derived class
class Child : public Parent
{
public:
    // defining of the overriding function
    void GeeksforGeeks_Print()
    {
        cout << "I am the Child class function" << endl;
    }
};
int main()
{
    // create instances of the derived class
    Child GFG1, GFG2;
    // call the overriding function
    GFG1.GeeksforGeeks_Print();
    // call the overridden function of the Base class
    GFG2.Parent::GeeksforGeeks_Print();
    return 0;
}
輸出
I am the Child class function
I am the Parent class function

函數重載與函數重寫

Function Overloading

Function Overriding

它屬於編譯時多態性 它屬於運行時多態性
一個函數可以被重載多次,因為它是在編譯時解析的 函數不能被重寫多次,因為它是在運行時解析的
無需繼承即可執行 沒有繼承就無法執行
他們在同一個範圍內 它們的範圍不同。

要了解更多信息,您可以參考 函數重載 VS 函數重寫.



相關用法


注:本文由純淨天空篩選整理自harsh_shokeen大神的英文原創作品 Function Overriding in C++。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。