欢迎来到天天文库
浏览记录
ID:37918999
大小:92.00 KB
页数:11页
时间:2019-06-02
《C++第5次作业》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、实验五:数据的共享与保护1.定义一个Cat类,拥有静态数据成员HowManyCats,记录Cat的个体数目;静态成员函数GetHowMany(),存取HowManyCats。设计程序测试这个类,体会静态数据成员和静态成员函数的用法。(1)实验分析:本题的难点就是静态数据成员和静态成员函数的定义和使用。(2)程序代码:#include#includeusingnamespacestd;classCat{public:Cat(stringname="Untitle",doubleage=0,dou
2、bleweight=0):name(name),age(age),weight(weight){HowManyCats++;}Cat(Cat&c):name(c.name),age(c.age),weight(c.weight){}staticintGetHowMany();voidshowCat();private:stringname;doubleage,weight;staticintHowManyCats;};intCat::HowManyCats=0;intCat::GetHowMany(){returnHowMany
3、Cats;}voidCat::showCat(){for(inti=0;i<30;i++)cout<<"*";cout<4、utputwords();cout<<"Nowweinputdataofonecat"<5、return0;}voidoutputwords(){cout<<"Nowthereare"<#includeusingnamespacestd;classPoint{public:Point(intX=0,in6、tY=0){x=X;y=Y;}friendfloatdist(Point&po1,Point&po2);private:intx,y;};floatdist(Point&po1,Point&po2){doublex=po1.x-po2.x;doubley=po1.y-po2.y;return(sqrt(x*x+y*y));}classline{public:line(Pointpx1,Pointpx2){po1=px1,po2=px2;}floatlength(){returndist(po1,po2);}private:Poi7、ntpo1,po2;};voidmain(){intx,y;cout<<"请输入第一点的坐标:";cin>>x>>y;Pointp1(x,y);cout<<"请输入第二点的坐标:";cin>>x>>y;Pointp2(x,y);lineAB(p1,p2);cout<<"Thedistanceis"<#include8、ector>usingnamespacestd;classStudent{public:intnAllNum;charcName[5];charcSex[2];intnScore;intnID;Student(){nAllNum=0;nScore=0;}prot
4、utputwords();cout<<"Nowweinputdataofonecat"<5、return0;}voidoutputwords(){cout<<"Nowthereare"<#includeusingnamespacestd;classPoint{public:Point(intX=0,in6、tY=0){x=X;y=Y;}friendfloatdist(Point&po1,Point&po2);private:intx,y;};floatdist(Point&po1,Point&po2){doublex=po1.x-po2.x;doubley=po1.y-po2.y;return(sqrt(x*x+y*y));}classline{public:line(Pointpx1,Pointpx2){po1=px1,po2=px2;}floatlength(){returndist(po1,po2);}private:Poi7、ntpo1,po2;};voidmain(){intx,y;cout<<"请输入第一点的坐标:";cin>>x>>y;Pointp1(x,y);cout<<"请输入第二点的坐标:";cin>>x>>y;Pointp2(x,y);lineAB(p1,p2);cout<<"Thedistanceis"<#include8、ector>usingnamespacestd;classStudent{public:intnAllNum;charcName[5];charcSex[2];intnScore;intnID;Student(){nAllNum=0;nScore=0;}prot
5、return0;}voidoutputwords(){cout<<"Nowthereare"<#includeusingnamespacestd;classPoint{public:Point(intX=0,in
6、tY=0){x=X;y=Y;}friendfloatdist(Point&po1,Point&po2);private:intx,y;};floatdist(Point&po1,Point&po2){doublex=po1.x-po2.x;doubley=po1.y-po2.y;return(sqrt(x*x+y*y));}classline{public:line(Pointpx1,Pointpx2){po1=px1,po2=px2;}floatlength(){returndist(po1,po2);}private:Poi
7、ntpo1,po2;};voidmain(){intx,y;cout<<"请输入第一点的坐标:";cin>>x>>y;Pointp1(x,y);cout<<"请输入第二点的坐标:";cin>>x>>y;Pointp2(x,y);lineAB(p1,p2);cout<<"Thedistanceis"<#include8、ector>usingnamespacestd;classStudent{public:intnAllNum;charcName[5];charcSex[2];intnScore;intnID;Student(){nAllNum=0;nScore=0;}prot
8、ector>usingnamespacestd;classStudent{public:intnAllNum;charcName[5];charcSex[2];intnScore;intnID;Student(){nAllNum=0;nScore=0;}prot
此文档下载收益归作者所有