Inheritance and Polymorphism of C++ Classes

C++ object oriented

  • The two main characteristics of object orientation are: inheritance and polymorphism

roaming

  • By default, the resolution of member functions is done dynamically at compile time. If it needs to be done dynamically at runtime, we need to add the virtual keyword before its declaration.
  • If there is a class A, which is the base class of class B, when an object of class B is defined, the constructors and destructors of both A and B will be executed (execution order is reversed).
  • If there are multiple levels of derivation, the constructor and destructor of the base class will be called in turn until there is no base class.
  • A static member function cannot be declared virtual

  • LibMat.h

    #ifndef  LIBMAT_H
    #define LIBMAT_H
    
    #include <cstdlib>
    #include <string>
    #include <iostream>
    #include <ostream>
    #include <assert.h>
    #include <time.h>
    #include <algorithm>
    #include <functional>
    #include <iterator>
    #include <fstream>
    #include <vector>
    using namespace std;
    
    class LibMat
    {
    public:
        LibMat()
        {
            cout << "LibMat::LibMat() default constructor!" << endl;
        }
    
        // 对于经过virtual修饰的member function,会在runtime进行动态解析
        virtual ~LibMat()
        {
            cout << "LibMat::~LibMat() default destructor!" << endl;
        }
        virtual void print() const
        {
            cout << "LibMat::print() -- I am a LibMat object!" << endl;
        }
    
    };
    #endif
    
  • Book.h

    #ifndef  BOOK_H
    #define  BOOK_H
    
    #include "LibMat.h"
    using namespace std;
    
    class Book : public LibMat
    {
    public:
        Book(const string &title, const string& author)
            :_title(title), _author(author)
        {
            cout << "Book::Book( " << _title << ", "
                << _author << " ) constructor" << endl;
        }
    
        virtual ~Book()
        {
            cout << "Book::~Book() default destructor!" << endl;
        }
    
        virtual void print()
        {
            cout << "Book:print() -- I am a Book object." << endl
                << "\tMy title is: " << _title << endl
                << "\tMy author is: " << _author << endl;
        }
    
        const string &title() const { return _title; }
        const string &author() const { return _author; }
    
    protected:
        string _title;
        string _author;
    };
    #endif
    
  • AudioBook.h

    #ifndef  AUDIOBOOK_H
    #define  AUDIOBOOK_H
    
    #include "Book.h"
    using namespace std;
    
    class AudioBook : public Book
    {
    public:
        AudioBook(const string &title, const string& author, const string& narrator)
            :Book(title, author), _narrator(narrator)
        {
            cout << "AudioBook::AudioBook( " << _title << ", "
                << _author << ", " << _narrator  << " ) constructor" << endl;
        }
    
        virtual ~AudioBook()
        {
            cout << "AudioBook::~AudioBook() default destructor!" << endl;
        }
    
        virtual void print()
        {
            cout << "AudioBook:print() -- I am a AudioBook object." << endl
                << "\t\tMy title is: " << _title << endl
                << "\t\tMy author is: " << _author << endl
                << "\t\tMy narrator is: " << _narrator << endl;
        }
    
        const string &narrator() const { return _narrator; }
    
    
    protected:
        string _narrator;
    };
    #endif
    
  • main.cpp

    #include "AudioBook.h"
    using namespace std;
    typedef long long ll;
    
    // 当定义一个派生对象时,基类和派生类的constructor都会被执行,destructor也都会被执行(但是次序颠倒)
    void testClass()
    {
        //LibMat mat;
        //mat.print();
    
        //Book book("book1", "author1");
        //book.print();
    
        AudioBook abook("book2", "author2", "narrator2");
        abook.print();
    
    }
    
    int main()
    {
        // 按照时间来初始化种子,保证每次结果不一样
        srand( (int)time(0) );
        //cout << rand() << endl;
    
        testClass();
    
        system("pause");
        return 0;
    }
    

other

  • For the class member function of static type, you need to declare it outside the class (usually in the h file), and then define it (can be in the cpp file or the h file), otherwise there will be unresolved externalserrors like this, you can Refer to this thread: https://bbs.csdn.net/topics/320269587

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325486043&siteId=291194637