C++中實現(xiàn)接口繼承與實現(xiàn)繼承的方法及它們的區(qū)別
概念
在 C++ 中,接口繼承和實現(xiàn)繼承是兩種不同的繼承方式,它們在設(shè)計模式、代碼復(fù)用和多態(tài)性方面有著不同的應(yīng)用。下面將分別解釋這兩者的概念、實現(xiàn)方式及其區(qū)別。
接口繼承
接口繼承指的是只繼承類的接口(即公共的成員函數(shù)聲明)而不實現(xiàn)這些函數(shù)。通常通過純虛函數(shù)來實現(xiàn)。這樣做的目的是約定一個行為規(guī)范,而具體的實現(xiàn)則留給派生類去完成。
示例:
#include <iostream> // 定義一個接口 class Drawable { public: virtual void draw() const = 0; // 純虛函數(shù) virtual ~Drawable() {} // 虛析構(gòu)函數(shù)以保證正確的析構(gòu) }; class Circle : public Drawable { public: void draw() const override { std::cout << "Drawing a circle." << std::endl; } }; class Square : public Drawable { public: void draw() const override { std::cout << "Drawing a square." << std::endl; } }; void render(const Drawable& shape) { shape.draw(); // 調(diào)用接口的實現(xiàn) } int main() { Circle circle; Square square; render(circle); // 輸出: Drawing a circle. render(square); // 輸出: Drawing a square. return 0; }
輸出
Drawing a circle.
Drawing a square.
代碼解析
接口定義
class Drawable { public: virtual void draw() const = 0; // 純虛函數(shù) virtual ~Drawable() {} // 虛析構(gòu)函數(shù)以保證正確的析構(gòu) };
- 類 Drawable 是一個接口類,使用大寫字母開頭的名稱有助于表明它是一個接口。
- virtual void draw() const = 0; 聲明了一個純虛函數(shù) draw(),這意味著 Drawable 類沒有實現(xiàn)此函數(shù),任何派生類必須實現(xiàn)這個函數(shù)。這里的 = 0 表示這是一個純虛函數(shù),任何包含它的類都被視為抽象類,無法直接實例化。
- virtual ~Drawable() {} 是一個虛析構(gòu)函數(shù),允許使用基類指針刪除派生類實例時確保資源正確釋放。這是接口設(shè)計的良好實踐。
- 實現(xiàn)接口的派生類
class Circle : public Drawable { public: void draw() const override { std::cout << "Drawing a circle." << std::endl; } };
- class Circle : public Drawable 表示 Circle 類繼承自 Drawable 接口,派生類必須實現(xiàn)所有的純虛函數(shù)。
- void draw() const override 這是對基類 Drawable 中 draw() 的實現(xiàn)。通過 override 關(guān)鍵字指示這是對基類虛函數(shù)的重寫。
- 在 draw() 方法中,我們使用 std::cout 輸出 “Drawing a circle.”。
class Square : public Drawable { public: void draw() const override { std::cout << "Drawing a square." << std::endl; } };
- Square 類實現(xiàn)與 Circle 類類似,只是輸出的信息不同。它同樣繼承了 Drawable 接口并實現(xiàn) draw() 方法。
- 渲染函數(shù)
void render(const Drawable& shape) { shape.draw(); // 調(diào)用接口的實現(xiàn) }
- render 函數(shù) 接受一個常量引用類型的 Drawable 對象,這允許傳入任何實現(xiàn)了 Drawable 接口的對象。
- 在函數(shù)內(nèi)部調(diào)用 shape.draw(),通過多態(tài)的機制,這將調(diào)用傳入對象的具體 draw() 實現(xiàn),打印出形狀信息。
- 主函數(shù)
int main() { Circle circle; Square square; render(circle); // 輸出: Drawing a circle. render(square); // 輸出: Drawing a square. return 0; }
- Circle circle; 和 Square square; 創(chuàng)建了 Circle 和 Square 對象。
- render(circle); 和 render(square); 調(diào)用了 render 函數(shù),分別傳入 circle 和 square 對象。
- 由于 render 函數(shù)使用了接口類型 Drawable 的引用,因此可以通過多態(tài)機制實現(xiàn)適當?shù)暮瘮?shù)調(diào)用。
關(guān)鍵點
- Drawable 類是一個接口,它定義了一個純虛函數(shù) draw()。
- Circle 和 Square 類實現(xiàn)了接口,重寫了 draw() 函數(shù)。
- 可以通過接口類型的引用或指針來實現(xiàn)多態(tài)。
總結(jié)
- 接口繼承的靈活性:通過使用抽象類(接口),我們可以定義統(tǒng)一的行為規(guī)范(draw())。不同的實現(xiàn)類(Circle 和 Square)可以各自實現(xiàn)該接口的函數(shù),從而提供特定的行為。這種設(shè)計模式使得代碼的可擴展性和可維護性得以提高。
- 多態(tài)性:使用指向接口類的指針或引用可以實現(xiàn)多態(tài),允許我們在不修改 render 函數(shù)的情況下輕松添加新的形狀類。
- 虛構(gòu)造和釋放:使用虛析構(gòu)函數(shù)確保在刪除基類指針時能夠正確地調(diào)用派生類的析構(gòu)函數(shù),從而避免內(nèi)存泄露。
實現(xiàn)繼承
實現(xiàn)繼承指的是繼承一個類的完整實現(xiàn),而不僅僅是接口。這意味著子類不僅可以使用父類的方法和屬性,還可以訪問和重用父類的實現(xiàn)。這種方式通常在需要共享代碼的場景中使用。
示例:
#include <iostream> // 基類 class Shape { public: void setPosition(int x, int y) { m_x = x; m_y = y; } protected: int m_x, m_y; // 保護的成員變量 }; class Circle : public Shape { public: void draw() { std::cout << "Drawing a circle at (" << m_x << ", " << m_y << ")." << std::endl; } }; class Square : public Shape { public: void draw() { std::cout << "Drawing a square at (" << m_x << ", " << m_y << ")." << std::endl; } }; int main() { Circle circle; circle.setPosition(10, 20); circle.draw(); // 輸出: Drawing a circle at (10, 20). Square square; square.setPosition(30, 40); square.draw(); // 輸出: Drawing a square at (30, 40). return 0; }
輸出
Drawing a circle at (10, 20).
Drawing a square at (30, 40).
代碼解析
基類定義
class Shape { public: void setPosition(int x, int y) { m_x = x; m_y = y; } protected: int m_x, m_y; // 保護的成員變量 };
- 基類 Shape 是一個描述形狀的類。
- setPosition(int x, int y) 方法 是一個公共方法,用于設(shè)置形狀的坐標。通過設(shè)置 m_x 和 m_y 成員變量,基類提供了一個基本的行為。
- protected 修飾符 表示 m_x 和 m_y 只能在 Shape 類及其派生類中訪問。這賦予了派生類對這些成員的訪問權(quán)限,允許它們讀取和修改位置。
- 派生類定義
class Circle : public Shape { public: void draw() { std::cout << "Drawing a circle at (" << m_x << ", " << m_y << ")." << std::endl; } };
- class Circle : public Shape 表示 Circle 類派生自 Shape 類,繼承了所有公共和保護成員。
- void draw() 方法 在 Circle 類中實現(xiàn),負責輸出當前圓形的坐標信息。
class Square : public Shape { public: void draw() { std::cout << "Drawing a square at (" << m_x << ", " << m_y << ")." << std::endl; } };
- class Square : public Shape 類似于 Circle 類,Square 也繼承自 Shape 類。
- draw() 方法負責輸出當前正方形的坐標信息。
- 主函數(shù)實現(xiàn)
int main() { Circle circle; circle.setPosition(10, 20); circle.draw(); // 輸出: Drawing a circle at (10, 20). Square square; square.setPosition(30, 40); square.draw(); // 輸出: Drawing a square at (30, 40). return 0; }
- 在 main() 函數(shù)中,首先創(chuàng)建了一個 Circle 對象。
- circle.setPosition(10, 20); 調(diào)用基類的 setPosition 方法設(shè)置圓形的坐標。
- circle.draw(); 調(diào)用 Circle 類的 draw() 方法輸出圓形的坐標。
- 類似的,對 Square 類進行相同的操作,設(shè)置其位置并輸出。
關(guān)鍵點
- Shape 類提供了一些具體的實現(xiàn),比如 setPosition() 方法來設(shè)置形狀的位置。
- Circle 和 Square 類繼承自 Shape,它們可以使用 Shape 中已實現(xiàn)的方法。
- 子類可以重用父類的代碼,也可以擴展其功能。
總結(jié)
- 實現(xiàn)繼承的作用:
- Circle 和 Square 類繼承自 Shape 類,能夠重用基類的代碼,特別是 setPosition 方法,避免了重復(fù)實現(xiàn)。
- 通過繼承,所有形狀類可以共享 Shape 類的成員和方法,簡化代碼結(jié)構(gòu)。
- 訪問控制:
- 使用 protected 修飾符允許派生類訪問基類的成員變量,保持數(shù)據(jù)的封裝性,但同時允許派生類訪問必要的數(shù)據(jù)。
- 良好的可擴展性:
- 由于 Shape 類作為基類,可以輕松添加更多形狀(例如 Triangle),只需繼承 Shape 類并實現(xiàn) draw() 方法而不需要重復(fù)代碼。
- 多態(tài)性:
- 在這個簡單的實現(xiàn)中沒有用到多態(tài)性,但可以通過把 draw() 方法聲明為虛函數(shù)和使用基類指針或引用來增強多態(tài)特性,這樣可以通過基類類型處理不同的派生類對象。
區(qū)別
到此這篇關(guān)于C++中如何實現(xiàn)接口繼承與實現(xiàn)繼承,以及它們的區(qū)別?的文章就介紹到這了,更多相關(guān)C++接口繼承與實現(xiàn)繼承內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
c++實現(xiàn)十進制轉(zhuǎn)換成16進制示例
這篇文章主要介紹了c++實現(xiàn)十進制轉(zhuǎn)換成16進制示例,需要的朋友可以參考下2014-05-05基于C++詳解數(shù)據(jù)結(jié)構(gòu)(附帶例題)
數(shù)據(jù)結(jié)構(gòu)作為每一個IT人不可回避的問題,本文基于C++編寫,下面這篇文章主要給大家介紹了關(guān)于數(shù)據(jù)結(jié)構(gòu)的相關(guān)資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下2022-06-06C語言數(shù)據(jù)結(jié)構(gòu)算法基礎(chǔ)之循環(huán)隊列示例
這篇文章主要為大家介紹了C語言數(shù)據(jù)結(jié)構(gòu)算法基礎(chǔ)之循環(huán)隊列,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-06-06