虛函數
虛函數是應在派生類中重新定義的成員函數。 當使用指針或對基類的引用來引用派生的類對象時,可以為該對象調用虛函數并執行該函數的派生類版本。
虛函數確保為該對象調用正確的函數,這與用于進行函數調用的表達式無關。
假定基類包含聲明為 virtual 的函數,并且派生類定義了相同的函數。 為派生類的對象調用派生類中的函數,即使它是使用指針或對基類的引用來調用的。 以下示例顯示了一個基類,它提供了 PrintBalance 函數和兩個派生類的實現
// deriv_VirtualFunctions.cpp// compile with: /EHsc#include <iostream>using namespace std;class Account {public: Account( double d ) { _balance = d; } virtual double GetBalance() { return _balance; } virtual void PrintBalance() { cerr << "Error. Balance not available for base type." << endl; }private: double _balance;};class CheckingAccount : public Account {public: CheckingAccount(double d) : Account(d) {} void PrintBalance() { cout << "Checking account balance: " << GetBalance() << endl; }};class SavingsAccount : public Account {public: SavingsAccount(double d) : Account(d) {} void PrintBalance() { cout << "Savings account balance: " << GetBalance(); }};int main() { // Create objects of type CheckingAccount and SavingsAccount. CheckingAccount *pChecking = new CheckingAccount( 100.00 ) ; SavingsAccount *pSavings = new SavingsAccount( 1000.00 ); // Call PrintBalance using a pointer to Account. Account *pAccount = pChecking; pAccount->PrintBalance(); // Call PrintBalance using a pointer to Account. pAccount = pSavings; pAccount->PrintBalance(); }
在前面的代碼中,對 PrintBalance 的調用是相同的,pAccount 所指向的對象除外。 由于 PrintBalance 是虛擬的,因此將調用為每個對象定義的函數版本。 派生類 PrintBalance 和 CheckingAccount 中的 SavingsAccount 函數“重寫”基類 Account 中的函數。
如果聲明的類不提供 PrintBalance 函數的重寫實現,則使用基類 Account 中的默認實現。
派生類中的函數僅在基類中的虛函數的類型相同時重寫這些虛函數。 派生類中的函數不能只是與其返回類型中的基類的虛函數不同;參數列表也必須不同。
當使用指針或引用調用函數時,以下規則將適用:
根據為其調用的對象的基本類型來解析對虛函數的調用。
根據指針或引用的類型來解析對非虛函數的調用。
以下示例說明在通過指針調用時虛函數和非虛函數的行為:
// deriv_VirtualFunctions2.cpp// compile with: /EHsc#include <iostream>using namespace std;class Base {public: virtual void NameOf(); // Virtual function. void InvokingClass(); // Nonvirtual function.};// Implement the two functions.void Base::NameOf() { cout << "Base::NameOf/n";}void Base::InvokingClass() { cout << "Invoked by Base/n";}class Derived : public Base {public: void NameOf(); // Virtual function. void InvokingClass(); // Nonvirtual function.};// Implement the two functions.void Derived::NameOf() { cout << "Derived::NameOf/n";}void Derived::InvokingClass() { cout << "Invoked by Derived/n";}int main() { // Declare an object of type Derived. Derived aDerived; // Declare two pointers, one of type Derived * and the other // of type Base *, and initialize them to point to aDerived. Derived *pDerived = &aDerived; Base *pBase = &aDerived; // Call the functions. pBase->NameOf(); // Call virtual function. pBase->InvokingClass(); // Call nonvirtual function. pDerived->NameOf(); // Call virtual function. pDerived->InvokingClass(); // Call nonvirtual function.}
輸出
Derived::NameOfInvoked by BaseDerived::NameOfInvoked by Derived
請注意,無論 NameOf 函數是通過指向 Base 的指針還是通過指向 Derived 的指針進行調用,它都會調用 Derived 的函數。 它調用 Derived 的函數,因為 NameOf 是虛函數,并且 pBase 和 pDerived 都指向類型 Derived 的對象。
由于僅為類類型的對象調用虛函數,因此不能將全局函數或靜態函數聲明為 virtual。
在派生類中聲明重寫函數時可使用 virtual 關鍵字,但它不是必需的;虛函數的重寫始終是虛擬的。
必須定義基類中的虛函數,除非使用 pure-specifier 聲明它們。 (有關純虛函數的詳細信息,請參閱抽象類。)
可通過使用范圍解析運算符 (::) 顯式限定函數名稱來禁用虛函數調用機制。 考慮先前涉及 Account 類的示例。 若要調用基類中的 PrintBalance,請使用如下所示的代碼:
CheckingAccount *pChecking = new CheckingAccount( 100.00 );pChecking->Account::PrintBalance(); // Explicit qualification.Account *pAccount = pChecking; // Call Account::PrintBalancepAccount->Account::PrintBalance(); // Explicit qualification.
在前面的示例中,對 PrintBalance 的調用將禁用虛函數調用機制。
單個繼承
在“單繼承”(繼承的常見形式)中,類僅具有一個基類。考慮下圖中闡釋的關系。
簡單單繼承關系圖
注意該圖中從常規到特定的進度。在大多數類層次結構的設計中發現的另一個常見特性是,派生類與基類具有“某種”關系。在該圖中,Book 是一種 PrintedDocument,而 PaperbackBook 是一種 book。
該圖中的另一個要注意的是:Book 既是派生類(來自 PrintedDocument),又是基類(PaperbackBook 派生自 Book)。此類類層次結構的框架聲明如下面的示例所示:
// deriv_SingleInheritance.cpp// compile with: /LDclass PrintedDocument {};// Book is derived from PrintedDocument.class Book : public PrintedDocument {};// PaperbackBook is derived from Book.class PaperbackBook : public Book {};
PrintedDocument 被視為 Book 的“直接基”類;它是 PaperbackBook 的“間接基”類。差異在于,直接基類出現在類聲明的基礎列表中,而間接基類不是這樣的。
在聲明派生的類之前聲明從中派生每個類的基類。為基類提供前向引用聲明是不夠的;它必須是一個完整聲明。
在前面的示例中,使用訪問說明符 public。 成員訪問控制中介紹了公共的、受保護的和私有的繼承的含義。
類可用作多個特定類的基類,如下圖所示。
注意
有向非循環圖對于單繼承不是唯一的。它們還用于表示多重繼承關系圖。 多重繼承中對本主題進行了說明。
在繼承中,派生類包含基類的成員以及您添加的所有新成員。因此,派生類可以引用基類的成員(除非在派生類中重新定義這些成員)。當在派生類中重新定義了直接或間接基類的成員時,范圍解析運算符 (::) 可用于引用這些成員。請看以下示例:
// deriv_SingleInheritance2.cpp// compile with: /EHsc /c#include <iostream>using namespace std;class Document {public: char *Name; // Document name. void PrintNameOf(); // Print name.};// Implementation of PrintNameOf function from class Document.void Document::PrintNameOf() { cout << Name << endl;}class Book : public Document {public: Book( char *name, long pagecount );private: long PageCount;};// Constructor from class Book.Book::Book( char *name, long pagecount ) { Name = new char[ strlen( name ) + 1 ]; strcpy_s( Name, strlen(Name), name ); PageCount = pagecount;};
請注意,Book 的構造函數 (Book::Book) 具有對數據成員 Name 的訪問權。在程序中,可以創建和使用類型為 Book 的對象,如下所示:
// Create a new object of type Book. This invokes the// constructor Book::Book.Book LibraryBook( "Programming Windows, 2nd Ed", 944 );...// Use PrintNameOf function inherited from class Document.LibraryBook.PrintNameOf();
如前面的示例所示,以相同的方式使用類成員和繼承的數據和函數。如果類 Book 的實現調用 PrintNameOf 函數的重新實現,則只能通過使用范圍解析 (Document) 運算符來調用屬于 :: 類的函數:
// deriv_SingleInheritance3.cpp// compile with: /EHsc /LD#include <iostream>using namespace std;class Document {public: char *Name; // Document name. void PrintNameOf() {} // Print name.};class Book : public Document { Book( char *name, long pagecount ); void PrintNameOf(); long PageCount;};void Book::PrintNameOf() { cout << "Name of book: "; Document::PrintNameOf();}
如果存在可訪問的明確基類,則可以隱式將派生類的指針和引用轉換為其基類的指針和引用。下面的代碼使用指針演示了此概念(相同的原則適用于引用):
// deriv_SingleInheritance4.cpp// compile with: /W3struct Document { char *Name; void PrintNameOf() {}};class PaperbackBook : public Document {};int main() { Document * DocLib[10]; // Library of ten documents. for (int i = 0 ; i < 10 ; i++) DocLib[i] = new Document;}
在前面的示例中,創建了不同的類型。但是,由于這些類型都派生自 Document 類,因此存在對 Document * 的隱式轉換。因此,DocLib 是“異類列表”(其中包含的所有對象并非屬于同一類型),該列表包含不同類型的對象。
由于 Document 類具有一個 PrintNameOf 函數,因此它可以打印庫中每本書的名稱,但它可能會忽略某些特定于文檔類型的信息(Book 的頁計數、HelpFile 的字節數等)。
注意
強制使用基類來實現函數(如 PrintNameOf)通常不是最佳設計。 虛函數提供其他設計替代方法。