Implement a inheritance hierarchy(cont.) a Virtual function If certain function has different implementations in the inheritance hierarchy declare it as virtual Dynamic binding makes the corresponding version of virtual function called according to the real object type If there's virtual member function in the class the destructor should also be declared as virtual class Base void fo Base* p= new Derived virtual -Base( delete p; ; I/virtual destructor ensures resources class Derived public Base llin derived classes could be released correctly in polymorphism Derived(0;
Implement a inheritance hierarchy (cont.) ◼ Virtual function ◼ If certain function has different implementations in the inheritance hierarchy, declare it as virtual ◼ Dynamic binding makes the corresponding version of virtual function called according to the real object type ◼ If there’s virtual member function in the class, the destructor should also be declared as virtual class Base { // ... virtual ~Base(); }; class Derived : public Base { // ... ~Derived(); }; void f() { Base* p = new Derived; delete p; //virtual destructor ensures resources //in derived classes could be released //correctly in polymorphism }
Implement a inheritance hierarchy(cont.) U Base class calling void global_ print(const LibMat& mat mat. print(; Result: Create a LibMat object int main( LibMat Default Constructor!/Constructing cout <<"Create a LibMat object This is a LibMat object!/print end LibMat Destructor!/destructing LibMat libman: global_print(lbma)
Implement a inheritance hierarchy (cont.) ◼ Base class calling void global_print(const LibMat& mat) { mat.print(); } int main() { cout << “Create a LibMat object!” << endl; LibMat libmat; global_print(libmat); }; Result: Create a LibMat object! LibMat Default Constructor!//constructing This is a LibMat object!//print LibMat Destructor!//destructing
Implement a inheritance hierarchy(cont.) The Book derived class class Book: public LibMat aPublic inheritance public Book(const string& title, const string& author): m_ szTitle(title m szAuthor(author) cout<<“Book(”<< m SzTitle<“,"<< m szAuthor≤“) Constructor ≤<endl;} virtual -BookOi cout <<"Book Destructor! "< endl; y virtual void print( const cout<<“ This is a Book object!"≤<endl; cout<<“Tite:”<< m szTitle<<end cout<<“ Author:”<< m szAuthor<<endl;} const string& title([ return m_szTitle; y const string& author(i return m_ szAuthor; y protected: //will be used in derived classes, so can't be private string m_szTitle, m szAuthor
Implement a inheritance hierarchy (cont.) ◼ The Book derived class class Book : public LibMat //public inheritance { public: Book(const string& title, const string& author) : m_szTitle(title), m_szAuthor(author) { cout << “Book( ” << m_szTitle << “, ”<< m_szAuthor << “ ) Constructor!” << endl; } virtual ~Book() { cout << “Book Destructor!” << endl; } virtual void print() const { cout << “This is a Book object!” << endl; cout << “Title: ” << m_szTitle << endl; cout << “Author: ” << m_szAuthor << endl; } const string& title() { return m_szTitle; } const string& author() { return m_szAuthor; } protected: //will be used in derived classes, so can’t be private string m_szTitle, m_szAuthor; };
Implement a inheritance hierarchy(cont.) Derived class calling int main( Result: cout < Create a Book object! Create a Book obiect << endl LibMat Default Constructor! constructing Book b("The Castle", "Franz Kafka);Book(The Castle, Franz Kafka)Constructor global_ print(b); This is a Book object! lprint Title: The Castle Author: Franz Kafka Book Destructor! /destructing LibMat Destructor
Implement a inheritance hierarchy (cont.) ◼ Derived class calling int main() { cout << “Create a Book object!” << endl; Book b(“The Castle”, “Franz Kafka”); global_print(b); }; Result: Create a Book object! LibMat Default Constructor! //constructing Book(The Castle, Franz Kafka) Constructor! This is a Book object! //print Title: The Castle Author: Franz Kafka Book Destructor! //destructing LibMat Destructor!
Implement a inheritance hierarchy(cont.) The audiobook derived class class Audio Book: public Book [public AudioBook(const string& title, const string& author, const string& narrator) Book(title, author), m szNarrator( narrator) I cout <<"AudioBook(<< m_ szTitle <<","<<m szAuthor /inherited ≤<“,"<< m naRrator<<“) Constructor!”≤<endl;} from book virtual~ Audio Book){cout≤<“ Audio Book Destructor!≤<endl;} virtual void printo const cout≤<“ This is an Audio Book object! < endl; cout≤<“ Title:”<< m szTitle<<end| cout<<“ Author:”<< m szAuthor<<endl cout<<“ Narrator::”< m naRrator<<endl;} const string& narrator(i return m_szNarrator; y/title(& author( have been inherited protected: string m szNarrator;
Implement a inheritance hierarchy (cont.) ◼ The AudioBook derived class class AudioBook:public Book {public: AudioBook(const string& title, const string& author, const string& narrator) :Book(title, author), m_szNarrator(narrator) { cout << “AudioBook( ” << m_szTitle << “, ”<< m_szAuthor //inherited << “, ” << m_szNarrator << “ ) Constructor!” << endl; } //from Book virtual ~AudioBook() { cout << “AudioBook Destructor!” << endl; } virtual void print() const { cout << “This is an AudioBook object!” << endl; cout << “Title: ” << m_szTitle << endl; cout << “Author: ” << m_szAuthor << endl; cout << “Narrator: ” << m_szNarrator << endl; } const string& narrator() { return m_szNarrator; }//title() & author() have been inherited protected: string m_szNarrator; };