###第九章作业
1第九章课后习题5:建立一个对象数组,内放5个学生数据,设立一个函数max。
#include<iostream>
using namespace std;
class Student
{
public:
Student(int n,int m)
{
num=n;
score=m;
}
void display();
int score;
int num;
};
void Student:: display()
{
cout<<num<<" "<<score<<endl;
}
void max(Student *a);
int main()
{
Student stud[5]={
Student(1001,80),
Student(1002,85),
Student(1003,87),
Student(1004,90),
Student(1005,85)};
Student *p;
p=&stud[0];
for(p=stud;p<stud+5;p++)
{
p->display();
}
Student *p1;
p1=&stud[0];
max(p1);
return 0;
}
void max(Student *a)
{
int i,k;
int max;
max=a[0].score;
for(i=0;i<5;i++)
if(a[i].score>max)
{max=a[i].score;
k=i;}
cout<<"the highest score is "<<max<<endl;
cout<<"the number of highester is "<<a[k].num<<endl;
}
这个程序不是自己写的,我对对象数组的使用,对象指针如何指向对象以及如何用指针赋值并在函数里返回指针,用指针做函数形参这类题还掌握不够,仅仅留在会看阶段,需要加强练习。
2第九章课后习题8:修改第6题程序,增加一个fun函数,编写main函数。
#include <iostream.h>
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 &stu)
{
stu.display();
stu.change(101,80.5);
stu.display();}
int main ()
{student stu1(101,78.5);
fun(stu1);
return 0;
}
3第九章课后习题9:商店销售某一商品,每天公布统一的折扣
#include<iostream>
using namespace std;
class product
{
public:
product(int n,int q,float p):num(n),quantity(q),price(p){}
static float average();
static void display();
void total();
private:
int num;
int quantity;
float price;
static float sum;
static int n;
};
void product::total()
{
n=n+quantity;
sum=sum+quantity*price;
}
int product::n=0;
float product::sum=0;
float product::average()
{
return sum/n;
}
void product::display()
{
cout<<"the sum of today is:"<<sum<<endl;
cout<<"the average price of today is: "<<product::average()<<endl;
}
int main()
{ product pro[]={
product (101,5,23.5),
product (102,12,24.56),
product (103,100,21.5)};
for( int i=0;i<3;i++)
pro[i].total();
product::display();
return 0;
}
4课后习题9.11:将例9.13中的time声明为date类的友元类
#include<iostream>
using namespace std;
class time;
class date
{
public:
date(int,int,int);
void display(time&);
private:
int year;
int month;
int day;
};
class time
{
public:
time(int,int,int);
friend void date::display(time&);
private:
int hour;
int minute;
int second;
};
date::date(int y,int m,int d)
{year=y;month=m;day=d;};
time::time(int h,int m,int s)
{hour=h;minute=m;second=s;};
void date::display(time&t)
{cout<<t.hour<<":"<<t.minute<<":"<<t.second<<endl;
cout<<year<<"/"<<month<<"/"<<day<<endl;};
int main()
{
time t1(10,13,56);
date d1(2005,12,24);
d1.display(t1);
return 0;
}
5.课后习题9.12:将例9.14改写为类模版外定义。
#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>
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 maxinum of two integer numbers."<<endl;
cout<<cmp1.min()<<" "<<"is the mininum of two integer numbers."<<endl;
compare<float>cmp2(12.9,18.6);
cout<<cmp2.max()<<" "<<"is the maxinum of two float numbers."<<endl;
cout<<cmp2.min()<<" "<<"is the mininum of two float numbers."<<endl;
compare<char>cmp3('a','A');
cout<<cmp3.max()<<" "<<"is the maxinum of two characters"<<endl;
cout<<cmp3.min()<<" "<<"is the mininum of two characters"<<endl;
return 0;
}