C++第5次作业.docx
- 文档编号:3583104
- 上传时间:2022-11-24
- 格式:DOCX
- 页数:13
- 大小:57.74KB
C++第5次作业.docx
《C++第5次作业.docx》由会员分享,可在线阅读,更多相关《C++第5次作业.docx(13页珍藏版)》请在冰豆网上搜索。
C++第5次作业
实验五:
数据的共享与保护
1.定义一个Cat类,拥有静态数据成员HowManyCats,记录Cat的个体数目;静态成员函数GetHowMany(),存取HowManyCats。
设计程序测试这个类,体会静态数据成员和静态成员函数的用法。
(1)实验分析:
本题的难点就是静态数据成员和静态成员函数的定义和使用。
(2)程序代码:
#include
#include
usingnamespacestd;
classCat
{
public:
Cat(stringname="Untitle",doubleage=0,doubleweight=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()
{
returnHowManyCats;
}
voidCat:
:
showCat()
{
for(inti=0;i<30;i++)
cout<<"*";
cout< cout<<"Thenameofthiscatis: "< cout<<"Theageofthiscatis(years): "< cout<<"Theweightofthiscatis(kg): "< for(i=0;i<30;i++) cout<<"*"; cout< } voidoutputwords(); intmain() { outputwords(); cout<<"Nowweinputdataofonecat"< Catcat1; cat1.showCat(); outputwords(); cout<<"Nowweinputdataofonecat"< Catcat2("Jack",8.5,80.7); cat2.showCat(); outputwords(); cout<<"Nowweinputdataofonecat"< Catcat3("哈哈",-100,0.5); cat3.showCat(); outputwords(); return0; } voidoutputwords() { cout<<"Nowthereare"< : GetHowMany()<<"cats"< } (3)实验结果: 2.设计一个点(point)类, 1)在点类中设计友员函数,求两点间的距离。 2)设计一个线段(line)类。 (1)实验分析: 两点间的距离用普通函数dist来计算。 (2)程序代码: #include #include usingnamespacestd; classPoint { public: Point(intX=0,intY=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: Pointpo1,po2; }; voidmain() { intx,y; cout<<"请输入第一点的坐标: "; cin>>x>>y; Pointp1(x,y); cout<<"请输入第二点的坐标: "; cin>>x>>y; Pointp2(x,y); lineAB(p1,p2); cout<<"Thedistanceis"< } (3)实验结果: 3、定义一个学生类,数据成员有: 总人数、、性别、成绩等,设计程序要求能统计学生的总人数、修改成绩并打印成绩。 程序代码: #include #include usingnamespacestd; classStudent { public: intnAllNum; charcName[5]; charcSex[2]; intnScore; intnID; Student() { nAllNum=0; nScore=0; } protected: private: }; voidmain() { vector StudentTmpStu; intnFlag=1; do { cout<<"请输入学生学号: "< cin>>TmpStu.nID; cout<<"请输入学生: "< cin>>TmpStu.cName; cout<<"请输入学生性别: "< cin>>TmpStu.cSex; cout<<"请输入学生分数: "< cin>>TmpStu.nScore; Stu.push_back(TmpStu); cout<<"继续输入? (1继续,0停止)"< cin>>nFlag; }while(nFlag! =0); intnAllNum=Stu.size(); intChangeID; cout<<"请输入想修改成绩的学生学号: "< cin>>ChangeID; intnFlag1=0; for(inti=0;i { if(Stu[i].nID==ChangeID) { cout<<"输入修改后的分数。 "< cin>>Stu[i].nScore; nFlag1=1; break; } } if(nFlag1! =1) { cout<<"未找到该学生! "< } FILE*fp=fopen("Result.txt","w"); fprintf(fp,"总人数为: %d\n",nAllNum); for(i=0;i { fprintf(fp,"%d\t",Stu[i].nID); fprintf(fp,"%s\t",Stu[i].cName); fprintf(fp,"%s\t",Stu[i].cSex); fprintf(fp,"%d\n",Stu[i].nScore); } cout<<"统计结果在Result文件中! "< fclose(fp); } 实验结果: 4.设计一个类,要求类中至少有一个友员函数,然后在主程序中访问友员函数。 程序代码: #include #include classPoint { public: Point(inta,intb){x=a;y=b;} intx,y; }; classLine { inta,b,c; public: Line(intx,inty,intz){a=x;b=y;c=z;} frienddoublelength(Linel,Pointp) { doubled; d=abs((l.a*p.x*p.x+l.b*p.x+l.c)/sqrt(l.a*l.a+l.b*l.b)); returnd; } }; voidmain() { Pointp(9,9); Linel(5,3,-4); cout<<"长度为"< } 实验结果: 5.设计一个用于人事管理的People(人员)类。 要求具有如下属性: charname[11]、编号charnumber[7]、性别charsex[3]、生日birthday、号charid[16]。 其中“出生日期”定义为一个“日期”类嵌对象。 用成员函数实现对人员信息的录入和显示。 要求包括: 构造函数和析构函数,拷贝构造函数、联成员函数、聚集。 在测试程序中定义people类的对象数组,录入数据并显示。 程序代码: #include #include usingnamespacestd; classBIRTHDAY/*生日的BIRTHDAY类*/ { private: intY,M,D; public: BIRTHDAY(inty=1900,intm=1,intd=1)/*构造函数*/ { Y=y; M=m; D=d; } BIRTHDAY(BIRTHDAY&p)/*拷贝构造函数*/ { Y=p.Y; M=p.M; D=p.D; } voidSet(inty,intm,intd)/*生日设定*/ { Y=y; M=m; D=d; } voidShow()/*输出生日*/ { cout< } intGetY() { returnY; } intGetM() { returnM; } intGetD() { returnD; } }; classPEOPLE { private: charName[11]; charNumber[7]; charSex[3]; charId[20]; BIRTHDAYBirth;/*日期类的嵌对象*/ public: PEOPLE(charname[]="'\0'",charnumber[]="'\0'",charsex[]="'\0'",charid[]="'\0'")/*构造函数*/ { strcpy(Name,name); strcpy(Number,number); strcpy(Sex,sex); strcpy(Id,id); } inlinevoidSet()/*基本信息设定*/ { inty,m,d; cout<<"请依次输入: "< cout<<": "< cin>>Name; cout<<"编号: "< cin>>Number; cout<<"性别: "< cin>>Sex; cout<<"号: "< cin>>Id; cout<<"请输入生日: "< cin>>y>>m>>d; BIRTHDAYday1(y,m,d);/*初始化生日*/ Birth=day1; } voidShow()/*输出信息*/ { cout<<": "< cout<<"编号: "< cout<<"性别: "< cout<<"ID: "< cout< } ~PEOPLE() { cout<<"调用析构函数! "< } }; voidmain() { PEOPLEp[3]; inti; for(i=0;i<3;i++) { p[i].Set(); } for(i=0;i<3;i++) { p[i].Show(); } } 实验结果:
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- C+ 作业