欢迎来到天天文库
浏览记录
ID:9327587
大小:30.00 KB
页数:7页
时间:2018-04-28
《静态成员与友元实验报告》由会员上传分享,免费在线阅读,更多相关内容在学术论文-天天文库。
1、上海电力学院实验报告课程名称高级程序设计C++(2)实验项目实验三静态成员与友元姓名学号班级专业信息安全同组人姓名无指导教师姓名XX实验日期一、实验目的Ø熟练掌握静态成员和友元的基本概念Ø理解静态数据成员和静态成员函数的使用Ø理解友元和友元函数的作用和使用方法二、实验内容和步骤编程序并上机调试运行。1.设计一个类,包含一个静态成员和一个静态成员函数,观察多个该类的对象对静态成员变量改变的执行情况和静态成员函数的调用情况。l程序源代码(注意添加注释)#includeusingnamespacestd;clas
2、sA{public:staticinta;staticvoidf(){a+=1;}voidout(){cout<3、骤与记录、实验分析与结论等。#includeusingnamespacestd;classStudent{private:staticintcount;//静态数据成员,记录Student的个体数目public:Student(){count++;}staticintgetcount(){returncount;}~Student(){count--;}};intStudent::count=0;//初始化静态数据成员intmain(){cout<<"未定义Student类对象之前:count="<4、dent::getcount()<5、原理与内容、实验步骤与记录、实验分析与结论等。3.设计一个日期类Date,包括日期的年份、月份和日号,要求能够设置日期、按照年/月/日的格式输出日期、编写友元函数,求两个日期之间相差的天数。l程序源代码(注意添加注释)#includeusingnamespacestd;intwhatday(inty,intm,intd);classDate{intyear,month,day;public:voidmodify_year(inty){year=y;}voidmodify_month(intm){month=6、m;}voidmodify_day(intd){day=d;}Date(inty=1900,intm=1,intd=1):year(y),month(m),day(d){}voidout(){cout<<"日期为:"<=d2.year)returnabs((d1.year-d2.ye7、ar)*365+whatday(d1.year,d1.month,d1.day)-whatday(d2.year,d2.month,d2.day));elsereturnabs((d2.year-d1.year)*365+whatday(d2.year,d2.month,d2.day)-whatday(d1.year,d1.month,d1.day));}intwhatday(inty,intm,intd)//此函数计算m月d日是本年的第几天{intwhatd=d;switch(m){case12:whatd+=30;case8、11:whatd+=31;case10:whatd+=30;case9:whatd+=31;case8:whatd+=31;case7:whatd+=30;case6:whatd+=31;case5:whatd+=30;case4:whatd+=31;case3:if(y%4
3、骤与记录、实验分析与结论等。#includeusingnamespacestd;classStudent{private:staticintcount;//静态数据成员,记录Student的个体数目public:Student(){count++;}staticintgetcount(){returncount;}~Student(){count--;}};intStudent::count=0;//初始化静态数据成员intmain(){cout<<"未定义Student类对象之前:count="<4、dent::getcount()<5、原理与内容、实验步骤与记录、实验分析与结论等。3.设计一个日期类Date,包括日期的年份、月份和日号,要求能够设置日期、按照年/月/日的格式输出日期、编写友元函数,求两个日期之间相差的天数。l程序源代码(注意添加注释)#includeusingnamespacestd;intwhatday(inty,intm,intd);classDate{intyear,month,day;public:voidmodify_year(inty){year=y;}voidmodify_month(intm){month=6、m;}voidmodify_day(intd){day=d;}Date(inty=1900,intm=1,intd=1):year(y),month(m),day(d){}voidout(){cout<<"日期为:"<=d2.year)returnabs((d1.year-d2.ye7、ar)*365+whatday(d1.year,d1.month,d1.day)-whatday(d2.year,d2.month,d2.day));elsereturnabs((d2.year-d1.year)*365+whatday(d2.year,d2.month,d2.day)-whatday(d1.year,d1.month,d1.day));}intwhatday(inty,intm,intd)//此函数计算m月d日是本年的第几天{intwhatd=d;switch(m){case12:whatd+=30;case8、11:whatd+=31;case10:whatd+=30;case9:whatd+=31;case8:whatd+=31;case7:whatd+=30;case6:whatd+=31;case5:whatd+=30;case4:whatd+=31;case3:if(y%4
4、dent::getcount()<5、原理与内容、实验步骤与记录、实验分析与结论等。3.设计一个日期类Date,包括日期的年份、月份和日号,要求能够设置日期、按照年/月/日的格式输出日期、编写友元函数,求两个日期之间相差的天数。l程序源代码(注意添加注释)#includeusingnamespacestd;intwhatday(inty,intm,intd);classDate{intyear,month,day;public:voidmodify_year(inty){year=y;}voidmodify_month(intm){month=6、m;}voidmodify_day(intd){day=d;}Date(inty=1900,intm=1,intd=1):year(y),month(m),day(d){}voidout(){cout<<"日期为:"<=d2.year)returnabs((d1.year-d2.ye7、ar)*365+whatday(d1.year,d1.month,d1.day)-whatday(d2.year,d2.month,d2.day));elsereturnabs((d2.year-d1.year)*365+whatday(d2.year,d2.month,d2.day)-whatday(d1.year,d1.month,d1.day));}intwhatday(inty,intm,intd)//此函数计算m月d日是本年的第几天{intwhatd=d;switch(m){case12:whatd+=30;case8、11:whatd+=31;case10:whatd+=30;case9:whatd+=31;case8:whatd+=31;case7:whatd+=30;case6:whatd+=31;case5:whatd+=30;case4:whatd+=31;case3:if(y%4
5、原理与内容、实验步骤与记录、实验分析与结论等。3.设计一个日期类Date,包括日期的年份、月份和日号,要求能够设置日期、按照年/月/日的格式输出日期、编写友元函数,求两个日期之间相差的天数。l程序源代码(注意添加注释)#includeusingnamespacestd;intwhatday(inty,intm,intd);classDate{intyear,month,day;public:voidmodify_year(inty){year=y;}voidmodify_month(intm){month=
6、m;}voidmodify_day(intd){day=d;}Date(inty=1900,intm=1,intd=1):year(y),month(m),day(d){}voidout(){cout<<"日期为:"<=d2.year)returnabs((d1.year-d2.ye
7、ar)*365+whatday(d1.year,d1.month,d1.day)-whatday(d2.year,d2.month,d2.day));elsereturnabs((d2.year-d1.year)*365+whatday(d2.year,d2.month,d2.day)-whatday(d1.year,d1.month,d1.day));}intwhatday(inty,intm,intd)//此函数计算m月d日是本年的第几天{intwhatd=d;switch(m){case12:whatd+=30;case
8、11:whatd+=31;case10:whatd+=30;case9:whatd+=31;case8:whatd+=31;case7:whatd+=30;case6:whatd+=31;case5:whatd+=30;case4:whatd+=31;case3:if(y%4
此文档下载收益归作者所有