C++上机实验报告类和对象Ⅱ.docx
- 文档编号:2590621
- 上传时间:2023-05-04
- 格式:DOCX
- 页数:12
- 大小:44.76KB
C++上机实验报告类和对象Ⅱ.docx
《C++上机实验报告类和对象Ⅱ.docx》由会员分享,可在线阅读,更多相关《C++上机实验报告类和对象Ⅱ.docx(12页珍藏版)》请在冰点文库上搜索。
C++上机实验报告类和对象Ⅱ
C++上机实验报告
实验名称:
类和对象
专业班级:
姓名:
学号:
实验日期:
1.实验目的
2.实验内容
3.程序代码
4.调试结果
5.实验心得
1.实验目的
(1)进一步加深对类和对象的理解;
(2)掌握类的构造函数和析构函数的概念和使用方法;
(3)掌握对象数组,对象的指针及其使用方法;
(4)掌握友元的概念和使用;
(5)了解类模板的使用方法。
2.实验内容
(1)有以下程序:
#include
classStudent
{public:
Student(intn,floats):
num(n),score(s){}
voidchange(intn,floats){num=n;score=s;}
voiddisplay(){cout< private: intnum; floatscore; }; voidmain() {Studentstud(101,78.5); stud.display(); stud.change(101,80.5); stud.display(); } Ⅰ.阅读此程序,分析其执行过程,然后上机运行,对比输出结果; Ⅱ.修改上面的程序,增加一个fun函数,改写main函数。 在main函数中调用fun函数,在fun函数中调用change和display函数。 在fun函数中使用对象的引用(Student&)作为形参。 (2)商店销售某一商品,商店每天公布统一的折扣(discount)。 同时允许销售人员在销售时灵活掌握售价(price),在此基础上,对一次购10件以上者还可以享受9.8折优惠。 现已知当天3个销货员销售情况为 销货员号(num)销货件数(quantity)销货单价(price) 101523.5 1021224.56 10310021.5 请编些程序,计算出当日此商品的总销售款sum以及每件商品的平均售价。 要求用静态数据成员和静态成员函数。 (3)有以下程序: #include usingnamespacestd; classDate;//对Date类的提前引用声明 classTime//定义Time类 {public: Time(int,int,int); voiddisplay(Date&);//display是成员函数,形参是Date类对象的引用 private: inthour; intminute; intsec; }; classDate//声明Date类 {public: Date(int,int,int); friendvoidTime: : display(Date&);//声明Time中的display函数为友元成员函数 private: intmonth; intday; intyear; }; Time: : Time(inth,intm,ints)//类Time的构造函数 {hour=h; minute=m; sec=s; } voidTime: : display(Date&d)//display的作用是输出年,月,日和时,分,秒 {cout< cout< "< "< } Date: : Date(intm,intd,inty)//类Date的构造函数 {month=m; day=d; year=y; } intmain() {Timet1(10,13,56);//定义Time类对象t1 Dated1(12,25,2004);//定义Date类对象d1 t1.display(d1);//调用t1中的display函数,实参是Date类对象d1 return0; } 将程序中的display函数不放在Time类中,而作为类外的普通函数,然后分别在Time和Date类中将display声明为友元函数。 在主函数中调用display函数,display函数分别引用Time和Date两个类的对象的私有数据输出年,月,日和时,分,秒。 修改后上机调试和运行。 (4)有以下使用类模板程序: #include usingnamespacestd; template classCompare//类模板名为Compare {public: Compare(numtypea,numtypeb)//定义构造函数 {x=a;y=b;} numtypemax()//类型函数暂定为numtype {return(x>y)? x: y;} numtypemin() {return(x x: y;} private: numtypex,y; }; intmain() {Compare cout< cout< Compare cout< cout< Compare cout< cout< return0; } Ⅰ.运行此程序; Ⅱ.将它改写为在类模板外定义各成员函数。 3.程序代码 (1) #include usingnamespacestd; classStudent {public: Student(intn,floats): num(n),score(s){} voidchange(intn,floats){num=n;score=s;} voiddisplay(){cout< private: intnum; floatscore; }; intmain() {Studentstud(101,78.5); voidfun(Student&); fun(stud); return0; } voidfun(Student&stu) {stu.display(); stu.change(101,80.5); stu.display(); } (2) #include usingnamespacestd; classProduct {public: Product(intm,intq,floatp): num(m),quantity(q),price(p){}; voidtotal(); staticfloataverage(); staticvoiddisplay(); private: intnum; intquantity; floatprice; staticfloatdiscount; staticfloatsum; staticintn; }; voidProduct: : total() {floatrate=1.0; if(quantity>10)rate=0.98*rate; sum=sum+quantity*price*rate*(1-discount); n=n+quantity; } voidProduct: : display() {cout< cout< } floatProduct: : average() {return(sum/n);} floatProduct: : discount=0.05; floatProduct: : sum=0; intProduct: : n=0; intmain() {ProductProd[3]={Product(101,5,23.5),Product(102,12,24.56),Product(103,100,21.5)}; for(inti=0;i<3;i++) Prod[i].total(); Product: : display(); return0; } (3) #include usingnamespacestd; classDate; classTime {public: Time(int,int,int); friendvoiddisplay(constDate&,constTime&); private: inthour; intminute; intsec; }; Time: : Time(inth,intm,ints) {hour=h; minute=m; sec=s; } classDate {public: Date(int,int,int); friendvoiddisplay(constDate&,constTime&); private: intmonth; intday; intyear; }; Date: : Date(intm,intd,inty) {month=m; day=d; year=y; } voiddisplay(constDate&d,constTime&t) { cout< cout< "< "< } intmain() { Timet1(10,13,56); Dated1(12,25,2004); display(d1,t1); return0; } (4) #include usingnamespacestd; templste classCompare {public: Compare(numtypea,numtypeb); numtypemax(); numtypemin(); private: numtypex,y; }; template Compare : Compare(numtypea,numtypeb) {x=a;y=b} template numtypeCompare : max() {return(x>y)? x: y;} template numtypeCompare : min() {return(x x: y;} intmain() {Compare cout< cout< Compare cout< cout< Compare cout< cout< return0; } 4.调试结果 (1) Ⅰ Ⅱ (2) (3) (4) 5.实验心得 以前上机实验的时候,对某一个程序进行调试,显示出错,那么我就仅只是单纯将打出来的程序与课本上的源程序进行比较,然后凭借眼力找出错误。 虽然这样的方法可行,却也实在浪费了很多时间。 这一次,是我真正尝试着通过调试后界面给出的error和warn的提示找出错误,也第一次尝试去理解系统给出的语言。 虽然对其他人来说,可能不算什么,但是我觉得这算是我自己的一个提高。 然后,也通过上机编码程序,更加地了解了类模板函数,对类的构造函数和析构函数在形式和结构上也能很好的分辨。 至于友元函数,我还需要多分析一些程序,进而了解友元函数访问权限。 总的来说,我觉得这次试验真的是收获挺大的,也可能是几次试验下来觉得比较成功的一个了。
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- C+ 上机 实验 报告 对象