c面向对象-实例题集锦.doc
《c面向对象-实例题集锦.doc》由会员分享,可在线阅读,更多相关《c面向对象-实例题集锦.doc(25页珍藏版)》请在得力文库 - 分享文档赚钱的网站上搜索。
1、-_C+面向对象实例:面向对象实例:C+面向对象类的实例题目二面向对象类的实例题目二 题目描述:编写一个程序,设计一个产品类 Product,其定义如下:cpp view plaincopyprint?1.class Product 2. 3. public: 4. Product(char *n,int p,int q); /构造函数 5. Product(); /析构函数 6. void buy(int money); /购买产品 7. void get() const; /显示剩余产品数量 8. private: 9. char * name; /产品名称 10. int price;
2、/产品单价 11. int quantity; /剩余产品数量 12. ; 并用数据进行测试。code:cpp view plaincopyprint?1.#include 2.#include 3.using namespace std; 4.class Product 5. 6. char *name; 7. int price; 8. int quantity; 9. public: 10. Product(char *n,int p,int q); 11. Product(); 12. void buy(int money); 13. void get()const; 14. ; -_
3、15. Product:Product(char *n,int p,int q) 16. 17. name = n; 18. price = p; 19. quantity = q; 20. 21. Product:Product() 22. 23. 24. void Product:buy(int money) 25. 26. int r,n; 27. n = money/price; 28. r = money%price; 29. if(n quantity) 30. 31. cout 2.using namespace std; 3.class CData 4. 5. public:
4、6. CData(int y,int m,int d); 7. void setdate(int y, int m, int d); 8. void display(); 9. void add(); 10. private: 11. int day; 12. int month; 13. int year; 14. ; 15. CData:CData(int y,int m,int d) 16. -_17. day = d; 18. month = m; 19. year = y; 20. 21. void CData:setdate(int y,int m,int d) 22. 23. d
5、ay = d; 24. month = m; 25. year = y; 26. 27. void CData:display() 28. 29. coutday)day+; 40. else 41. 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. else 55. 56. month+; 57. if(month12) 58. 59. year+; 60. month = 1;
6、 -_61. 62. day = 1; 63. 64. 65. 66. int main() 67. 68. CData date(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. return 0; 77. 结果输出:cpp view plaincopyprint?1.31/12/2013 2.1/1/2014 3.11/11/
7、2014 4.12/11/2014 C+面向对象类的实例题目四面向对象类的实例题目四 题目描述:以面向对象的概念设计一个类,此类包含 3 个私有数据:unlead、lead(无铅汽油和有铅汽油)以及 total(当天总收入,无铅汽油的价格是 17 元/升,有铅汽油的加个是 16 元/升),请以构造函数方式建立此值。试输入某天所加的汽油量,本程序将列出加油当天的总收入。程序代码:cpp view plaincopyprint?1.#include 2.using namespace std; 3.class Gas 4. 5. public: 6. Gas(double ulp,double l
8、p) -_7. 8. unprice = ulp; 9. price = lp; 10. 11. void show() 12. 13. total = unlead*unprice + lead*price; 14. coutunlead; 21. coutlead; 23. 24. private: 25. double unprice; 26. double price; 27. double lead; 28. double unlead; 29. double total; 30. ; 31. int main() 32. 33. Gas g1(17,16); 34. g1.getd
9、ata(); 35. g1.show(); 36. return 0; 37. 程序输出:cpp view plaincopyprint?1.请输入当天无铅汽油的总量:10 2.请输入当天有铅汽油的总量:20 3.无铅汽油的价格为 17 元/升,有铅汽油的价格为 16 元/升 4.total:490 -_C+面向对象类的实例题目五面向对象类的实例题目五 题目描述:编写一个程序,采用一个类求 n!,并输出 5!的值。程序代码:cpp view plaincopyprint?1.#include 2.using namespace std; 3.class CFactorial 4. 5. pub
10、lic: 6. CFactorial(int n) 7. 8. num = n; 9. total = 1; 10. 11. void calculate() 12. 13. int n = num; 14. while(n0) 15. 16. total *= n-; 17. 18. 19. void display() 20. 21. cout 2.using namespace std; 3.class Rectangular 4. 5. public: 6. Rectangular(double w,double l) 7. 8. width = w; 9. length = l; 1
11、0. 11. double getc() 12. 13. circumference = width + length; 14. return circumference; 15. 16. double adddata(Rectangular 19. 20. private: 21. double width; 22. double length; 23. double circumference; 24. ; 25. int main() 26. 27. Rectangular r1(2,3); -_28. cout 2.#include 3.using namespace std; 4.c
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 面向 对象 实例 集锦
限制150内