作业4.17
#include<iostream>
using namespace std;
class Student
{
public:
void setStu(int num,float s)
{number=num;score=s;}
void disp()
{cout<<number<<" "<<score<<endl;}
private:
int number;
float score;
};
int main()
{
Student stu[6];
Student*p;
stu[0].setStu(21,90);
stu[1].setStu(22,87.5);
stu[2].setStu(23,77);
stu[3].setStu(24,88);
stu[4].setStu(25,90);
stu[5].setStu(26,89);
p=stu;
int i;
for(i=0;i<6;i++,p++)
p->disp();
return 0;
}
作业4.18
#include <iostream>
using namespace std;
class Student
{
public:
Student(int, float);
void max(Student* s);
private:
int num;
float score;
};
Student::Student(int n, float s):num(n),score(s){}
void Student::max(Student* s)
{
int i, max_num=0, max_score=0;
for(i=0;i<6;++i)
if((s+i)->score > max_score)
{max_score=(s+i)->score;
max_num=(s+i)->num;}
cout<<"所有学生中成绩最高学生的学号为:"<<max_num<< endl;}
int main()
{ int i;
Student s[6]={Student(1,98),Student(22,95),Student(23,85),Student(24,89.5),Student(25,88),Student(26,90)};
Student *p;
p=&s[0];
s[i].max(p);
return 0;
}
作业4.19
#include<iostream>
using namespace std;
class book
{
public:
book(int j)
{qu=j;
price=10*qu;}
void disp()
{cout<<"qu*price="<<qu*price<<endl;}
private:
int qu;
float price;
};
int main()
{
int i;
book b1[5]={1,2,3,4,5};
for(i=0;i<5;i++)
{b1[i].disp();}
return 0;
}
作业4.20
#include<iostream>
using namespace std;
class book
{
public:
void setbook(int j)
{qu=j;
price=10*qu;}
void disp()
{cout<<"qu*price="<<qu*price<<endl;}
private:
int qu;
float price;
};
int main()
{ book b1[5];
book *p;
b1[0].setbook(1);
b1[1].setbook(2);
b1[2].setbook(3);
b1[3].setbook(4);
b1[4].setbook(5);
p=b1;
int i;
for(i=0;i<5;i++,p++)
b1[4-i].disp();
return 0;
}