C++第三章

来源:互联网 发布:淘宝优质卖家 编辑:程序博客网 时间:2024/06/15 23:48
  1. #include <iostream>  
  2. using namespace std;  
  3. class Date  
  4. {  
  5. public:  
  6. Date(int=1,int=1,int=2005);  
  7. void display();  
  8. private:  
  9. int month;  
  10. int day;  
  11. int year;  
  12. };  
  13. Date::Date(int m,int d,int y):month(m),day(d),year(y){}  
  14. void Date::display()  
  15. {  
  16.  cout<<month<<"/"<<day<<"/"<<year<<endl;  
  17. }  
  18. int main()  
  19. {  
  20.  Date d1(10,13,2005);  
  21.  Date d2(12,30);  
  22.  Date d3(10);  
  23.  Date d4;  
  24.  d1.display();  
  25.  d2.display();  
  26.  d3.display();  
  27.  d4.display();  
  28.  return 0;  
  29. }  


第四题

[html] view plaincopy
  1. #include <iostream>  
  2. using namespace std;  
  3. class Student  
  4. {  
  5. public:  
  6. Student(int n,int s):num(n),score(s){}  
  7. void display();  
  8. private:  
  9. int num;  
  10. int score;  
  11. };  
  12. void Student::display()  
  13. {  
  14.  cout<<num<<" "<<score<<endl;  
  15. }  
  16. int main()  
  17. {  
  18. Student stud[5]={Student(1,88),Student(2,86),Student(3,90),Student(4,78),Student(5,88)};  
  19. Student *p=stud;  
  20. for(int i=0;i<3;p+=2,i++)  
  21. p->display();  
  22.  return 0;  
  23. }  



第五题

[html] view plaincopy
  1. #include<iostream>  
  2. using namespace std;  
  3. class Student {  
  4. public:  
  5. Student(int n,int s):num(n),score(s){};  
  6.   
  7.   
  8.   
  9. int num;  
  10. int score;  
  11. };  
  12. void stu_max(Student *p){  
  13. int i=0,k=0,max=p[0].score;  
  14. for(i;i<5;i++){  
  15. if(p[i].score>max)  
  16. {  
  17. max=p[i].score  
  18. ;k++;}  
  19.   
  20. }  
  21.   
  22. cout<<"最高分数是"<<p[k].score<<"学号是"<<p[k].num;   
  23. }  
  24.   
  25.   
  26.   
  27.   
  28. int main(){  
  29.   
  30. Student std[5]={Student(1,88),Student  
  31.   
  32.   
  33. (2,86),Student  
  34.   
  35.   
  36. (3,90),Student(4,78),Student(5,88)};  
  37. Student *p=std;  
  38. stu_max(p);  
  39. return 0;  
  40.   
  41. }  



第六题

[html] view plaincopy
  1. #include <iostream>  
  2. using namespace std;  
  3. class Student  
  4. {  
  5. public:  
  6. Student(int n,float s):num(n),score(s){}  
  7. void change(int n,float s)  
  8. {  
  9. num=n;  
  10. score=s;  
  11. }  
  12. void display()  
  13. {  
  14. cout<<num<<" "<<score<<endl;  
  15. }  
  16. private:  
  17. int num;  
  18. float score;  
  19. };  
  20. int main()  
  21. {  
  22.  Student stud(101,78.5);  
  23.  stud.display();  
  24.  stud.change(101,80.5);  
  25.  stud.display();  
  26.  return 0;  
  27. }  

第七题
[html] view plaincopy
  1. #include <iostream>  
  2. using namespace std;  
  3. class Student  
  4. {  
  5. public:  
  6. Student(int n,float s):num(n),score(s){}  
  7. void change(int n,float s)  
  8. {  
  9. num=n;  
  10. score=s;  
  11. }  
  12. //void display()  
  13. void display()const  
  14. {  
  15. cout<<num<<" "<<score<<endl;  
  16. }  
  17. private:  
  18. int num;  
  19. float score;  
  20. };  
  21. int main()  
  22. {  
  23.    
  24.  const Student stud(101,78.5);  
  25.  stud.display();  
  26.    
  27.  return 0;  
  28. }  

[html] view plaincopy
  1.  #include <iostream>  
  2. using namespace std;  
  3. class Student  
  4. {  
  5. public:  
  6. Student(int n,float s):num(n),score(s){}  
  7. //void change(int n,float s)  
  8. void change(int n,float s)const  
  9. {  
  10. num=n;  
  11. score=s;  
  12. }  
  13. //void display()  
  14. void display()const  
  15. {  
  16. cout<<num<<" "<<score<<endl;  
  17. }  
  18. private:  
  19. //int num;  
  20. mutable int num;//将变量变为可改变的值  
  21. //float score;  
  22. mutable float score;  
  23. };  
  24. int main()  
  25. {  
  26.  //Student stud(101,78.5);  
  27.  const Student stud(101,78.5);  
  28.  stud.display();  
  29.  stud.change(101,80.5);  
  30.  stud.display();  
  31.  return 0;  
  32. }  


[html] view plaincopy
  1. class Student  
  2. {  
  3. public:  
  4. Student(int n,float s):num(n),score(s){}  
  5. void change(int n,float s)  
  6. {  
  7. num=n;  
  8. score=s;  
  9. }  
  10. void display()  
  11. {  
  12. cout<<num<<" "<<score<<endl;  
  13. }  
  14. private:  
  15. int num;  
  16. float score;  
  17. };  
  18. int main()  
  19. {  
  20.  Student stud(101,78.5);  
  21.  Student *p=&stud;  
  22.  p->display();  
  23.  p->change(101,80.5);  
  24.  p->display();  
  25.  return 0;  
  26. }  


[html] view plaincopy
  1.  #include <iostream>  
  2. using namespace std;  
  3. class Student  
  4. {  
  5. public:  
  6. Student(int n,float s):num(n),score(s){}  
  7. //void change(int n,float s)  
  8. void change(int n,float s)const  
  9. {  
  10. num=n;  
  11. score=s;  
  12. }  
  13. //void display()  
  14. void display()const  
  15. {  
  16. cout<<num<<" "<<score<<endl;  
  17. }  
  18. private:  
  19. //int num;  
  20. mutable int num;  
  21. //float score;  
  22. mutable float score;  
  23. };  
  24. int main()  
  25. {  
  26.  //Student stud(101,78.5);  
  27.  const Student stud(101,78.5);  
  28.  stud.display();  
  29.  const Student *p = &stud;  
  30.  //stud.change(101,80.5);  
  31.  p->change(101,80.5);  
  32.  //stud.display();  
  33.  p->display();  
  34.  return 0;  
  35. }  


[html] view plaincopy
  1. #include <iostream>  
  2. using namespace std;  
  3. class Student  
  4. {  
  5. public:  
  6. Student(int n,float s):num(n),score(s){}  
  7. //void change(int n,float s)  
  8. void change(int n,float s)const  
  9. {  
  10. num=n;  
  11. score=s;  
  12. }  
  13. //void display()  
  14. void display()const  
  15. {  
  16. cout<<num<<" "<<score<<endl;  
  17. }  
  18. private:  
  19. //int num;  
  20. mutable int num;  
  21. //float score;  
  22. mutable float score;  
  23. };  
  24. int main()  
  25. {  
  26.  //Student stud(101,78.5);  
  27.  const Student stud(101,78.5);  
  28.  stud.display();  
  29.  const Student *p = &stud;  
  30.  //stud.change(101,80.5);  
  31.  p->change(101,80.5);  
  32.  //stud.display();  
  33.  p->display();  
  34.  return 0;  
  35. }  


 第八题

[html] view plaincopy
  1. #include <iostream>  
  2. using namespace std;  
  3. class Student  
  4. {  
  5. public:  
  6. Student(int n,float s):num(n),score(s){}  
  7. void change(int n,float s)  
  8. {  
  9. num=n;  
  10. score=s;  
  11. }  
  12. void display()  
  13. {  
  14. cout<<num<<" "<<score<<endl;  
  15. }  
  16. private:  
  17. int num;  
  18. float score;  
  19. };  
  20. int main()  
  21. {  
  22.  Student stud(101,78.5);  
  23.  void fun(Student &);  
  24.  fun(stud);  
  25.  return 0;  
  26. }  
  27. void fun(Student &stu)  
  28. {  
  29.  stu.display();  
  30.  stu.change(101,80.5);  
  31.  stu.display();  
[html] view plaincopy
  1. }  

第九题

[html] view plaincopy
  1. #include <iostream>  
  2. using namespace std;  
  3. class Product  
  4. {  
  5. public:  
  6. Product(int n,int q,float p):num(n),quantity(q),price(p){}  
  7. void total();  
  8. static float average();  
  9. static void display();  
  10. private:  
  11. int num;  
  12. int quantity;  
  13. float price;  
  14. static float discount;  
  15. static float sum;  
  16. static int n;  
  17. };  
  18. void Product::total()  
  19. {  
  20.  float rate=1.0;  
  21.  if(quantity>10)  
  22. rate=rate*0.98;  
  23.  sum=sum+quantity*price*rate*(1-discount);  
  24.  n=n+quantity;  
  25. }  
  26. void Product::display()  
  27. {  
  28.  cout<<sum<<endl;  
  29.  cout<<average()<<endl;  
  30. }  
  31. float Product::average()  
  32. {  
  33.  return (sum/n);  
  34. }  
  35. float Product::discount=0.05;  
  36. float Product::sum=0.0;  
  37. int Product::n=0;  
  38. int main()  
  39. {  
  40. Product prod[3]={Product(101,5,23.5),Product  
  41.   
  42.   
  43. (102,12,24.56),Product(103,100,21.5)};  
  44. for(int i=0;i<3;i++)  
  45. prod[i].total();  
  46. Product::display();  
  47. return 0;  
  48. }  


第十题

[html] view plaincopy
  1. #include <iostream>  
  2. using namespace std;  
  3. class Date;  
  4. class Time  
  5. {  
  6. public:  
  7. Time(int ,int ,int );  
  8. friend void display(const Date &,const Time &);  
  9. private:  
  10. int hour;  
  11. int minute;  
  12. int sec;  
  13. };  
  14. Time::Time(int h,int m,int s)  
  15. {  
  16.  hour=h;  
  17.  minute=m;  
  18.  sec=s;  
  19. }  
  20. class Date  
  21. {  
  22. public:  
  23. Date(int ,int ,int );  
  24. friend void display(const Date &,const Time &);  
  25. private:  
  26. int day;  
  27. int month;  
  28. int year;  
  29. };  
  30. Date::Date(int d,int m,int y)  
  31. {  
  32.  day=d;  
  33.  month=m;  
  34.  year=y;  
  35. }  
  36. void display(const Date &d,const Time &t)  
  37. {  
  38.  cout<<d.day<<" "<<d.month<<" "<<d.year<<endl;  
  39.  cout<<t.hour<<" "<<t.minute<<" "<<t.sec<<endl;  
  40. }  
  41. int main()  
  42. {  
  43.  Time t(10,13,56);  
  44.  Date d(10,25,2004);  
  45.  display(d,t);  
  46.  return 0;  
  47. }  



第十一题

[html] view plaincopy
  1. #include <iostream>  
  2. using namespace std;  
  3. class Time;  
  4. class Date  
  5. {  
  6. public:  
  7. Date(int ,int ,int);  
  8. friend Time;  
  9. private:  
  10. int day;  
  11. int month;  
  12. int year;  
  13. };  
  14. Date::Date(int d,int m,int y)  
  15. {  
  16.  int day=d;  
  17.  int month=m;  
  18.  int year=y;  
  19. }  
  20. class Time  
  21. {  
  22. public:  
  23. Time(int,int,int);  
  24. void display(const Date &);  
  25. private:  
  26. int hour;  
  27. int minute;  
  28. int sec;  
  29. };  
  30. Time::Time(int h,int m,int s)  
  31. {  
  32.  hour=h;  
  33.  minute=m;  
  34.  sec=s;  
  35. }  
  36. void Time::display(const Date &d)  
  37. {  
  38.  cout<<d.day<<" "<<d.month<<" "<<d.year<<endl;  
  39.  cout<<hour<<" "<<minute<<" "<<sec<<endl;  
  40. }  
  41. int main()  
  42. {  
  43.  Time t(10,13,56);  
  44.  Date d(12,25,2004);  
  45.  t.display(d);  
  46.  return 0;  
  47. }  


第十二题

[html] view plaincopy
  1. #include <iostream>  
  2. using namespace std;  
  3. template<class numtype>  
  4. class Compare  
  5. {  
  6.  public:  
  7. Compare(numtype a,numtype b);  
  8. numtype max();  
  9. numtype min();  
  10.  private:  
  11. numtype x,y;  
  12. };  
  13. template<class numtype>  
  14. Compare<numtype>::Compare(numtype a,numtype b)  
  15. {  
  16.  x=a;  
  17.  y=b;  
  18. }  
  19. template<class numtype>  
  20. numtype Compare<numtype>::max()  
  21. {  
  22. return (x>y)?x:y;  
  23. }  
  24. template<class numtype>  
  25. numtype Compare<numtype>::min()  
  26. {  
  27.     return (x<y)?x:y;  
  28. }  
  29. int main()  
  30. {  
  31. Compare<int> cmp1(3,7);  
  32. cout<<cmp1.max()<<"最大."<<endl;  
  33. cout<<cmp1.min()<<"最小."<<endl;  
  34. Compare<float> cmp2(45.78,93.6);  
  35. cout<<cmp2.max()<<"最大."<<endl;  
  36. cout<<cmp2.min()<<"最小."<<endl;  
  37. Compare<char> cmp3('a','A');  
  38. cout<<cmp3.max()<<"最大."<<endl;  
  39. cout<<cmp3.min()<<"最小."<<endl;  
  40.   
  41.   
  42. }  


0 0
原创粉丝点击