#include<iostream>
#include <string>
#include <vector>
using namespace std;
enum eTStyle
{
T_STYLE1=0,
T_STYLE2,
T_STYLE3
};
class CDress
{
protected:
string t_scolor;
int t_nsize;
eTStyle t_estyle;
public:
CDress(string color, int size, eTStyle style)
{
t_scolor = color;
t_nsize = size;
t_estyle = style;
}
~CDress()
{}
virtual bool GetColor()
{
return true;
}
virtual bool GetSize()
{
return true;
}
virtual bool GetStyle()
{
return true;
}
void GetAll()
{
GetColor();
GetSize();
GetStyle();
}
};
class CTshirt:public CDress
{
public:
CTshirt(string color, int size, eTStyle style):CDress(color,size,style)
{
cout << "Initilize Shirt...." << endl;
}
bool GetColor()
{
cout << "Shirt Color is: " << t_scolor<<endl;
return true;
}
bool GetSize()
{
cout << "Shirt Size is: " << t_nsize<<endl;
return true;
}
bool GetStyle()
{
cout << "Shirt Style is: " << t_estyle<<endl;
return true;
}
};
class CPants:public CDress
{
public:
CPants(string color, int size, eTStyle style) :CDress(color, size, style)
{
cout << "Initilize Pants...." << endl;
}
bool GetColor()
{
cout << "Pants Color is: " << t_scolor<<endl;
return true;
}
bool GetSize()
{
cout << "Pants Size is: " << t_nsize<<endl;
return true;
}
bool GetStyle()
{
cout << "Pants Style is: " << t_estyle<<endl;
return true;
}
};
class CShoes:public CDress
{
public:
CShoes(string color, int size, eTStyle style) :CDress(color, size, style)
{
cout << "Initilize Shoes...." << endl;
}
bool GetColor()
{
cout << "Shoes Color is: " << t_scolor<<endl;
return true;
}
bool GetSize()
{
cout << "Shoes Size is: " << t_nsize<<endl;
return true;
}
bool GetStyle()
{
cout << "Shoes Style is: " << t_estyle<<endl;
return true;
}
};
class CSuits
{
private:
vector <CDress *> m_vpdress;
public:
CSuits(vector<CDress*> vpdress)
{
m_vpdress = vpdress;
}
void GetAll()
{
for (vector <CDress*>::iterator iter = m_vpdress.begin(); iter != m_vpdress.end(); iter++)
{
(*iter)->GetAll();
}
}
};
int main()
{
vector <CDress *> m_vpdress;
CTshirt *pshirt = new CTshirt("red", 165, T_STYLE1);
CPants *ppants = new CPants("white", 165, T_STYLE1);
CShoes *pshoes = new CShoes("black", 37, T_STYLE1);
m_vpdress.push_back(pshirt);
m_vpdress.push_back(ppants);
m_vpdress.push_back(pshoes);
CSuits *psuit = new CSuits(m_vpdress);
psuit->GetAll();
return 0;
}
Initilize Shirt....
Initilize Pants....
Initilize Shoes....
Shirt Color is: red
Shirt Size is: 165
Shirt Style is: 0
Pants Color is: white
Pants Size is: 165
Pants Style is: 0
Shoes Color is: black
Shoes Size is: 37
Shoes Style is: 0
请按任意键继续. . .
上一版中,衣服裤子和鞋子类都具有相同的元素和方法,重复内容非常多。这一版对这方面做改进。