首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> C語言 >> C++ >> 關於C++

C++ 植物類繼承體系:虛繼承解決菱形問題完整實現

     編輯:關於C++

類圖結構

       Botany (虛基類)
      /       \
  Tree      Flower
      \       /
   MicheliaAlba (白蘭花)

完整代碼(三文件分離)

plant.h

#pragma once
#include 
using namespace std;

class Botany {
public:
    Botany(const string& name);
    virtual ~Botany();
    virtual void Display();
    Botany(const Botany& b);
protected:
    string _name;
    static size_t _sCount;
};

class Tree : virtual public Botany {
public:
    Tree(const string& name, int h);
    virtual ~Tree();
    virtual void Display();
    Tree(const Tree& t);
protected:
    int _height;
};

class Flower : virtual public Botany {
public:
    Flower(const string& name, const string& color);
    virtual ~Flower();
    virtual void Display();
    Flower(const Flower& f);
protected:
    string _color;
};

class MicheliaAlba : public Flower, public Tree {
public:
    MicheliaAlba(const string& name, const string& color, int h);
    virtual ~MicheliaAlba();
    virtual void Display();
    MicheliaAlba(const MicheliaAlba& m);
protected:
    string _kind;
};

plant.cpp(關鍵實現)

size_t Botany::_sCount = 0;

Botany::Botany(const string& name) : _name(name) { ++_sCount; cout << "Botany\n"; }
Botany::~Botany() { cout << "~Botany\n"; }
void Botany::Display() { cout << "Botany: " << _name << " " << _sCount << "\n"; }
Botany::Botany(const Botany& b) : _name(b._name) {}

Tree::Tree(const string& name, int h) : Botany(name), _height(h) { cout << "Tree\n"; }
Tree::~Tree() { cout << "~Tree\n"; }
void Tree::Display() { cout << "Tree: " << _name << " h=" << _height << " " << _sCount << "\n"; }
Tree::Tree(const Tree& t) : Botany(t._name), _height(t._height) {}

Flower::Flower(const string& name, const string& color) : Botany(name), _color(color) { cout << "Flower\n"; }
Flower::~Flower() { cout << "~Flower\n"; }
void Flower::Display() { cout << "Flower: " << _name << " c=" << _color << " " << _sCount << "\n"; }
Flower::Flower(const Flower& f) : Botany(f._name), _color(f._color) {}

// 虛基類由最派生類構造
MicheliaAlba::MicheliaAlba(const string& name, const string& color, int h)
    : Botany(name), Flower(name, color), Tree(name, h) { cout << "MicheliaAlba\n"; }
MicheliaAlba::~MicheliaAlba() { cout << "~MicheliaAlba\n"; }
void MicheliaAlba::Display() {
    cout << "MicheliaAlba: " << Flower::_name << " " << Flower::_color << " " << Tree::_height << " " << _sCount << "\n";
}
MicheliaAlba::MicheliaAlba(const MicheliaAlba& m)
    : Botany(m._name), Flower(m._name, m._color), Tree(m._name, m._height) {}

main.cpp

#include "plant.h"
int main() {
    MicheliaAlba m("白蘭花", "紅色", 20);
    m.Display();
    MicheliaAlba m2(m);
    m2.Display();
    m.Display();
    return 0;
}

關鍵點總結

  1. virtual public Botany:虛繼承確保只有一份 Botany 子對象。
  2. 最派生類 MicheliaAlba **必須**在初始化列表顯式調用虛基類構造函數 Botany(name)。
  3. 拷貝構造同理需顯式初始化虛基類與兩條路徑的直接基類。
  4. 析構順序:派生 → 直接基類(逆聲明序) → 虛基類。
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved