首页 > 应用开发 > IT综合 > 例程实作----庖丁解羊(下) (转)
例程实作XML:namespace prefix = o ns = "urn:schemas-microsoft-com:Office:office" />
----庖丁解羊(下)
#include
#include
using namespace std;
class Part{
public:
virtual void Draw()=0;
virtual ~Part(){}
};
class Shape{
public:
Shape( string const& _s ):data( _s ){}
void Draw( unsigned color )
{
cout << data << " with color :" << hex << color << endl;
} //提供了借口,但是没有实现绘图
private:
string data;
//读者可以自己实现绘图部分
};
//绒毛,骨架,胸,腹,背,眼睛,鼻子,嘴巴,心,脾,肝,肠,肾,脚,尾巴,耳朵,肺,胃{{部分,显示=轮廓+颜色},轮廓[1],颜色[1]}
class BasePart : public Part{
public:
BasePart( string const& _s , unsigned _c ):shape( _s ),color( _c ){}
void Draw()
{
shape.Draw( color );
}
private:
Shape shape;
unsigned color;
};
//绒毛
class Floss : public BasePart{
public:
Floss( ):BasePart("Floss " ,0){}
};
//骨架
class Skelecton : public BasePart{
public:
Skelecton( ):BasePart("Skelecton " ,0){}
};
//胸
class Bosom : public BasePart{
public:
Bosom( ):BasePart("Bosom " ,0){}
};
//腹
class Abdomen : public BasePart{
public:
Abdomen ( ):BasePart("Abdomen " ,0){}
};
//背
class Backside: public BasePart{
public:
Backside ( ):BasePart("Backside " ,0){}
};
//眼睛
class Eye : public BasePart{
public:
Eye ( ):BasePart("Eye " ,0){}
};
//耳朵
class Ear : public BasePart{
public:
Ear ( ):BasePart("Ear " ,0){}
};
//鼻子
class Nose : public BasePart{
public:
Nose ( ):BasePart("Nose " ,0){}
};
//嘴巴
class Mouth : public BasePart{
public:
Mouth ( ):BasePart("Mouth " ,0){}
};
//尾巴
class Stern : public BasePart{
public:
Stern ( ):BasePart("Stern " ,0){}
};
//胡子
class Goatee : public BasePart{
public:
Goatee ( ):BasePart("Goatee " ,0){}
};
//心
class Heart : public BasePart{
public:
Heart ( ):BasePart("Heart " ,0){}
};
//脾
class Spleen : public BasePart{
public:
Spleen ( ):BasePart("Spleen " ,0){}
};
//肝
class Liver : public BasePart{
public:
Liver ( ):BasePart("Liver " ,0){}
};
//肺
class Lung : public BasePart{
public:
Lung ( ):BasePart("Lung " ,0){}
};
//胃
class Stomach : public BasePart{
public:
Stomach ( ):BasePart("Stomach " ,0){}
};
//肠
class Intestine : public BasePart{
public:
Intestine ( ):BasePart("Intestine " ,0){}
};
//肾
class Kidney : public BasePart{
public:
Kidney ( ):BasePart("Kidney " ,0){}
};
//脚
class Leg : public BasePart{
public:
Leg ( ):BasePart("Leg " ,0){}
};
//肢体{{部分, 显示=显示肢体},脚[1],绒毛[n],骨架[1]}
class Limb : public Part{
public:
void Draw()
{
cout << "Has a ";
leg.Draw();
cout << "Has a ";
skelecton.Draw();
cout << "Has some ";
floss[0].Draw();
}
private:
Leg leg;
Floss floss[10];
Skelecton skelecton;
};
//前肢,后肢{{肢体}}
class Forelimb : public Limb{
public:
void Draw()
{
cout<< "Forelimb " << endl;
Limb::Draw();
}
};
class Backlimb : public Limb{
public:
void Draw()
{
cout<< "Backlimb " << endl;
Limb::Draw();
}
};
//四肢{{部分,显示=显示四肢},前肢[2],后肢[2]}
class ExteRmity : public Part{
public:
void Draw()
{
cout<< "Has a Left ";
Leftforelimb.Draw();
cout<< "Has a Right ";
Rightforelimb.Draw();
cout<< "Has a Left ";
Leftbacklimb.Draw();
cout<< "Has a Right ";
Rightbacklimb.Draw();
}
private:
Forelimb Leftforelimb,Rightforelimb;
Backlimb Leftbacklimb,Rightbacklimb;
};
//内脏{{部分,显示=显示内脏},心[1],脾[1],肝[1],肺[2],胃[1],肠[n],肾[2]}
class Harslet: public Part{
public:
void Draw()
{
cout << "Harslet " < cout << "Has a "; heart.Draw(); cout << "Has a "; spleen.Draw(); cout << "Has a "; liver.Draw(); cout << "Has a Left "; Leftlung.Draw(); cout << "Has a Right "; Rightlung.Draw(); cout << "Has a "; stomach.Draw(); cout << "Has Some "; intestine[0].Draw(); cout << "Has a Left "; Leftkidney.Draw(); cout << "Has a Right "; Rightkidney.Draw(); } private: Heart heart; Spleen spleen; Liver liver; Lung Leftlung , Rightlung; Stomach stomach; Intestine intestine[10]; Kidney Leftkidney,Rightkidney; }; //五官{{部分,显示=显示五官},耳朵[2],眼睛[2],鼻子[1],嘴巴[1]} class Face : public Part{ public: void Draw() { cout << "Face " < cout << "Has a Left "; Leftear.Draw(); cout << "Has a Right "; Rightear.Draw(); cout << "Has a Left "; Lefteye.Draw(); cout << "Has a Right "; Righteye.Draw(); cout << "Has a "; nose.Draw(); cout << "Has a "; mouth.Draw(); } private: Ear Leftear,Rightear; Eye Lefteye,Righteye; Nose nose; Mouth mouth; }; //尾部{{部分,显示=显示尾部},尾巴[1],绒毛[n],骨架[1]} class Tail: public Part{ public: void Draw() { cout << "Tail " < cout << "Has a "; stern.Draw(); cout << "Has a "; skelecton.Draw(); cout << "Has some "; floss[0].Draw(); } private: Stern stern; Floss floss[10]; Skelecton skelecton; }; //躯体{{部分,显示=显示躯体},胸[1],腹[1],背[1],内脏[1],绒毛[n],骨架[1]} class Body: public Part{ public: void Draw() { cout << "Body " < cout << "Has a "; bosom.Draw(); cout << "Has a "; abdomen.Draw(); cout << "Has a "; backside.Draw(); cout << "Has a "; harslet.Draw(); cout << "Has a "; skelecton.Draw(); cout << "Has some "; floss[0].Draw(); } private: Bosom bosom; Abdomen abdomen; Backside backside; Harslet harslet; Floss floss[10]; Skelecton skelecton; }; //脑袋{{部分,显示=显示脑袋},五官[1],胡子[n],绒毛[n],骨架[1]} class Head: public Part{ public: void Draw() { cout <<"Head " << endl; cout << "Has a "; face.Draw(); cout << "Has a "; skelecton.Draw(); cout << "Has some "; goatee[0].Draw(); } private: Face face; Goatee goatee[10]; Skelecton skelecton; }; //山羊{{部分,显示=显示山羊},脑袋[1],躯体[1],四肢[1],尾部[1]} class Sheep: public Part{ public: void Draw() { cout << "Sheep " << endl; cout << "Has a "; head.Draw(); cout << "Has a "; body.Draw(); cout << "Has a "; extermity.Draw(); cout << "Has a "; tail.Draw(); } private: Head head; Body body; Extermity extermity; Tail tail; }; void main(void) { Sheep sheep; sheep.Draw(); cin.get(); } 结果是: Sheep Has a Head Has a Face Has a Left Ear with color :0 Has a Right Ear with color :0 Has a Left Eye with color :0 Has a Right Eye with color :0 Has a Nose with color :0 Has a Mouth with color :0 Has a Skelecton with color :0 Has some Goatee with color :0 Has a Body Has a Bosom with color :0 Has a Abdomen with color :0 Has a Backside with color :0 Has a Harslet Has a Heart with color :0 Has a Spleen with color :0 Has a Liver with color :0 Has a Left Lung with color :0 Has a Right Lung with color :0 Has a Stomach with color :0 Has Some Intestine with color :0 Has a Left Kidney with color :0 Has a Right Kidney with color :0 Has a Skelecton with color :0 Has some Floss with color :0 Has a Has a Left Forelimb Has a Leg with color :0 Has a Skelecton with color :0 Has some Floss with color :0 Has a Right Forelimb Has a Leg with color :0 Has a Skelecton with color :0 Has some Floss with color :0 Has a Left Backlimb Has a Leg with color :0 Has a Skelecton with color :0 Has some Floss with color :0 Has a Right Backlimb Has a Leg with color :0 Has a Skelecton with color :0 Has some Floss with color :0 Has a Tail Has a Stern with color :0 Has a Skelecton with color :0 Has some Floss with color :0 邪门的是,这个程序我从头到尾打完以后,第一遍运行,居然没有错误,连误输入的错误都没有,这是不是说,我的指法又上层楼呢^o^。 虽然我提供了源程序,但是我希望还是认真的输入一遍,一来加深印象,二来可以练习指法。程序员的指法也是生产力啊。 不过这个程序只是演示性的,使用了固定的数组和变量,事实上弹性还远远不够,有一些属性的数目是可变的,比如羊毛是会掉的,瘸了一条腿的羊还是羊,所以重视弹性的话,这些变量都要改为指针可以动态的增减。 好了,说了半天相信读者也累了,还是让大家自己体会吧。 2001/9/27 丁宁
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/10748419/viewspace-979378/ ,如需转载,请注明出处,否则将追究法律责任。