Design mode C + + (composite combined mode)

Source: Internet
Author: User

Ref:http://www.cnblogs.com/wanggary/archive/2011/04/19/2021638.html

Directly on the source:

#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 ("Name:");
Info.append (This->m_name);
Info.append ("T-position:");
Info.append (this->m_position);
Info.append ("T-Salary:");
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 ("N", "Ooo", 2);
Cleafnode *three = new Cleafnode ("Three", "Ooo", 3);

Cbranchnode *branch = new Cbranchnode ("Branch", "ooo", 100);
Branch->add (one);
Branch->add (both);
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);
}

Design mode C + + (composite combined mode)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.