1. 程式人生 > 其它 >類之間的關係:繼承關係

類之間的關係:繼承關係

技術標籤:c++

1.類圖

泛化和繼承其實是一個逆過程 泛化就是有子類抽象出一個父類 而繼承就是由父類具體化一個子類 例如足球比聯賽跟什麼西甲 意甲 英超之間就是泛化/繼承的關係

2.程式碼實現

#include<iostream>
using namespace std;

class Base
{
public:

    Base()
    {
         cout << "Base()" << endl;
    }

    ~Base()
    {
        cout << "~Base()" << endl;
    }

    void showBase()
    {
         cout << "_pub=" << _pub << endl;
    }

public:
    int _pub;
    int _pro;
    int _pri;

};

class Derive :public Base
{
public:
    Derive()
    {
        cout << "Derive()" << endl;
    }
    void showDerive()
    {
        cout << "_pub=" <<_pub << endl;
    }
    ~Derive()
    {
        cout << "~Derive()" << endl;
    }
};

void FunTest()
{
    Derive d;
    d._pub