c++面向对象实例题集锦_第1页
c++面向对象实例题集锦_第2页
c++面向对象实例题集锦_第3页
c++面向对象实例题集锦_第4页
c++面向对象实例题集锦_第5页
已阅读5页,还剩19页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

最新文档

评论

0/150

提交评论