ReplaceTypeCodeWithClass(以类取代类型码)

#ifndef REPLACETYPECODEWITHCLASS_H_
#define REPLACETYPECODEWITHCLASS_H_
#include <string>

const static int SIZE_OF_BLOOD_TYPE = 4;

enum BloodType
{
	O = 0,
	A,
	B,
	AB,
};

class BloodGroup
{
public:
	static std::string toString(BloodType type);
public:
	BloodGroup(BloodType type);
	std::string getBloodType();

private:
	BloodType type;
};

class Person
{
public:
	Person(std::string arg1, BloodGroup& arg2);
	std::string getBloodType();
	std::string getName();
private:
	std::string name;
	BloodGroup bloodGroup;
};







#endif /* REPLACETYPECODEWITHCLASS_H_ */

#include "ReplaceTypeCodeWithClass.h"
#include <iostream>
using namespace std;

Person::Person(string arg1, BloodGroup& arg2):name(arg1), bloodGroup(arg2)
{}

string Person::getBloodType()
{
	return bloodGroup.getBloodType();
}

string Person::getName()
{
	return name;
}

string BloodGroup::toString(BloodType type)
{
	static string typeOfString[SIZE_OF_BLOOD_TYPE] = {"O","A","B","AB"};

	return typeOfString[type];
}


BloodGroup::BloodGroup(BloodType type):type(type)
{}

string BloodGroup::getBloodType()
{
	return toString(type);
}

void ReplaceTypeCodeWithClass()
{
	BloodGroup o(O);
	BloodGroup ab(AB);

	Person p1("James",o);
	Person p2("Jane", ab);

	cout<<p1.getName()<<" BloodType is "<<p1.getBloodType()<<endl;
	cout<<p2.getName()<<" BloodType is "<<p2.getBloodType()<<endl;

}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值