已阅读5页,还剩20页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
C+面向对象实例:C+面向对象类的实例题目二 题目描述:编写一个程序,设计一个产品类Product,其定义如下:cpp view plaincopyprint?1. classProduct2. 3. public:4. Product(char*n,intp,intq);/构造函数5. Product();/析构函数6. voidbuy(intmoney);/购买产品7. voidget()const;/显示剩余产品数量8. private:9. char*name;/产品名称10. intprice;/产品单价11. intquantity;/剩余产品数量12. ;class Productpublic:Product(char *n,int p,int q);/构造函数Product();/析构函数void buy(int money);/购买产品void get() const;/显示剩余产品数量 private:char * name;/产品名称int price;/产品单价int quantity;/剩余产品数量; 并用数据进行测试。code:cpp view plaincopyprint?1. #include2. #include3. usingnamespacestd;4. classProduct5. 6. char*name;7. intprice;8. intquantity;9. public:10. Product(char*n,intp,intq);11. Product();12. voidbuy(intmoney);13. voidget()const;14. ;15. Product:Product(char*n,intp,intq)16. 17. name=n;18. price=p;19. quantity=q;20. 21. Product:Product()22. 23. 24. voidProduct:buy(intmoney)25. 26. intr,n;27. n=money/price;28. r=money%price;29. if(nquantity)30. 31. cout数量不够endl;32. 33. else34. 35. quantity-=n;36. cout名称:name,单价:price元endl;37. cout顾客使用money元,购买n台,剩余r元endl;38. 39. 40. voidProduct:get()const41. 42. cout产品:name,单价:price,剩余:quantity台endl;43. 44. intmain()45. 46. Productp(Iphone6,100,20);47. p.buy(10);48. p.get();49. coutn=nendl;50. p.buy(1000);51. p.get();52. return0;53. #include#includeusing namespace std;class Productchar *name;int price;int quantity;public:Product(char *n,int p,int q);Product();void buy(int money);void get()const;Product:Product(char *n,int p,int q)name = n;price = p;quantity = q;Product:Product()void Product:buy(int money)int r,n;n = money/price;r = money%price;if(n quantity)cout数量不够endl;elsequantity -= n;cout名称:name,单价:price元endl;cout顾客使用money元,购买n台,剩余r元endl; void Product:get()constcout产品:name,单价:price,剩余:quantity台endl; int main()Product p(Iphone6,100,20);p.buy(10);p.get();coutn=nendl; p.buy(1000);p.get();return 0; 输出:C+面向对象类的实例题目三 编写一个程序,设计一个满足如下要求的CData类。(1)用下面的格式输出日期:日/月/年(2)输出在当前日期上加一天后的日期(3)设置日期code:cpp view plaincopyprint?1. #include2. usingnamespacestd;3. classCData4. 5. public:6. CData(inty,intm,intd);7. voidsetdate(inty,intm,intd);8. voiddisplay();9. voidadd();10. private:11. intday;12. intmonth;13. intyear;14. ;15. CData:CData(inty,intm,intd)16. 17. day=d;18. month=m;19. year=y;20. 21. voidCData:setdate(inty,intm,intd)22. 23. day=d;24. month=m;25. year=y;26. 27. voidCData:display()28. 29. coutday/month/yearday)day+;40. else41. 42. month+;43. if(month12)44. 45. year+;46. month=1;47. 48. day=1;49. 50. 51. else/平年的情况52. 53. if(a0month-1day)day+;54. else55. 56. month+;57. if(month12)58. 59. year+;60. month=1;61. 62. day=1;63. 64. 65. 66. intmain()67. 68. CDatadate(2013,12,31);69. date.display();70. date.add();71. date.display();72. date.setdate(2014,11,11);73. date.display();74. date.add();75. date.display();76. return0;77. #includeusing namespace std;class CData public:CData(int y,int m,int d); void setdate(int y, int m, int d);void display();void add();private:int day;int month;int year;CData:CData(int y,int m,int d)day = d;month = m;year = y;void CData:setdate(int y,int m,int d)day = d;month = m;year = y;void CData:display()coutday/month/yearday)day+;else month+;if(month12)year+;month = 1;day = 1; else/平年的情况 if(a0month-1day)day+;else month+;if(month12)year+;month = 1;day = 1; int main()CData date(2013,12,31);date.display();date.add();date.display();date.setdate(2014,11,11);date.display();date.add();date.display();return 0; 结果输出:cpp view plaincopyprint?1. 31/12/20132. 1/1/20143. 11/11/20144. 12/11/2014C+面向对象类的实例题目四 题目描述:以面向对象的概念设计一个类,此类包含3个私有数据:unlead、lead(无铅汽油和有铅汽油)以及total(当天总收入,无铅汽油的价格是17元/升,有铅汽油的加个是16元/升),请以构造函数方式建立此值。试输入某天所加的汽油量,本程序将列出加油当天的总收入。程序代码:cpp view plaincopyprint?1. #include2. usingnamespacestd;3. classGas4. 5. public:6. Gas(doubleulp,doublelp)7. 8. unprice=ulp;9. price=lp;10. 11. voidshow()12. 13. total=unlead*unprice+lead*price;14. cout无铅汽油的价格为17元/升,有铅汽油的价格为16元/升endl;15. couttotal:totalendl;16. 17. voidgetdata()18. 19. coutunlead;21. coutlead;23. 24. private:25. doubleunprice;26. doubleprice;27. doublelead;28. doubleunlead;29. doubletotal;30. ;31. intmain()32. 33. Gasg1(17,16);34. g1.getdata();35. g1.show();36. return0;37. #includeusing namespace std;class Gaspublic:Gas(double ulp,double lp)unprice = ulp;price = lp;void show()total = unlead*unprice + lead*price;cout无铅汽油的价格为17元/升,有铅汽油的价格为16元/升endl; couttotal:totalendl;void getdata()coutunlead;coutlead; private:double unprice;double price;double lead;double unlead;double total;int main()Gas g1(17,16);g1.getdata();g1.show();return 0;程序输出:cpp view plaincopyprint?1. 请输入当天无铅汽油的总量:102. 请输入当天有铅汽油的总量:203. 无铅汽油的价格为17元/升,有铅汽油的价格为16元/升4. total:490C+面向对象类的实例题目五 题目描述:编写一个程序,采用一个类求n!,并输出5!的值。程序代码:cpp view plaincopyprint?1. #include2. usingnamespacestd;3. classCFactorial4. 5. public:6. CFactorial(intn)7. 8. num=n;9. total=1;10. 11. voidcalculate()12. 13. intn=num;14. while(n0)15. 16. total*=n-;17. 18. 19. voiddisplay()20. 21. coutnum!=totalendl;22. 23. private:24. intnum;25. longtotal;26. ;27. intmain()28. 29. CFactorialf(5);30. f.calculate();31. f.display();32. return0;33. 34. #includeusing namespace std;class CFactorialpublic:CFactorial(int n)num = n;total = 1;void calculate()int n = num;while(n0)total *= n-;void display()coutnum! = totalendl; private:int num;long total;int main()CFactorial f(5);f.calculate();f.display();return 0; 程序输出:cpp view plaincopyprint?1. 5!=120C+面向对象类的实例题目六 问题描述:编写一个程序计算两个给定长方形的面积,其中在设计类成员函数addarea()(用于计算两个长方形的总面积)时使用对象作为参数。程序代码:cpp view plaincopyprint?1. #include2. usingnamespacestd;3. classRectangular4. 5. public:6. Rectangular(doublew,doublel)7. 8. width=w;9. length=l;10. 11. doublegetc()12. 13. circumference=width+length;14. returncircumference;15. 16. doubleadddata(Rectangular&r)17. 18. return(circumference+r.getc();19. 20. private:21. doublewidth;22. doublelength;23. doublecircumference;24. ;25. intmain()26. 27. Rectangularr1(2,3);28. coutCircumferenceofr1=r1.getc()endl;29. Rectangularr2(3,4);30. coutCircumferenceofr2=r2.getc()endl;31. coutCircumferenceofr1+r2=r1.adddata(r2)endl;32. return0;33. #includeusing namespace std;class Rectangularpublic:Rectangular(double w,double l)width = w;length = l;double getc()circumference = width + length;return circumference;double adddata(Rectangular &r)return (circumference + r.getc();private:double width;double length;double circumference; int main()Rectangular r1(2.5,3.5);coutCircumference of r1 =r1.getc()endl; Rectangular r2(2,3);coutCircumference of r2 =r2.getc()endl;coutCircumference of r1+r2 =r1.adddata(r2)endl;return 0; 结果输出:cpp view plaincopyprint?1. Circumferenceofr1=62. Circumferenceofr2=123. Circumferenceofr1+r2=18C+面向对象类的实例题目七 题目描述:编写两个有意义的类,使一个类嵌套在另一个类中。分析:本题涉及两个类student和cdegree,前者为学生类,包含学生的学号(nubner),姓名(name)和成绩(degree),而成绩degree是类cdegree的对象。cdegree类有3个数据成员,分别为数学(math),英语(english)和物理(phy)分数。程序代码:cpp view plaincopyprint?1. #include2. #include3. usingnamespacestd;4. classStudent5. 6. public:7. voidgetdata();8. voidshowdata();9. private:10. stringnumber;11. stringname;12. classCdegree13. 14. public:15. doublemath;16. doubleenglish;17. doublephy;18. degree;19. ;20. voidStudent:getdata()21. 22. coutnumber;24. coutname;26. coutdegree.math;28. coutdegree.english;30. coutdegree.phy;32. 33. voidStudent:showdata()34. 35. cout=分割线=endl;36. coutNumber:numberendl;37. coutName:nameendl;38. coutMathdegree.mathendl;39. coutEnglish:degree.englishendl;40. coutPhysics:degree.phyendl;41. 42. intmain()43. 44. Students1;45. s1.getdata();46. s1.showdata();47. return0;48. #include#includeusing namespace std;class Student public:void getdata();void showdata();private:string number;string name;class Cdegreepublic:double math;double english;double phy;degree;void Student:getdata() coutnumber;coutname;coutdegree.math;coutdegree.english;coutdegree.phy; void Student:showdata()cout=分割线=endl; coutNumber:numberendl;coutName:nameendl;coutMathdegree.mathendl;coutEnglish:degree.englishendl;coutPhysics:degree.phyendl; int main()Student s1;s1.getdata();s1.showdata();return 0;结果输出:cpp view plaincopyprint?1. Inputnumber:0072. Inputname:qianshou3. Inputdegreeofmath:894. Inputdegreeofenglish:995. Inputdegreeofphysics:1006. =分割线=7. Number:0078. Name:qianshou9. Math8910. English:9911. Physics:100C+面向对象类的实例题目八 题目描述:编写一个程序输入3个学生的英语和计算机成绩,并按照总分从高到低排序。要求设计一个学生类Student,其定义如下:程序代码:cpp view plaincopyprint?1. #include2. usingnamespacestd;3. classStudent4. 5. public:6. voidgetscore();/获取一个学生成绩7. voiddisplay();/显示一个学生成绩8. voidsort(Student*);/将若干个学生按总分从高到低排序9. private:10. intenglish;11. intcomputer;12. inttotal;13. ;14. voidStudent:getscore()15. 16. coutenglish;18. coutcomputer;20. total=english+computer;21. 22. voidStudent:display()23. 24. cout该学生的英语成绩为:english,计算机成绩为:computer,总分为:totaltotaltotal)/p指向的对象比该对象大的时候,则交换对象的值29. 30. intt1,t2,t3;31. t1=p-english;32. p-english=english;33. english=t1;34. t2=p-computer;35. p-computer=computer;36. computer=t2;37. t3=p-total;38. p-total=total;39. total=t3;40. 41. 42. intmain()43. 44. Studentst3;45. for(inti=0;i3;i+)46. 47. sti.getscore();48. sti.display();49. 50. st0.sort(&st1);51. st0.sort(&st2);52. st1.sort(&st2);53. cout=endl;54. cout排序结果如下:endl;55. for(inti=0;i3;i+)56. 57. sti.display();58. 59. 输出结果:cpp view plaincopyprint?1. 请输入该学生的英语成绩:802. 请输入该学生的计算机成绩:903. 该学生的英语成绩为:80,计算机成绩为:90,总分为:1704. 请输入该学生的英语成绩:705. 请输入该学生的计算机成绩:606. 该学生的英语成绩为:70,计算机成绩为:60,总分为:1307. 请输入该学生的英语成绩:998. 请输入该学生的计算机成绩:879. 该学生的英语成绩为:99,计算机成绩为:87,总分为:18610. =11. 排序结果如下:12. 该学生的英语成绩为:99,计算机成绩为:87,总分为:18613. 该学生的英语成绩为:80,计算机成绩为:90,总分为:17014. 该学生的英语成绩为:70,计算机成绩为:60,总分为:130C+面向对象类的实例题目九 题目描述:编写一个学生和老师数据输入和显示程序,学生数据有编号、姓名、班号和成绩,教师数据有编号、姓名、职称和部门。要求将编号、姓名、输入和显示设计成一个类person,并作为学生数据操作类student和教师数据操作类teacher的基类。程序代码:cpp view plaincopyprint?1. #include2. #include3. usingnamespacestd;4. classPerson5. 6. public:7. voidget()8. 9. coutnumber;11. coutname;13. 14. voidshow()15. 16. coutNO.numberendl;17. coutname:nameendl;18. 19. private:20. stringnumber;21. stringname;22. ;23. classStudent:publicPerson24. 25. public:26. voidget()27. 28. Person:get();29. coutclass_number;31. coutgrade;33. 34. voidshow()35. 36. Person:show();37. coutclass_number:class_numberendl;38. coutgrade:gradeendl;39. 40. private:41. stringclass_number;42. floatgrade;43. ;44. classTeacher:publicPerson45. 46. public:47. voidget()48. 49. Person:get();50. couttitle;52. coutdepartment;54. 55. voidshow()56. 57.
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 室内环境照明效果调节与优化方案
- 风电场工程沟通与协调方案
- 2026届黑龙江省齐齐哈尔市高二化学第一学期期中预测试题含解析
- 开学安全第一课教案课件
- 劳模分享会策划方案
- 设计师会议考试题及答案
- 中华传统文化安全课件
- 小学生冬季安全教育课件
- 隰县村官面试真题及答案
- 青海高中会考试题及答案
- 2024-2025学年江西省南昌市高二上学期期末联考数学试卷(含答案)
- 2025惠州市职工劳动合同范本惠州市劳动与社保局制
- 《化工园区公共管廊管架养护技术管理规程》文本及编制说明
- 尼康NikonCOOLPIXP500数码相机(中文)说明书
- 中智笔试题库及答案
- DB21T 1581-2020 数字林业 森林资源分类编码 属性代码
- 车辆运输协议书范本
- 民宿承包经营合同8
- 纪检监察业务知识试题库及答案
- 《孟子》精读学习通超星期末考试答案章节答案2024年
- 智慧城市公共安全视频监控系统维护手册
评论
0/150
提交评论