您的位置:首页 > 其它

关于类之间的五种关系 UML

2012-11-11 20:21 218 查看
如图关于如何实现上图图中的几种关系,最近我写的一个小程序,希望大家多多指点:



/************************************************************************/
/*课程科目:软件工程作者邮箱:juste0830@126.com*/
/*开发环境:VC6.0编写日期:2012年11月10日晨*/
/***********************************************************************/
#include<iostream.h>

/************定义水类*****************/
classwater
{
//关于水的属性和操作
public:
charprovide_need2;//水提供生命存的一个需求之一
charprovide_need1;//水提供生命存的一个需求之一
};
/************定义翅膀类*****************/
classwing
{
public:
friendclassbird;
private:
wing()
{
cout<<"有翅膀-";
}
};
/************定义气候类*****************/
classclimate
{
public:
voidshow()
{
cout<<"气候适宜,";
}
};

/************定义动物类*****************/
classanimal
{
private:
charhave_live;//有生命
charneed1;
charneed2;
public:

animal(constwater&w)
{
need1=w.provide_need1;
need2=w.provide_need2;
}
voidmultiply()//繁殖
{
cout<<"-能繁殖"<<endl;
}
};

/************定义鸟类*****************/
classbird:publicanimal//继承关系
{
public:
wingtheWing;
bird(water&w):animal(w)
{
}

voidlay_egg()
{
cout<<"下蛋"<<endl;
}

private:
charhave_feather;//有羽毛
charhave_no_tooth;//没有牙齿
};
/************定义大雁类*****************/
classgoose:publicbird
{
public:
goose(water&w):bird(w)
{
}

};
/************定义雁群类*****************/
classgeese
{
public:
gooseg1; //雁群有大雁组成
gooseg2;

};

/************定义企鹅类*****************/
classpenguin:publicbird
{
public:
penguin(water&w):bird(w)
{
}
voidLive(climateCL)
{
CL.show();
cout<<"企鹅生存下来"<<endl;
}
};

intmain()
{

watertheWater;
//animaltheAnimal;//这里会出现错误,因为没有水,动物就不能生存------------------依赖关系
animaltheAnimal(theWater);//给与了水,动物才能存在(被创建)
cout<<"这是个鸟:";
birdbird1(theWater);//鸟继承与动物,因此鸟也依赖水,没水不能有鸟的创建
bird1.multiply();//在bird中没有定义有生命,但是它继承了父类animal--------------继承关系

penguinthePenguin(theWater);
climatetheClimate;
//thePenguin.Live();//没有合适的气候,企鹅就不能生存
thePenguin.Live(theClimate);//存在依赖,继承,还有关联-----------------------------关联关系

//wingtheWing; //翅膀不能被单独定义,只能在鸟存在时,翅膀才能存在-----------------组合关系
cout<<"这是大雁:";
//如果不定义大雁类,雁群类将无法定义。也就是说,如果不存在大雁,就不存在雁群
goosetheGoose(theWater);//但是大雁可以单独存在----------------------------------聚合关系
cout<<"\n不存在大雁,就不存在雁群"<<endl;
cout<<"不存在雁群,去可以存在大雁"<<endl;
cout<<endl;
return0;

}


                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: