第三章

来源:互联网 发布:笔记本降温软件 编辑:程序博客网 时间:2024/04/28 05:23

得意

 

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)const
 {}
 void display()const
 {cout << num <<" " << score <<endl;}
private:
 int num;
 float score;
};
int main()
{
 const Student stud(101,78.5);
    stud.display();
    stud.change(102,80.5);
    stud.display();
    return 0;
}

7(2)
#include <iostream>
using namespace std;
class Student
{public:
    Student(int n,float s):num(n),score(s){}
    void change(int n,float s)const
 {num=n;score=s;}
 void display()const
 {cout << num <<" " << score <<endl;}
private:
 mutable int num;
 mutable float score;
};
int main()
{
 const Student stud(101,78.5);
    stud.display();
    stud.change(102,80.5);
    stud.display();
    return 0;
}
7(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;
}
7(4)

#include <iostream>
using namespace std;
class Student
{public:
    Student(int n,float s):num(n),score(s){}
    void change(int n,float s)const
 {num=n;score=s;}
 void display()const
 {cout << num <<" " << score <<endl;}
private:
  mutable int num;
  mutable float score;
};
int main()
{
 const Student stud(101,78.5);
 const Student *p=&stud;
 p->display();
 p->change(99,678);
 p->display();
 return 0;
}
7(5)
#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:
  mutable int num;
  mutable float score;
};
int main()
{
 Student stud(101,78.5);
  Student * const p=&stud;
 p->display();
 p->change(99,678);
 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;
};
void fun(Student &stud)
{
 stud.display();
 stud.change(101,80.5);
 stud.display();
}
int main()
{
 Student stud(101,78.5);
 fun(stud);
 return 0;
}


9.不完整的
#include <iostream>
using namespace std;
class station{
 private:
 public:
  int num;
  int  quantity;
     float price;
  station(int ,int ,float );
     int sum();
  float ave();  
};
station::station(int n,int q,float p)
{
 int num;
 int quantity;
 float price;
}
int station::sum()
{
 return 0;
}/*
static float station::ave()
{
 
}*/
int main()
{
 station s1(101,5,23.5);
 station s2(102,12,24.56);
 station s3(103,100,21.5);
 return 0;
}

10.
#include <iostream>
using namespace std;
class Date;
class Time{
 public:
  Time(int ,int ,int);
  friend void display(Time &);
 private:
  int hour;
  int minute;
  int sec;
};
class Date{
 public:
  Date(int ,int ,int);
  friend void display(Date &);
 private:
  int mouth;
  int day;
  int year;
};
Time::Time(int h,int m,int s)
{
 hour=h;
 minute=m;
 sec=s;
}
void display(Date &d)
{
 cout<<d.mouth<<"/"<<d.day<<"/"<<d.year<<"/"<<endl;
}
void display(Time &d)
{
 cout<<d.hour<<":"<<d.minute<<":"<<d.sec<<endl;
}
Date::Date(int m,int d,int y)
{
 mouth=m;
 day=d;
 year=y;
}
int main()
{
 Time t1(10,13,56);
 Date d1(12,25,2004);
 display(t1);
 display(d1);
 return 0;
}
11.
#include <iostream>
using namespace std;
class Date;
class Time{
 friend Date;
 public:
  Time(int ,int ,int);
  void display(Date &);
 private:
  int hour;
  int minute;
  int sec;
};
class Date{
 public:
  Date(int ,int ,int);
  friend void Time::display(Date &);
 private:
  int mouth;
  int day;
  int year;
};
Time::Time(int h,int m,int s)
{
 hour=h;
 minute=m;
 sec=s;
}
void Time::display(Date &d)
{
 cout<<d.mouth<<"/"<<d.day<<"/"<<d.year<<"/"<<endl;
 cout<<hour<<":"<<minute<<":"<<sec<<endl;
}
Date::Date(int m,int d,int y)
{
 mouth=m;
 day=d;
 year=y;
}
int main()
{
 Time t1(10,13,56);
 Date d1(12,25,2004);
 t1.display(d1);
 return 0;
}

 

12.

#include <iostream>
using namespace std;
template<class numtype>             
class compare//声明类模板
{public:  
compare(numtype,numtype);  
numtype max(); 
 numtype min();
private:
numtype x,y;
};
template<class numtype>
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<<endl;
compare<float> cmp2(45.78,93.6);  
cout<<cmp2.max( )<<" is the Maximum of two float numbers."<<endl;
cout<<cmp2.min( )<<"is the Minimum of two float numbers."<<endl<<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;
return 0;
}

 

0 0
原创粉丝点击