uml学习之开篇 -- 重点在于组建和零件的关系

**今天写一些uml方面的内容,其实学习uml主要是想要提高自己在软件设计方面的思想,以求能够在编程道路上获得一些实质性进展。

#include <iostream>
#include <vector>
#include <string>
using namespace std;

class Catalog
{
public:
    Catalog (double cst) :  m_cost(cst) {}
    double GetCost () { return m_cost; }
private:
    double m_cost;
};

class Component
{
public:
    virtual double Cost() { return 0.0;}
};

class Part : public Component
{
public:
    Part(Catalog cg) : m_cata (cg) {}
    double Cost() { return m_cata.GetCost(); }
private:
    Catalog m_cata;
};

class Assembly : public Component
{
public:
    void AddComponent(Component* cpn)
    {
        m_Comps.push_back(cpn);
    }
    double Cost () 
    {
        double ret = 0.0;
        vector<Component*>::iterator cter = m_Comps.begin();
        while(cter != m_Comps.end())
        {
            ret += (*cter)->Cost();
            ++cter;
        }
        return ret;
    }
private:
    vector<Component*> m_Comps;
};

int main(void)
{
    Catalog a(3.4);
    Catalog b(4.1);
    Catalog c(5.7);
    Component *cp1 = new Part(a);
    Component *cp2 = new Part(b);
    Component *cp3 = new Part(c);
    // 构造一个小组件
    Assembly SmallAss;
    SmallAss.AddComponent(cp1);
    SmallAss.AddComponent(cp2);
    SmallAss.AddComponent(cp3);
    // 构造一个大组建,包含小组件和几个零件
    Assembly BigAss;
    Component *Smlas = &SmallAss;

    BigAss.AddComponent(cp1);
    BigAss.AddComponent(cp2);
    BigAss.AddComponent(Smlas);
    cout << BigAss.Cost() << endl;
    return 0;
}

这段代码设计了一个类Component(构件)作为基类,类Part(零件)和类Assembly (组件)作为子类,在Assembly 中有一个私有数据vector

class Assembly : public Component
{
public:
    void AddComponent(Component& cpn)
    {
        m_Comps.push_back(cpn);
    }
    double Cost () 
    {
        double ret = 0.0;
        vector<Component*>::iterator cter = m_Comps.begin();
        while(cter != m_Comps.end())
        {
            ret += (*cter)->Cost();
            ++cter;
        }
        return ret;
    }
private:
    vector<Component> m_Comps;
};

上面的代码试图将Component的引用push_back进m_Comps,结果发现运行后打印的值是0.0,跟了一下代码发现a1添加的两个元素调用的Cost函数都是父类里面的。感觉用引用来实现多态把引用push_back进vector后,再调用并没有产生多态的效果。

对于为什么引用不能产生多态的效果,做了如下简单的测试:

class Base
{
public:
    virtual void Print() { cout << "Base::Print()" << endl; }
};

class Derived : public Base
{
public:
    void AddItem( Base &item)
    {
        m_b.push_back(item);
    }
    void Print() 
    {
        cout << "Derived::Print()" << endl; 
        vector<Base>::iterator bter = m_b.begin();
        while(bter != m_b.end())
        {
            (*bter).Print();
            ++bter;
        }
    }
private:
    vector<Base> m_b;
};

int main(void)
{
    Derived a1;
    Derived a2;
    Base &b1 = a1;
    Base &b2 = a2;
    Derived a3;
    a3.AddItem(b1);
    a3.AddItem(b2);
    a3.Print();

    return 0;
}

写了一个Base类和Derived类验证,发现在a3调用Print函数遍历m_b中每个元素调用(*bter).Print();时,并没有表现出多态的性质。我想原因可能是定义的vector m_b, 并没有真正的push_back进去引用,而只是将b1,b2当作两个父类对象压进去的。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值