肉店

代码1:

#include <iostream>

using namespace std;

enum Rou_type{ZHUROU,NIUROU,GOUROU};

class CRou
{
public:
Rou_type rou_tyle; // 标记 这个肉是什么肉
public:
virtual void Show()=0;
};

class CNiu : public CRou
{
public:
CNiu()
{
rou_tyle = NIUROU;
}
virtual void Show()
{
cout << "牛肉 ";
}
};

class CZhu : public CRou
{
public:
CZhu()
{
rou_tyle = ZHUROU;
}
virtual void Show()
{
cout << "猪肉 ";
}
};
class CGou : public CRou
{
public:
CGou()
{
rou_tyle = GOUROU;
}
virtual void Show()
{
cout << "狗肉 ";
}
};
class CKouRou : public CZhu
{
public:
CKouRou()
{
rou_tyle = ZHUROU;
}
virtual void Show()
{
cout << "扣肉 ";
}
};
class CLaGouRou : public CGou
{
public:
CLaGouRou()
{
rou_tyle = GOUROU;
}
virtual void Show()
{
cout << "腊肉 ";
}
};

class CRen 
{
public:

virtual void Show()=0;
virtual void CanEatRou(CRou* rou)=0;
void EatRou(CRou* rou)
{
// 显示一下人
this->Show();
// 显示一下肉
rou->Show();
// 看能吃不能吃
this->CanEatRou(rou);
}

};

class CHan : public CRen
{
public:
virtual void Show()
{
cout << "汉族人 ";
}
virtual void CanEatRou(CRou* rou)
{
cout << "猛吃" << endl;
}
};

class CMan : public CRen
{
public:
virtual void Show()
{
cout << "满族人 ";
}
virtual void CanEatRou(CRou* rou)
{
if (rou->rou_tyle == GOUROU)
{
cout << "不吃" << endl;
}
else
{
cout << "猛吃" << endl;
}
}
};

class CHui : public CRen
{
public:
virtual void Show()
{
cout << "回族人 ";
}
virtual void CanEatRou(CRou* rou)
{
if (rou->rou_tyle == GOUROU || rou->rou_tyle == ZHUROU)
{
cout << "不吃" << endl;
}
else
{
cout << "猛吃" << endl;
}
}
};

int main()
{

CRen* han = new CHan;
CRen* hui = new CHui;
CRen* man = new CMan;

CRou* zhu = new CZhu;
CRou* niu = new CNiu;
CRou* gou = new CGou;
CRou* kou = new CKouRou;
CRou* lagou = new CLaGouRou;

han->EatRou(zhu);
han->EatRou(niu);
han->EatRou(gou);
han->EatRou(kou);
han->EatRou(lagou);
cout << "-------------" << endl;

hui->EatRou(zhu);
hui->EatRou(niu);
hui->EatRou(gou);
hui->EatRou(kou);
hui->EatRou(lagou);
cout << "-------------" << endl;

man->EatRou(zhu);
man->EatRou(niu);
man->EatRou(gou);
man->EatRou(kou);
man->EatRou(lagou);
cout << "-------------" << endl;

system("pause");
return 0;
}

  

  

代码2:动态识别类型

RuntimeClass.h

#pragma once

struct RuntimeClass
{
char m_ClassName[20]; // 存自己的类名
RuntimeClass* pFather; // 指向父类节点的指针
};

 

MFC.h

#pragma once
#include "RuntimeClass.h"

#define RUNTIME_CLASS(ThisClass) &ThisClass::runtime

#define DECLAER_DYNAMIC()\
static RuntimeClass runtime;\
virtual RuntimeClass* GetRuntimeClass();

#define IMPLEMENT_DYNAMIC(ThisClass,BaseClass)\
RuntimeClass* ThisClass::GetRuntimeClass()\
{\
return &runtime;\
}\
RuntimeClass ThisClass::runtime={#ThisClass,&BaseClass::runtime};

class CRou
{
public:
DECLAER_DYNAMIC()
virtual void Show()=0;
bool IsKindOf(RuntimeClass* run);
};

class CGou : public CRou
{
public:
DECLAER_DYNAMIC()
virtual void Show();
};

class CNiu : public CRou
{
public:
DECLAER_DYNAMIC()
virtual void Show();
};

class CZhu : public CRou
{
public:
DECLAER_DYNAMIC()
virtual void Show();
};

class CKou :public CZhu
{
public:
DECLAER_DYNAMIC()
virtual void Show();
};

class CLaGou : public CGou
{
public:
DECLAER_DYNAMIC()
virtual void Show();
};

class CHuLaGouRou : public CLaGou
{
public:
DECLAER_DYNAMIC()
virtual void Show();
};

 MFC.cpp

#include <iostream>
#include "MFC.h"

using namespace std;

bool CRou::IsKindOf(RuntimeClass* run)
{

RuntimeClass* temp = this->GetRuntimeClass(); // 得到 new出来这个类里的节点
// 从这个节点开始找 找这个链表里有没有run
while(temp)
{
if (temp == run) // 判断地址 是否相等
{
return true;
}
else
{
temp = temp->pFather;
}
}
return false;
}
RuntimeClass* CRou::GetRuntimeClass()
{
return &runtime;
}
RuntimeClass CRou::runtime={"CRou",NULL}; // 结构体的初始化

void CZhu::Show()
{
cout << "猪肉 ";
}
IMPLEMENT_DYNAMIC(CZhu,CRou)

void CNiu::Show()
{
cout << "牛肉 ";
}
IMPLEMENT_DYNAMIC(CNiu,CRou)

void CGou::Show()
{
cout << "狗肉 ";
}
IMPLEMENT_DYNAMIC(CGou,CRou)

void CLaGou::Show()
{
cout << "腊狗 ";
}
IMPLEMENT_DYNAMIC(CLaGou,CGou)

void CKou::Show()
{
cout << "扣肉 ";
}
IMPLEMENT_DYNAMIC(CKou,CZhu)

void CHuLaGouRou::Show()
{
cout << "湖肉 ";
}
IMPLEMENT_DYNAMIC(CHuLaGouRou,CLaGou)

main.cpp

#include<iostream>
#include "MFC.h"

using namespace std;

class CRen 
{
public:

virtual void Show()=0;
virtual void CanEatRou(CRou* rou)=0;
void EatRou(CRou* rou)
{
// 显示一下人
this->Show();
// 显示一下肉
rou->Show();
// 看能吃不能吃
this->CanEatRou(rou);
}

};

class CHan : public CRen
{
public:
virtual void Show()
{
cout << "汉族人 ";
}
virtual void CanEatRou(CRou* rou)
{
cout << "猛吃" << endl;
}
};

class CMan : public CRen
{
public:
virtual void Show()
{
cout << "满族人 ";
}
virtual void CanEatRou(CRou* rou)
{	

if (rou->IsKindOf(RUNTIME_CLASS(CGou)))
{
cout << "不吃" << endl;
}
else
{
cout << "猛吃" << endl;
}
}
};

class CHui : public CRen
{
public:
virtual void Show()
{
cout << "回族人 ";
}
virtual void CanEatRou(CRou* rou)
{
if (rou->IsKindOf(RUNTIME_CLASS(CZhu)) || rou->IsKindOf(RUNTIME_CLASS(CGou)) )
{
cout << "不吃" << endl;
}
else
{
cout << "猛吃" << endl;
}
}
};

class CHuNanRen : public CRen
{
public:
virtual void Show()
{
cout << "湖南人 ";
}
virtual void CanEatRou(CRou* rou)
{
if (rou->IsKindOf(RUNTIME_CLASS(CHuLaGouRou)))
{
cout << "猛吃" << endl;
}
else
{
cout << "不吃" << endl;	
}
}
};

int main()
{

CRen* han = new CHan;
CRen* hui = new CHui;
CRen* man = new CMan;
CRen* hunan = new CHuNanRen;

CRou* zhu = new CZhu;
CRou* niu = new CNiu;
CRou* gou = new CGou;
CRou* kou = new CKou;
CRou* lagou = new CLaGou;
CRou* hularou = new CHuLaGouRou;

han->EatRou(zhu);
han->EatRou(niu);
han->EatRou(gou);
han->EatRou(kou);
han->EatRou(lagou);
han->EatRou(hularou);
cout << "-------------" << endl;

hui->EatRou(zhu);
hui->EatRou(niu);
hui->EatRou(gou);
hui->EatRou(kou);
hui->EatRou(lagou);
hui->EatRou(hularou);
cout << "-------------" << endl;

man->EatRou(zhu);
man->EatRou(niu);
man->EatRou(gou);
man->EatRou(kou);
man->EatRou(lagou);
man->EatRou(hularou);
cout << "-------------" << endl;

hunan->EatRou(zhu);
hunan->EatRou(niu);
hunan->EatRou(gou);
hunan->EatRou(kou);
hunan->EatRou(lagou);
hunan->EatRou(hularou);
cout << "-------------" << endl;

system("pause");
return 0;
}

  

  

 

代码3:动态创建对象

#include <iostream>
using namespace std;

class CMeat;

struct Node
{
char ClassName[20];
Node* Father;
Node* pNext;
static Node* pHead;
CMeat* (*p)();
static CMeat* loadclass(char* name)
{
Node* Temp = Node::pHead;
while(Temp)
{
if(strcmp(Temp->ClassName,name)==0)
{
return (*(Temp->p))();
}
Temp = Temp->pNext;
}
}
};
Node* Node::pHead = NULL;

struct AFX_CLASS
{
AFX_CLASS(Node* Temp)
{
Temp->pNext = Node::pHead;
Node::pHead = Temp;
}
};

class CMeat
{
public:
static Node node;

virtual void show() = 0;
virtual Node* Get() = 0;
bool IsKindOf(Node* p)
{
Node* temp;
temp=this->Get();
while (temp)
{
if (temp==p)
{
return true;
}
else
{
temp=temp->pNext;
}
}
return false;
}
};
Node CMeat::node = {"CMeat",NULL,NULL,NULL};
AFX_CLASS afx_class(&(CMeat::node));

class CPig : public CMeat
{
public:
static Node node;
Node* Get()
{
return &node;
}
static CMeat* GetClass()
{
return new CPig;
}
virtual void show()
{
cout<<"猪肉 ";
}
};
Node CPig::node = {"CPig",&(CMeat::node),NULL,GetClass};
AFX_CLASS afx_class1(&(CPig::node));

class CKou : public CPig
{
public:
static Node node;
Node* Get()
{
return &node;
}
static CMeat* GetClass()
{
return new CKou;
}
virtual void show()
{
cout<<"扣肉 ";
}
};
Node CKou::node = {"CKou",&(CPig::node),NULL,GetClass};
AFX_CLASS afx_class2(&(CKou::node));

class CCow : public CMeat
{
public:
static Node node;
Node* Get()
{
return &node;
}
static CMeat* GetClass()
{
return new CCow;
}
virtual void show()
{
cout<<"牛肉 ";
}
};
Node CCow::node = {"CCow",&(CMeat::node),NULL,GetClass};
AFX_CLASS afx_class3(&(CCow::node));

class CDog : public CMeat
{
public:
static Node node;
Node* Get()
{
return &node;
}
static CMeat* GetClass()
{
return new CDog;
}
virtual void show()
{
cout<<"狗肉 ";
}
};
Node CDog::node = {"CDog",&(CMeat::node),NULL,GetClass};
AFX_CLASS afx_class4(&(CDog::node));

class CLaDog : public CDog
{
public:
static Node node;
Node* Get()
{
return &node;
}
static CMeat* GetClass()
{
return new CLaDog;
}
virtual void show()
{
cout<<"腊狗肉 ";
}
};
Node CLaDog::node = {"CLaDog",&(CDog::node),NULL,GetClass};
AFX_CLASS afx_class5(&(CLaDog::node));

class CPeople
{
public:
virtual void show() = 0;
virtual void CanEat(CMeat* meat) = 0;
void Eat(CMeat* meat)
{
//显示人
this->show();
//显示肉
meat->show();
//能不能吃
this->CanEat(meat);
}
};

class CHan : public CPeople
{
public:
virtual void show()
{
cout<<"汉族人 ";
}
virtual void CanEat(CMeat* meat)
{
cout<<"吃"<<endl;
}
};

class CHui : public CPeople
{
public:
virtual void show()
{
cout<<"回族人 ";
}
virtual void CanEat(CMeat* meat)
{
if (meat->IsKindOf(&CPig::node) || meat->IsKindOf(&CCow::node))
{
cout<<"不吃"<<endl;
}
else
{
cout<<"吃"<<endl;
}
}
};

class CMan : public CPeople
{
public:
virtual void show()
{
cout<<"满族人 ";
}
virtual void CanEat(CMeat* meat)
{
if (meat->IsKindOf(&CDog::node))
{
cout<<"不吃"<<endl;
}
else
{
cout<<"吃"<<endl;
}
}
};

int main()
{
CPeople* han = new CHan;
CMeat* meat = Node::loadclass("CDog");
han->Eat(meat);

CPeople* man = new CMan;
CMeat* meat1 = Node::loadclass("CCow");
man->Eat(meat1);

system("pause");
return 0;
}

  

  

posted @ 2013-01-24 15:05  I want you  阅读(305)  评论(0)    收藏  举报