1.概念解析
2.示例代码
#include<string>
#include<iostream>
using namespace std;
class Person
{
public:
Person(){ }
Person(string strName)
{
_strName = strName;
}
virtual void show()
{
cout<<"装扮的是:"<<_strName<<endl;
}
private:
string _strName;
};
class Finery:public Person
{
protected:
Person *_component;
public:
void Decorate(Person *component)
{
this->_component = component;
}
virtual void show()
{
_component->show();
}
};
class Tshirts:public Finery
{
public:
void show()
{
cout<<"T Shirts"<<endl;
_component->show();
}
};
class BigTrouser:public Finery
{
public:
void show()
{
cout<<"Big Trouser"<<endl;
_component->show();
}
};
int main()
{
Person *p = new Person("小李");
BigTrouser *bt = new BigTrouser();
Tshirts *ts = new Tshirts();
bt->Decorate(p);
ts->Decorate(bt);
ts->show();
return 0;
}
3.运行结果