快捷搜索:

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

#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 Earwith color :0

Has a Right Earwith color :0

Has a Left Eyewith color :0

Has a Right Eyewith color :0

Has a Nosewith color :0

Has a Mouthwith color :0

Has a Skelectonwith color :0

Has some Goateewith color :0

Has a Body

Has a Bosomwith color :0

Has a Abdomenwith color :0

Has a Backsidewith color :0

Has a Harslet

Has a Heartwith color :0

Has a Spleenwith color :0

Has a Liverwith color :0

Has a Left Lungwith color :0

Has a Right Lungwith color :0

Has a Stomachwith color :0

Has Some Intestinewith color :0

Has a Left Kidneywith color :0

Has a Right Kidneywith color :0

Has a Skelectonwith color :0

Has some Flosswith color :0

Has a Has a Left Forelimb

Has a Legwith color :0

Has a Skelectonwith color :0

Has some Flosswith color :0

Has a Right Forelimb

Has a Legwith color :0

Has a Skelectonwith color :0

Has some Flosswith color :0

Has a Left Backlimb

Has a Legwith color :0

Has a Skelectonwith color :0

Has some Flosswith color :0

Has a Right Backlimb

Has a Legwith color :0

Has a Skelectonwith color :0

Has some Flosswith color :0

Has a Tail

Has a Sternwith color :0

Has a Skelectonwith color :0

Has some Flosswith color :0

邪门的是,这个法度榜样我从头到尾打完今后,第一遍运行,居然没有差错,连误输入的差错都没有,这是不是说,我的指法又上层楼呢^o^。

虽然我供给了源法度榜样,然则我盼望照样卖力的输入一遍,一来加深印象,二来可以演习指法。法度榜样员的指法也是临盆力啊。

不过这个法度榜样只是演示性的,应用了固定的数组和变量,事实上弹性还远远不敷,有一些属性的数目是可变的,比如羊毛是会掉落的,瘸了一条腿的羊照样羊,以是注重弹性的话,这些变量都要改为指针可以动态的增减。

您可能还会对下面的文章感兴趣: