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; /产品单价 11. int qua
2、ntity; /剩余产品数量 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. ; -_15. Product:P
3、roduct(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: 6. CData(in
4、t 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. day = d; 24.
5、 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; -_61. 62.
6、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/2014 4.
7、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 lp) -_7. 8. un
8、price = 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.getdata(); 35. g1
9、.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. public: 6. CFactoria
10、l(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; 10. 11. double g
11、etc() 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. class Student
12、 5. 6. public: 7. void getdata(); 8. void showdata(); 9. private: 10. string number; 11. string name; 12. class Cdegree -_13. 14. public: 15. double math; 16. double english; 17. double phy; 18. degree; 19. ; 20. void Student:getdata() 21. 22. coutnumber; 24. coutname; 26. coutdegree.math; 28. coutd
13、egree.english; 30. coutdegree.phy; 32. 33. void Student:showdata() 34. 35. cout“=分割线=“endl; 36. cout“Number:“numberendl; 37. cout“Name:“nameendl; 38. cout“Math“degree.mathendl; 39. cout“English:“degree.englishendl; 40. cout“Physics:“degree.phyendl; 41. 42. int main() 43. 44. Student s1; 45. s1.getdata(); 46. s1.showdata(); 47. return 0; 48. 结果输出:cpp view plaincopyprint?1. Input number:007 2. Input name:qianshou