




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、C+面向对象实例:C+面向对象类的实例题目二 题目描述:编写一个程序,设计一个产品类Product,其定义如下:cpp view plaincopyprint?1. class Product 2. 3. public: 4. Product(char *n,int p,int q); /构造函数
2、160;5. Product(); /析构函数 6. void buy(int money);
3、; /购买产品 7. void get() const; /显示剩余产品数量 8.
4、private: 9. char * name; /产品名称 10. int price; /产品单价 11.
5、 int quantity; /剩余产品数量 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 plaincopyp
6、rint?1. #include<iostream> 2. #include<cstring> 3. using namespace std; 4. class Product 5. 6. char *name; 7. &
7、#160;int price; 8. int quantity; 9. public: 10. Product(char *n,int p,int q); 11.
8、; Product(); 12. void buy(int money); 13. void get()const; 14. ; 15. Product:Product(char *n,int p,int q)
9、160;16. 17. name = n; 18. price = p; 19. quantity = q; 20. 21. Product:Product() 22. 23. 24. void Pro
10、duct:buy(int money) 25. 26. int r,n; 27. n = money/price; 28. r = money%price; 29. if(n > quantity)
11、 30. 31. cout<<"数量不够"<<endl; 32. 33. else 34. 35.
12、60; quantity -= n; 36. cout<<"名称:"<<name<<",单价:"<<price<<"元"<<endl; 37. cout<<
13、;"顾客使用"<<money<<"元,购买"<<n<<"台,剩余"<<r<<"元"<<endl; 38. 39. 40. void Product:get()const 41. 42. cout<
14、<"产品:"<<name<<",单价:"<<price<<",剩余:"<<quantity<<"台"<<endl; 43. 44. int main() 45. 46. Product p("Iphone6",100,20);
15、 47. p.buy(10); 48. p.get(); 49. cout<<"n=n"<<endl; 50. p.buy(1000); 51. p.get(); 52.
16、 return 0; 53. #include<iostream>#include<cstring>using 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)na
17、me = 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<<"元"<<end
18、l;cout<<"顾客使用"<<money<<"元,购买"<<n<<"台,剩余"<<r<<"元"<<endl; void Product:get()constcout<<"产品:"<<name<<",单价:"<<price<<",剩余:"<<quantity<<"台&q
19、uot;<<endl; int main()Product p("Iphone6",100,20);p.buy(10);p.get();cout<<"n=n"<<endl; p.buy(1000);p.get();return 0; 输出:C+面向对象类的实例题目三 编写一个程序,设计一个满足如下要求的CData类。(1)用下面的格式输出日期:日/月/年(2)输出在当前日期上加一天后的日期(3)设置日期code:cpp view plaincopyprint?1. #include<iostream>
20、60; 2. using namespace std; 3. class CData 4. 5. public: 6. CData(int y,int m,int d); 7.
21、 void setdate(int y, int m, int d); 8. void display(); 9. void add(); 10. private: 11
22、. int day; 12. int month; 13. int year; 14. ; 15. CData:CData(int y,int m,int d)
23、160; 16. 17. day = d; 18. month = m; 19. year = y; 20. 21. void CData:setdate(int y,int m,int d) 22.
24、0; 23. day = d; 24. month = m; 25. year = y; 26. 27. void CData:display() 28. 29. cout<<da
25、y<<"/"<<month<<"/"<<year<<endl; 30. 31. void CData:add() 32. 33. int a212= 34. 31,28,31,30,31,30,31,31,30,31,30,31, 35
26、. 31,29,31,30,31,30,31,31,30,31,30,31 36. 37. if(year%400 = 0)|(year%100 !=0 && year%4 =0)/闰年的情况 38. 39.
27、60; if(a1month-1>day)day+; 40. else 41. 42. month+;
28、60; 43. if(month>12) 44. 45.
29、year+; 46. month = 1; 47. 48.
30、0; day = 1; 49. 50. 51. else
31、 /平年的情况 52. 53. if(a0month-1>day)day+; 54. else 5
32、5. 56. month+; 57. if(month>12) 58.
33、; 59. year+; 60. month = 1; 6
34、1. 62. day = 1; 63. 6
35、4. 65. 66. int main() 67. 68. CData date(2013,12,31); 69. date.display(); 70. date.add(); 71.
36、160;date.display(); 72. date.setdate(2014,11,11); 73. date.display(); 74. date.add(); 75. date.display(); 76. return 0;&
37、#160; 77. #include<iostream>using 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,i
38、nt m,int d)day = d;month = m;year = y;void CData:display()cout<<day<<"/"<<month<<"/"<<year<<endl; void CData:add()int a212=31,28,31,30,31,30,31,31,30,31,30,31,31,29,31,30,31,30,31,31,30,31,30,31;if(year%400 = 0)|(year%100 !=0 && year%4
39、=0)/闰年的情况 if(a1month-1>day)day+;else month+;if(month>12)year+;month = 1;day = 1; else/平年的情况 if(a0month-1>day)day+;else month+;if(month>12)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();
40、date.display();return 0; 结果输出:cpp view plaincopyprint?1. 31/12/2013 2. 1/1/2014 3. 11/11/2014 4. 12/11/2014 C+面向对象类的实例题目四 题目描述:以面向对象的概念设计一个类,此类包含3个私有数据:unlead、lead(无铅汽油和有铅汽油)以及total(当天总收入,无铅汽油的价格是17元/升,有铅汽油的加个是16元/升),请以构造函数方式建立此值。试输入某天所加的汽油量,本程序将列出加油当天的总收入
41、。程序代码:cpp view plaincopyprint?1. #include<iostream> 2. using namespace std; 3. class Gas 4. 5. public: 6. Gas(double ulp,double lp)
42、0;7. 8. unprice = ulp; 9. price = lp; 10.
43、 11. void show() 12. 13. total = unlead*unprice
44、 + lead*price; 14. cout<<"无铅汽油的价格为17元/升,有铅汽油的价格为16元/升"<<endl; 15. cout<<"total:"
45、<<total<<endl; 16. 17. void getdata() 18. 19.
46、; cout<<"请输入当天无铅汽油的总量:" 20. cin>>unlead; 21. cout<<"请输入当天有铅汽油的总量:" &
47、#160;22. cin>>lead; 23. 24. private: 25. double unprice;&
48、#160; 26. double price; 27. double lead; 28. double unlead; 29.
49、60; double total; 30. ; 31. int main() 32. 33. Gas g1(17,16); 34. g1.getdata(); 35.
50、60; g1.show(); 36. return 0; 37. #include<iostream>using namespace std;class Gaspublic:Gas(double ulp,double lp)unprice = ulp;price = lp;void show()total = unlead*unprice + lead*price;cout<<"无铅汽油的价格为17元/升,有铅汽油的价格为16
51、元/升"<<endl; cout<<"total:"<<total<<endl;void getdata()cout<<"请输入当天无铅汽油的总量:"cin>>unlead;cout<<"请输入当天有铅汽油的总量:"cin>>lead; private:double unprice;double price;double lead;double unlead;double total;int main()Gas g1(17,16)
52、;g1.getdata();g1.show();return 0;程序输出:cpp view plaincopyprint?1. 请输入当天无铅汽油的总量:10 2. 请输入当天有铅汽油的总量:20 3. 无铅汽油的价格为17元/升,有铅汽油的价格为16元/升 4. total:490 C+面向对象类的实例题目五 题目描述:编写一个程序,采用一个类求n!,并输出5!的值。程序代码:cpp view plaincopyprint?1. #include<iostream> 2
53、. using namespace std; 3. class CFactorial 4. 5. public: 6. CFactorial(int n) 7. 8.
54、160; num = n; 9. total = 1; 10. 11. &
55、#160; void calculate() 12. 13. int n = num; 14. &
56、#160;while(n>0) 15. 16. total *= n-; 17.
57、160; 18. 19. void display() 20. 21.
58、60; cout<<num<<"! = "<<total<<endl; 22. 23. private: 24. int num;
59、60; 25. long total; 26. ; 27. int main() 28. 29. CFactorial f(5); 30. f.calculate(); 31
60、. f.display(); 32. return 0; 33. 34. #include<iostream>using namespace std;class CFactorialpublic:CFactorial(int n)num = n;total = 1;void calculate()int n = num;while(n>0)tot
61、al *= n-;void display()cout<<num<<"! = "<<total<<endl; private:int num;long total;int main()CFactorial f(5);f.calculate();f.display();return 0; 程序输出:cpp view plaincopyprint?1. 5! = 120 C+面向对象类的实例题目六 问题描述:编写一个程序计算两个给定长方形的面积,其中在设计类成员函数addarea()(用于
62、计算两个长方形的总面积)时使用对象作为参数。程序代码:cpp view plaincopyprint?1. #include<iostream> 2. using namespace std; 3. class Rectangular 4. 5. public: 6. Rectangular(doub
63、le w,double l) 7. 8. width = w; 9. length =
64、l; 10. 11. double getc() 12. 13.
65、circumference = width + length; 14. return circumference; 15. 16. double&
66、#160;adddata(Rectangular &r) 17. 18. return (circumference + r.getc(); 19.
67、; 20. private: 21. double width; 22. double length; 23. double circumfere
68、nce; 24. ; 25. int main() 26. 27. Rectangular r1(2,3); 28. cout<<"Circumference of r1 ="<<r1.getc()<<endl; &
69、#160;29. Rectangular r2(3,4); 30. cout<<"Circumference of r2 ="<<r2.getc()<<endl; 31. cout<<"Circumference of r1+r2 ="<<r
70、1.adddata(r2)<<endl; 32. return 0; 33. #include<iostream>using namespace std;class Rectangularpublic:Rectangular(double w,double l)width = w;length = l;double getc()circumference = width + length;retur
71、n circumference;double adddata(Rectangular &r)return (circumference + r.getc();private:double width;double length;double circumference; int main()Rectangular r1(2.5,3.5);cout<<"Circumference of r1 ="<<r1.getc()<<endl; Rectangular r2(2,3);cout<<"Circumferenc
72、e of r2 ="<<r2.getc()<<endl;cout<<"Circumference of r1+r2 ="<<r1.adddata(r2)<<endl;return 0; 结果输出:cpp view plaincopyprint?1. Circumference of r1 =6 2. Circumference of r2 =12 3. Circumference of
73、0;r1+r2 =18 C+面向对象类的实例题目七 题目描述:编写两个有意义的类,使一个类嵌套在另一个类中。分析:本题涉及两个类student和cdegree,前者为学生类,包含学生的学号(nubner),姓名(name)和成绩(degree),而成绩degree是类cdegree的对象。cdegree类有3个数据成员,分别为数学(math),英语(english)和物理(phy)分数。程序代码:cpp view plaincopyprint?1. #include<iostream> 2. #include<string
74、> 3. using namespace std; 4. class Student 5. 6. public: 7. void getdata(); 8. void&
75、#160;showdata(); 9. private: 10. string number; 11. string name; 12. class
76、0;Cdegree 13. 14. public: 15. double math;
77、60; 16. double english; 17. double phy; 18.
78、60; degree; 19. ; 20. void Student:getdata() 21. 22. cout<<"Input number:"
79、23. cin>>number; 24. cout<<"Input name:" 25. cin>>name; 26. cout<<"Input degree of math:" 27.
80、160; cin>>degree.math; 28. cout<<"Input degree of english:" 29. cin>>degree.english; 30. cout<<"Input degree of
81、60;physics:" 31. cin>>degree.phy; 32. 33. void Student:showdata() 34. 35. cout<<"=分割线="<<endl; 36. cout<<
82、;"Number:"<<number<<endl; 37. cout<<"Name:"<<name<<endl; 38. cout<<"Math"<<degree.math<<endl; 39. cout<<"E
83、nglish:"<<degree.english<<endl; 40. cout<<"Physics:"<<degree.phy<<endl; 41. 42. int main() 43. 44. Student s1; 45.
84、160; s1.getdata(); 46. s1.showdata(); 47. return 0; 48. #include<iostream>#include<string>using namespace std;class Student public:void getdata();void showdata();private:string nu
85、mber;string name;class Cdegreepublic:double math;double english;double phy;degree;void Student:getdata() cout<<"Input number:"cin>>number;cout<<"Input name:"cin>>name;cout<<"Input degree of math:"cin>>degree.math;cout<<"Inp
86、ut degree of english:"cin>>degree.english;cout<<"Input degree of physics:"cin>>degree.phy; void Student:showdata()cout<<"=分割线="<<endl; cout<<"Number:"<<number<<endl;cout<<"Name:"<<name<<end
87、l;cout<<"Math"<<degree.math<<endl;cout<<"English:"<<degree.english<<endl;cout<<"Physics:"<<degree.phy<<endl; int main()Student s1;s1.getdata();s1.showdata();return 0;结果输出:cpp view plaincopyprint?1. Input number:
88、007 2. Input name:qianshou 3. Input degree of math:89 4. Input degree of english:99 5. Input degree of physics:100 6. =分割线= 7. Number:007 8. Name:qianshou 9. Math
89、89 10. English:99 11. Physics:100 C+面向对象类的实例题目八 题目描述:编写一个程序输入3个学生的英语和计算机成绩,并按照总分从高到低排序。要求设计一个学生类Student,其定义如下:程序代码:cpp view plaincopyprint?1. #include<iostream> 2. using namespace std; 3. class Student 4.
90、160;5. public: 6. void getscore(); /获取一个学生成绩 7. void display(); /显示一个学生成绩 8.
91、160; void sort( Student *); /将若干个学生按总分从高到低排序 9. private: 10. int english; 11. int
92、 computer; 12. int total; 13. ; 14. void Student:getscore() 15. 16. cout<<"请输入该学生的英语成绩:" 17. cin>
93、>english; 18. cout<<"请输入该学生的计算机成绩:" 19. cin>>computer; 20. total = english + computer; 21. 22. void Student:display(
94、) 23. 24. cout<<"该学生的英语成绩为:"<<english<<",计算机成绩为:"<<computer<<",总分为:"<<total<<endl; 25. 26. void Student:sort(Student *p) 27.
95、 28. if(p->total > total) /p指向的对象比该对象大的时候,则交换对象的值 29. 30. int t1,t2,t3; 31. t1&
96、#160;= p->english; 32. p->english = english; 33. english = t1; 34. t2 = p->comp
97、uter; 35. p->computer = computer; 36. computer = t2; 37. t3 = p->total;
98、0; 38. p->total = total; 39. total = t3; 40. 41. 42. int main() 43. 44.
99、 Student st3; 45. for(int i = 0; i < 3; i+) 46. 47. sti.getscore(); 48.
100、60; sti.display(); 49. 50. st0.sort(&st1); 51. st0.sort(&st2); 52. st1.sort(&st2); 53.
101、160; cout<<"="<<endl; 54. cout<<"排序结果如下:"<<endl; 55. for(int i = 0; i < 3; i+) 56. 57.
102、60; sti.display(); 58. 59. 输出结果:cpp view plaincopyprint?1. 请输入该学生的英语成绩:80 2. 请输入该学生的计算机成绩:90 3. 该学生的英语成绩为:80,计算机成绩为:90,总分为:170 4. 请输入该学生的英语成绩:70 5. 请输
103、入该学生的计算机成绩:60 6. 该学生的英语成绩为:70,计算机成绩为:60,总分为:130 7. 请输入该学生的英语成绩:99 8. 请输入该学生的计算机成绩:87 9. 该学生的英语成绩为:99,计算机成绩为:87,总分为:186 10. = 11. 排序结果如下: 12. 该学生的英语成绩为:99,计算机成绩为:87,总分为:186 13. 该学生的英语成绩为:80,计算机成绩为:90,总分为:170
104、 14. 该学生的英语成绩为:70,计算机成绩为:60,总分为:130 C+面向对象类的实例题目九 题目描述:编写一个学生和老师数据输入和显示程序,学生数据有编号、姓名、班号和成绩,教师数据有编号、姓名、职称和部门。要求将编号、姓名、输入和显示设计成一个类person,并作为学生数据操作类student和教师数据操作类teacher的基类。程序代码:cpp view plaincopyprint?1. #include<iostream> 2. #include<string> 3. using
105、 namespace std; 4. class Person 5. 6. public: 7. void get() 8. 9.
106、160; cout<<"请输入编号:" 10. cin>>number; 11. cout<<"请输入姓名:"
107、 12. cin>>name; 13. 14. void show() 15.
108、 16. cout<<"NO."<<number<<endl; 17. cout<<"name:"<<name<<end
109、l; 18. 19. private: 20. string number; 21. string name; 22. ;
110、; 23. class Student:public Person 24. 25. public: 26. void get() 27. 28.
111、 Person:get(); 29. cout<<"请输入班级编号:" 30. cin>>class_number; &
112、#160;31. cout<<"请输入成绩:" 32. cin>>grade; 33. 34
113、. void show() 35. 36. Person:show(); 37.
114、0; cout<<"class_number:"<<class_number<<endl; 38. cout<<"grade:"<<grade<<endl; 39.
115、160; 40. private: 41. string class_number; 42. float grade; 43. ; 44. class Teacher:public Person 45. 46. public: 47. voi
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 16 《大家排好队》(教学设计)2024-2025学年统编版(2024)小学道德与法治一年级上册
- 云南科技信息职业学院《文学作品与影视改编》2023-2024学年第二学期期末试卷
- 临沂职业学院《交通大数据分析与处理》2023-2024学年第二学期期末试卷
- 河南2025年河南省委党校省直分校招聘博士研究生2人笔试历年参考题库附带答案详解
- 辽宁装备制造职业技术学院《水质监测与实验》2023-2024学年第二学期期末试卷
- 洛阳师范学院《运动技能学习与控制》2023-2024学年第二学期期末试卷
- 2025年度文化活动场地租赁合同规范文本
- 监理机构职责
- 小数的意义二(教学设计)-2023-2024学年四年级下册数学北师大版
- 2025年度文化产业反担保保证合同及文化产业发展规划
- 《电力建设工程施工安全管理导则》(NB∕T 10096-2018)
- 2024-2025学年广东省部分学校高一(上)第一次联合考试物理试卷(含答案)
- 《黄色新闻的泛滥》课件
- 2024年山东省公务员考试《行测》真题及答案解析
- 化工原理Ⅱ学习通超星期末考试答案章节答案2024年
- 2024-2025学年初中体育与健康九年级全一册人教版(2024)教学设计合集
- 环保产业政策及市场发展趋势分析研究
- 2024年河南省高考对口升学语文英语试题
- 学习白求恩精神,做一个高尚的人一个纯洁的人
- 《中医药学概论》期末考试复习题库(含答案)
- 2024年秋季新外研版三年级上册英语课件 Unit 1 第1课时(Get ready)
评论
0/150
提交评论