分享
 
 
 

例程实作----庖丁解羊(下)

王朝other·作者佚名  2006-01-08
窄屏简体版  字體: |||超大  

例程实作

----庖丁解羊(下)

#include <iostream>

#include <string>

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 " <<endl;

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 " <<endl;

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 " <<endl;

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 " <<endl;

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

丁宁

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有