标签:
ref:http://www.cnblogs.com/wanggary/archive/2011/04/19/2021638.html
直接上源码:
#include <iostream>
#include <string>
#include <list>
#include <string>
#include <sstream>
using namespace std;
class CCorpNode
{
public:
CCorpNode();
CCorpNode(string _name, string _pos, int _salary);
virtual ~CCorpNode(void);
virtual string GetInfo();
void SetParent(CCorpNode *_pParent);
CCorpNode * GetParent();
virtual bool IsLeaf() = 0;
private:
string m_name;
string m_position;
int m_salary;
protected:
bool m_isLeaf;
CCorpNode *m_pParent;
};
CCorpNode::CCorpNode(void)
{
m_name = "";
m_position = "";
m_salary = 0;
}
CCorpNode::CCorpNode(string _name, string _pos, int _salary) : m_name(_name), m_position(_pos), m_salary(_salary)
{
}
CCorpNode::~CCorpNode(void)
{
}
string CCorpNode::GetInfo()
{
stringstream temp;
temp << this->m_salary;
string info = "";
info.append("姓名:");
info.append(this->m_name);
info.append("\t职位:");
info.append(this->m_position);
info.append("\t薪水:");
info.append(temp.str());
return info;
}
void CCorpNode::SetParent( CCorpNode *_parent )
{
this->m_pParent = _parent;
}
CCorpNode * CCorpNode::GetParent()
{
return this->m_pParent;
}
class CBranchNode :
public CCorpNode
{
public:
CBranchNode(void);
CBranchNode(string name, string pos, int salary);
~CBranchNode(void);
void Add(CCorpNode *pcorpNode);
list<CCorpNode*> GetSubordinateInfo();
bool IsLeaf();
private:
list<CCorpNode*> m_subordinateList;
};
CBranchNode::CBranchNode(void)
{
m_isLeaf = false;
}
CBranchNode::CBranchNode( string name, string pos, int salary ) : CCorpNode(name, pos, salary)
{
m_isLeaf = false;
}
CBranchNode::~CBranchNode(void)
{
}
void CBranchNode::Add( CCorpNode *pcorpNode )
{
pcorpNode->SetParent(this);
m_subordinateList.push_back(pcorpNode);
}
list<CCorpNode*> CBranchNode::GetSubordinateInfo()
{
return this->m_subordinateList;
}
bool CBranchNode::IsLeaf()
{
return m_isLeaf;
}
class CLeafNode :
public CCorpNode
{
public:
CLeafNode(void);
CLeafNode(string name, string pos, int salary);
~CLeafNode(void);
bool IsLeaf();
};
CLeafNode::CLeafNode(void)
{
m_isLeaf = true;
}
CLeafNode::CLeafNode( string name, string pos, int salary ) : CCorpNode(name, pos, salary)
{
m_isLeaf = true;
}
CLeafNode::~CLeafNode(void)
{
}
bool CLeafNode::IsLeaf()
{
return m_isLeaf;
}
void DoNew()
{
CLeafNode *one = new CLeafNode("one", "ooo", 1);
CLeafNode *two = new CLeafNode("two", "ooo", 2);
CLeafNode *three = new CLeafNode("three", "ooo", 3);
CBranchNode *Branch = new CBranchNode("Branch", "ooo", 100);
Branch->Add(one);
Branch->Add(two);
Branch->Add(three);
list<CCorpNode *> temp = Branch->GetSubordinateInfo();
list<CCorpNode *>::iterator it;
for ( it = temp.begin(); it != temp.end(); it++ )
{
cout << (*it)->GetParent()->GetInfo() << endl;
cout << (*it)->GetInfo() << endl;
}
for ( it = temp.begin(); it != temp.end(); it++ )
{
free(*it);
(*it) = NULL;
}
free(Branch);
Branch = NULL;
}
int
main(int argc, char **argv)
{
DoNew();
getchar();
return (0);
}
标签:
原文地址:http://www.cnblogs.com/kaishan1990/p/4979822.html