c面向对象-实例题集锦.doc
-_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 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. ; -_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: 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. day = d; 24. month = m; 25. year = y; 26. 27. void CData:display() 28. 29. coutday)day+; 40. else 41. 42. month+; 43. if(month>12) 44. 45. year+; 46. month = 1; 47. 48. day = 1; 49. 50. 51. else /平年的情况 52. 53. if(a0month-1>day)day+; 54. else 55. 56. month+; 57. if(month>12) 58. 59. year+; 60. month = 1; -_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/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 lp) -_7. 8. unprice = ulp; 9. price = lp; 10. 11. void show() 12. 13. total = unlead*unprice + lead*price; 14. cout>unlead; 21. cout>lead; 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.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. CFactorial(int n) 7. 8. num = n; 9. total = 1; 10. 11. void calculate() 12. 13. int n = num; 14. while(n>0) 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 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.class Student 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. cout>number; 24. cout>name; 26. cout>degree.math; 28. cout>degree.english; 30. cout>degree.phy; 32. 33. void Student:showdata() 34. 35. cout 2.using namespace std; 3.class Student 4. 5. public: 6. void getscore(); /获取一个学生成绩 7. void display(); /显示一个学生成绩 8. void sort( Student *); /将若干个学生按总分从高到低排序 9. private: 10. int english; 11. int computer; 12. int total; 13. ; 14. void Student:getscore() 15. 16. cout>english; 18. cout>computer; 20. total = english + computer; 21. 22. void Student:display() -_23. 24. couttotal > total) /p 指向的对象比该对象大的时候,则交换对象的值29. 30. int t1,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. int main() 43. 44. Student st3; 45. for(int i = 0; i 2.#include 3.using namespace std; 4.class Person 5. 6. public: 7. void get() 8. 9. cout>number; 11. cout>name; 13. 14. void show() 15. -_16. cout>class_number; 31. cout>grade; 33. 34. void show() 35. 36. Person:show(); 37. cout>title; 52. cout>department; 54. 55. void show() 56. 57. Person:show(); 58. cout 2.using namespace std; 3.class Vehicle 4. 5. public: 6. Vehicle(int wl,double wh):wheels(wl),weight(wh); 7. void show() 8. 9. cout 2.#include 3.#define PAI 3.1415 4.using namespace std; 5.class Shape 6. 7. public: 8. virtual float area() /定义一个求面积的成员函数 9. 10. return 0; 11. 12. virtual void ShapeName() = 0;/定义一个纯虚函数 13. ; 14. class Triangle:public Shape 15. 16. public: 17. Triangle(float x,float y,float z):a(x),b(y),c(z); 18. void ShapeName() 19. 20. coutShapeName(); 68. coutarea()ShapeName(); 72. coutarea()ShapeName(); 76. coutarea() 2.#define PAI 3.1415 3.using namespace std; 4.class Shape 5. 6. public: 7. virtual void ShapeName()=0; 8. virtual void area() 9. 10. return ; 11. 12. virtual void volume() 13. 14. return ; 15. 16. ; 17. class Cube:public Shape 18. 19. public: 20. Cube(float len):length(len); 21. void ShapeName() 22. 23. coutShapeName(); 86. pt->area(); 87. pt->volume(); 88. coutShapeName(); 91. pt->area(); 92. pt->volume(); 93. coutShapeName(); 96. pt->area(); 97. pt->volume(); 98. cout 2.using namespace std; 3.class Vehicle 4. 5. public: 6. Vehicle(int wl,double wh):wheels(wl),weight(wh); 7. void show() 8. 9. cout<<“wheels:“<<wheels<<“,weight:“<<weight<<endl; 10. 11. protected: 12. int wheels; 13. double weight; 14. ; 15. class Car:private Vehicle 16. 17. public: 18. Car(int wl,double wh,int pl):Vehicle(wl,wh),passager_load(pl); 19. void show() 20. 21. Vehicle:show(); 22. cout<<“passager_load:“<<passager_load<<endl; 23. 24. private: -_25. int passager_load; 26. ; 27. class Truck:private Vehicle 28. 29. public: 30. Truck(int wl,double wh,int pl,double psl):Vehicle(wl,wh),passager_load(pl),payload(psl); 31. void show() 32. 33. Vehicle:show(); 34. cout<<“passager_load:“<<passager_load<<endl; 35. cout<<“payload:“<<payload<<endl; 36. 37. private: 38. int passager_load; 39. double payload; 40. ; 41. int main() 42. 43. Vehicle v1(4,200); 44. v1.show(); 45. cout<<“=“<<endl; 46. Car c1(4,290,10); 47. c1.show(); 48. cout<<“=“<<endl; 49. Truck t1(4,500,50,200); 50. t1.show(); 51. return 0; 52. 结果输出:cpp view plaincopyprint?1.wheels:4,weight:200 2.= 3.wheels:4,weight:290 4.passager_load:10 5.= 6.wheels:4,weight:500 7.passager_load:50 8.payload:200 -_