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(11,90);
stu[1].setstu(12,90.5);
stu[2].setstu(13,91);
stu[3].setstu(14,92);
stu[4].setstu(15,93);
stu[5].setstu(16,89.5);
p=stu;
p=p+1;
for(int i=1;i<=3;p=p+2,i++)
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(11,88),Student(12,91),Student(13,89),Student(14,92.5),Student(15,96),Student(16,92)};
Student *p;
p=&s[0];
s[i].max(p);
return 0;
}
4.19
#include<iostream>
using namespace std;
class book
{
public:
book(int i)
{
qu=i;
price=qu*10;
}
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 i)
{
qu=i;
price=qu*10;
}
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;
}
4.21
#include<iostream>
#include<string>
using namespace std;
class Student
{
public:
Student(string name1,int number1,float score1);
void show();
void show_count_sum_ave();
private:
string name;
int number;
float score;
static int count;
static float sum;
static float ave;
};
Student::Student(string name1,int number1,float score1)
{
name=name1;
number=number1;
score=score1;
count++;
sum+=score;
ave=sum/count;
}
void Student::show()
{
cout<<"name:"<<name<<endl;
cout<<"number:"<<number<<endl;
cout<<"score:"<<score<<endl;}
void Student::show_count_sum_ave()
{
cout<<"count:"<<count<<endl;
cout<<"sum:"<<sum<<endl;
cout<<"ave:"<<ave<<endl;
}
int Student::count=0;
float Student::sum=0.0;
float Student::ave=0.0;
int main()
{Student stu1("Xiao",30,89);
stu1.show();
stu1.show_count_sum_ave();
Student stu2("Yuan",33,91);
stu2.show();
stu2.show_count_sum_ave();
Student stu3("Chen",2,82.5);
stu3.show();
stu3.show_count_sum_ave();
return 0;
}
4.22
#include <iostream>
#include <cmath>
using namespace std;
class Point
{
public:
Point(float x1, float y1):x(x1),y(y1){}
void showpoint()
{
cout << "(x,y)=(" <<x << "," << y << ")" << endl;
}
friend float dist(Point &, Point &);
private:
float x;
float y;
};
float dist(Point & A, Point & B)
{
return sqrt((A.x-B.x)*(A.x-B.x)+(A.y-B.y)*(A.y-B.y));
}
int main()
{
Point a(1.2, 2);
Point b(1.4, 3);
a.showpoint();
b.showpoint();
cout << "a与b间的距离是:" << dist(a, b) <<endl;
return 0;
}