搭配衣服(基于上一版的改进)

#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
请按任意键继续. . .


上一版中,衣服裤子和鞋子类都具有相同的元素和方法,重复内容非常多。这一版对这方面做改进。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值