c++第三次作业

来源:互联网 发布:mysql frm hy000 编辑:程序博客网 时间:2024/06/05 12:39
3.#include <iostream>  using namespace std;  class Date  {  public:      Date(int=1,int=1,int=2005);      void display();  private:      int month;      int day;      int year;  };  Date::Date(int m,int d,int y):month(m),day(d),year(y){}  void Date::display()  {   cout<<month<<":"<<day<<":"<<year<<endl;  }  int main()  {   Date d1(10,13,2005);   Date d2(12,30);   Date d3(10);   Date d4;   d1.display();   d2.display();   d3.display();   d4.display();   return 0;  }    4. #include <iostream>  using namespace std;  class Student  {  public:      Student(int n,int s):num(n),score(s){}      void display();  private:      int num;      int score;  };  void Student::display()  {   cout<<num<<" "<<score<<endl;  }  int main()  {      Student stud[5]={Student(1,98),Student(2,97),Student(3,96),Student(4,95),Student(5,94)};  Student *p=stud;  for(i=0;i<3;p=+2,i++)      p->display();   return 0;  }    5. #include <iostream>  using namespace std;  class Student  {  public:      Student(int n,int s):num(n),score(s){}      int num;      int score;  };  void max(Student *a)  {   int i,k=0,max_score=a[0].score;   for(i=1;i<5;i++)   {    if(a[i].score>max_score)    {max_score=a[i].score;k++;}   }  cout<<a[k].num<<" "<<max_score<<endl;  }  int main()  {        Student stud[5]={Student(1,98),Student(2,97),Student(3,96),Student(4,95),Student(5,94)};      Student *p=stud;      max(p);      return 0;  }    6. #include <iostream>  using namespace std;  class Student  {  public:      Student(int n,float s):num(n),score(s){}      void change(int n,float s)      {          num=n;          score=s;      }      void display()      {       cout<<num<<" "<<score<<endl;      }  private:      int num;      float score;  };  int main()  {   Student stud(101,78.5);   stud.display();   stud.change(101,80.5);   stud.display();   return 0;  }    7.(1) #include <iostream>  using namespace std;  class Student  {  public:      Student(int n,float s):num(n),score(s){}      void change(int n,float s)      {          num=n;          score=s;      }      //void display()      void display()const      {       cout<<num<<" "<<score<<endl;      }  private:      int num;      float score;  };  int main()  {   //Student stud(101,78.5);   const Student stud(101,78.5);   stud.display();   //stud.change(101,80.5);   //stud.display();   return 0;  }    (2) #include <iostream>  using namespace std;  class Student  {  public:      Student(int n,float s):num(n),score(s){}      //void change(int n,float s)      void change(int n,float s)const      {          num=n;          score=s;      }      //void display()      void display()const      {       cout<<num<<" "<<score<<endl;      }  private:      //int num;      mutable int num;      //float score;      mutable float score;  };  int main()  {   //Student stud(101,78.5);   const Student stud(101,78.5);   stud.display();   stud.change(101,80.5);   stud.display();   return 0;  }  (3) #include <iostream>  using namespace std;  class Student  {  public:      Student(int n,float s):num(n),score(s){}      void change(int n,float s)      {          num=n;          score=s;      }      void display()      {       cout<<num<<" "<<score<<endl;      }  private:      int num;      float score;  };  int main()  {   Student stud(101,78.5);   Student *p=&stud;   p->display();   p->change(101,80.5);   p->display();   return 0;  }    (4) #include <iostream>  using namespace std;  class Student  {  public:      Student(int n,float s):num(n),score(s){}      //void change(int n,float s)      void change(int n,float s)const      {          num=n;          score=s;      }      //void display()      void display()const      {       cout<<num<<" "<<score<<endl;      }  private:      //int num;      mutable int num;      //float score;      mutable float score;  };  int main()  {   //Student stud(101,78.5);   const Student stud(101,78.5);   stud.display();   const Student *p = &stud;   //stud.change(101,80.5);   p->change(101,80.5);   //stud.display();   p->display();   return 0;  }    (5)#include <iostream>  using namespace std;  class Student  {  public:      Student(int n,float s):num(n),score(s){}      //void change(int n,float s)      void change(int n,float s)const      {          num=n;          score=s;      }      //void display()      void display()const      {       cout<<num<<" "<<score<<endl;      }  private:      //int num;      mutable int num;      //float score;      mutable float score;  };  int main()  {   //Student stud(101,78.5);   const Student stud(101,78.5);   stud.display();   const Student *p = &stud;   //stud.change(101,80.5);   p->change(101,80.5);   //stud.display();   p->display();   return 0;  }    (5)#include <iostream>  using namespace std;  class Student  {  public:      Student(int n,float s):num(n),score(s){}      //void change(int n,float s)      void change(int n,float s)const      {          num=n;          score=s;      }      //void display()      void display() const      {       cout<<num<<" "<<score<<endl;      }  private:      //int num;      mutable int num;      //float score;      mutable float score;  };  int main()  {   //Student stud(101,78.5);   const Student stud(101,78.5);   stud.display();   //const Student *p = &stud;   Student * const p=&stud;   //stud.change(101,80.5);   //p->change(101,80.5);   //stud.display();   p->display();   return 0;  }  8. #include <iostream>  using namespace std;  class Student  {  public:      Student(int n,float s):num(n),score(s){}      void change(int n,float s)      {       num=n;       score=s;      }      void display()      {       cout<<num<<" "<<score<<endl;      }  private:      int num;      float score;  };  int main()  {   Student stud(101,78.5);   void fun(Student &);   fun(stud);   return 0;  }  void fun(Student &stu)  {   stu.display();   stu.change(101,80.5);   stu.display();  }    9. #include <iostream>  using namespace std;  class Product  {  public:      Product(int n,int q,float p):num(n),quantity(q),price(p){}      void total();      static float average();      static void display();  private:      int num;      int quantity;      float price;      static float discount;      static float sum;      static int n;  };  void Product::total()  {   float rate=1.0;   if(quantity>10)       rate=rate*0.98;   sum=sum+quantity*price*rate*(1-discount);   n=n+quantity;  }  void Product::display()  {   cout<<sum<<endl;   cout<<average()<<endl;  }  float Product::average()  {   return (sum/n);  }  float Product::discount=0.05;  float Product::sum=0.0;  int Product::n=0;  int main()  {      Product prod[3]={Product(101,5,23.5),Product(102,12,24.56),Product(103,100,21.5)};      for(int i=0;i<3;i++)          prod[i].total();      Product::display();      return 0;  }    10. #include <iostream>  using namespace std;  class Date;  class Time  {  public:      Time(int ,int ,int );      friend void display(const Date &,const Time &);  private:      int hour;      int minute;      int sec;  };  Time::Time(int h,int m,int s)  {   hour=h;   minute=m;   sec=s;  }  class Date  {  public:      Date(int ,int ,int );      friend void display(const Date &,const Time &);  private:      int day;      int month;      int year;  };  Date::Date(int d,int m,int y)  {   day=d;   month=m;   year=y;  }  void display(const Date &d,const Time &t)  {   cout<<d.day<<" "<<d.month<<" "<<d.year<<endl;   cout<<t.hour<<" "<<t.minute<<" "<<t.sec<<endl;  }  int main()  {   Time t(10,13,56);   Date d(10,25,2004);   display(d,t);   return 0;  }    11.#include <iostream>  using namespace std;  class Time;  class Date  {  public:      Date(int ,int ,int);      friend Time;  private:      int day;      int month;      int year;  };  Date::Date(int d,int m,int y)  {   int day=d;   int month=m;   int year=y;  }  class Time  {  public:      Time(int,int,int);      void display(const Date &);  private:      int hour;      int minute;      int sec;  };  Time::Time(int h,int m,int s)  {   hour=h;   minute=m;   sec=s;  }  void Time::display(const Date &d)  {   cout<<d.day<<" "<<d.month<<" "<<d.year<<endl;   cout<<hour<<" "<<minute<<" "<<sec<<endl;  }  int main()  {   Time t(10,13,56);   Date d(12,25,2004);   t.display(d);   return 0;  }    12. #include <iostream>  using namespace std;  template<class numtype>  class Compare  {   public:       Compare(numtype a,numtype b);       numtype max();       numtype min();   private:       numtype x,y;  };  template<class numtype>  Compare<numtype>::Compare(numtype a,numtype b)  {   x=a;   y=b;  }  template<class numtype>  numtype Compare<numtype>::max()  {      return (x>y)?x:y;  }  template<class numtype>  numtype Compare<numtype>::min()  {      return (x<y)?x:y;  }  int main()  {  Compare<int> cmp1(3,7);  cout<<cmp1.max()<<"is the Maximum of two integer numbers."<<endl;  cout<<cmp1.min()<<"is the Minimum of two integer numbers."<<endl;  Compare<float> cmp2(45.78,93.6);  cout<<cmp2.max()<<"is the Maximum of two integer numbers."<<endl;  cout<<cmp2.min()<<"is the Minimum of two integer numbers."<<endl;  Compare<char> cmp3('a','A');  cout<<cmp3.max()<<"is the Maximum of two characters."<<endl;  cout<<cmp3.min()<<"is the Minimum of two characters."<<endl;    }  

0 0
原创粉丝点击