C上机实验报告(类和对象Ⅱ).docx_第1页
C上机实验报告(类和对象Ⅱ).docx_第2页
C上机实验报告(类和对象Ⅱ).docx_第3页
C上机实验报告(类和对象Ⅱ).docx_第4页
C上机实验报告(类和对象Ⅱ).docx_第5页
已阅读5页,还剩13页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

C+上机实验报告实验名称:类和对象专业班级:姓 名:学 号:实验日期:目录1. 实验目的2. 实验内容3. 程序代码4. 调试结果5. 实验心得1. 实验目的(1) 进一步加深对类和对象的理解;(2) 掌握类的构造函数和析构函数的概念和使用方法;(3) 掌握对象数组,对象的指针及其使用方法;(4) 掌握友元的概念和使用;(5) 了解类模板的使用方法。2. 实验内容(1) 有以下程序:#includeclass Studentpublic:Student(int n,float s):num(n),score(s)void change(int n,float s)num=n;score=s;void display()coutnum scoreendl;private: int num; float score;void main()Student stud(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) 101 5 23.5 102 12 24.56 103 100 21.5请编些程序,计算出当日此商品的总销售款sum以及每件商品的平均售价。要求用静态数据成员和静态成员函数。(3)有以下程序:#includeusing namespace std;class Date; /对Date类的提前引用声明class Time /定义Time类public:Time(int,int,int);void display(Date &); /display是成员函数,形参是Date类对象的引用private:int hour;int minute;int sec;class Date /声明Date类public:Date(int,int,int);friend void Time:display(Date &); /声明Time中的display函数为友元成员函数private:int month;int day;int year;Time:Time(int h,int m,int s) /类Time的构造函数hour=h;minute=m;sec=s;void Time:display(Date &d) /display的作用是输出年,月,日和时,分,秒coutd.month/d.day/d.yearendl; /引用Date类对象中的私有数据couthour:minute:secendl; /引用本类对象中的私有数据 Date:Date(int m,int d,int y) /类Date的构造函数month=m;day=d;year=y;int main()Time t1(10,13,56); /定义Time类对象t1Date d1(12,25,2004); /定义Date类对象d1t1.display(d1); /调用t1中的display函数,实参是Date类对象d1return 0;将程序中的display函数不放在Time类中,而作为类外的普通函数,然后分别在Time和Date类中将display声明为友元函数。在主函数中调用display函数,display函数分别引用Time和Date两个类的对象的私有数据输出 年,月,日和时,分,秒。修改后上机调试和运行。(4)有以下使用类模板程序:#includeusing namespace std;template /声明类模板,虚拟类型名为numtypeclass Compare /类模板名为Comparepublic:Compare(numtype a,numtype b) /定义构造函数x=a;y=b;numtype max() /类型函数暂定为numtypereturn(xy)?x:y;numtype min()return(xy)?x:y;private:numtype x,y;int main()Compare cmp1(3,7); /定义对象cmp1,用于两个整数比较coutcmp1.max() is the Maximum of two integer numbers. endl;coutcmp1.min() is the minimum of two integer numbers. endlendl;Compare cmp2(45.78,93.6); /定义对象cmp2,用于两个浮点数的比较coutcmp2.max() is the Maximum of two float numbers. endl;coutcmp2.min() is the minimum of two float numbers. endlendl;Compare cmp3(a,A); /定义对象cmp3,用于两个字符的比较coutcmp3.max() is the maximum of two characters. endl;coutcmp3.min() is the minimum of two characters. endl;return 0;.运行此程序;.将它改写为在类模板外定义各成员函数。3. 程序代码(1) #includeusing namespace std;class Studentpublic:Student(int n,float s):num(n),score(s)void change(int n,float s)num=n;score=s;void display()coutnumscoreendl;private:int num;float score;int main()Student stud(101,78.5);void fun(Student &);fun(stud);return 0;void fun(Student &stu)stu.display();stu.change(101,80.5);stu.display(); (2) #includeusing namespace std;class Productpublic:Product(int m,int q,float p):num(m),quantity(q),price(p);void total();static float average();static void display();private: int num; int quantity; float price; static float discount; static float sum; static int n;void Product:total()float rate=1.0;if(quantity10)rate=0.98*rate;sum=sum+quantity*price*rate*(1-discount);n=n+quantity;void Product:display()coutsumendl;coutaverage()endl;float Product:average()return(sum/n);float Product:discount=0.05;float Product:sum=0;int Product:n=0;int main()Product Prod3=Product(101,5,23.5),Product(102,12,24.56),Product(103,100,21.5);for(int i=0;i3;i+) Prodi.total();Product:display();return 0;(3) #includeusing namespace std;class Date;class Timepublic:Time(int,int,int);friend void display(const Date &,const Time &);private: int hour; int minute; int sec;Time:Time(int h,int m,int s)hour=h;minute=m;sec=s;class Datepublic:Date(int,int,int);friend void display(const Date &,const Time &);private: int month; int day; int year;Date:Date(int m,int d,int y)month=m;day=d;year=y;void display(const Date &d,const Time &t)coutd.month/d.day/d.yearendl; coutt.hour:t.minute:t.secendl;int main() Time t1(10,13,56); Date d1(12,25,2004); display(d1,t1); return 0;(4) #includeusing namespace std;templsteclass Comparepublic: Compare(numtype a,numtype b); numtype max(); numtype min();private: numtype x,y;templateCompare:Compare(numtype a,numtype b)x=a;y=btemplatenumtype Compare:max()return(xy)?x:y;templatenumtype Compare:min()return (xy)?x:y;int main()Comparecmp1(3,7);coutcmp1.max()is the Maximum of two integer numbers.endl;coutcmp1.min()is the Minimum of two integer numbers.endlendl;Comparecmp2(45.78,93.6);coutcmp2.max()is the Maximum of two float numbers.endl;coutcmp2.min()is the Minimum of two float numbers.endlendl;Comparecmp3(a,A);coutcmp3.max()is the maximum of two characters.endl;coutcmp3.min()is the minimum of two characters.endl;return 0;4. 调试结果(1) (2) (3) (4) 5. 实验心得以前上机实验的时候,对某一个程序进行调试,显示出错,那么我就仅只是单纯将打出来的程序与课本上的源程序进行

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论