4.21
#include<iostream>
#include<string>
using namespace std;
class student{
public:
student(int num1,string name1,double score1)
{
num=num1;
name=name1;
score=score1;
}
void showcj()
{
cout<<"学号:"<<num<<'\t';
cout<<"姓名:"<<name<<" ";
cout<<"分数:"<<score<<'\t';
cout<<endl;
}
void zongscore()
{
zscore+=score;
count++;
}
static void average()
{
double aver;
aver=zscore/count;
cout<<"学生人数为:"<<count<<endl;
cout<<"平均分为:"<<aver<<endl;
}
private:
int num;
string name;
double score;
static double zscore;
static int count;
};
double student::zscore=0.0;
int student::count=0;
int main()
{
student s1(1,"wy",94),s2(2,"tz",99),s3(3,"sc",94);
s1.showcj();s1.zongscore();
s2.showcj();s2.zongscore();
s3.showcj();s3.zongscore();
student::average();
system("pause");
return 0;
}
4.22
#include<iostream>
#include<cmath>
using namespace std;
class location
{
public:
location(double X,double Y)
{
x=X;
y=Y;
}
void show()
{
cout<<"第"<<A<<"点的坐标为:("<<x<<","<<y<<")"<<endl;
A=A+1;
}
friend double dist(location& d1,location& d2);
private:
double x,y;
static char A;
};char location::A='A';
double dist(location& d1,location& d2)
{
double L;
L=sqrt((d1.x-d2.x)*(d1.x-d2.x)+(d1.y-d2.y)*(d1.y-d2.y));
return L;
}
int main()
{
location A(3,2),B(1,1);
A.show();
B.show();
cout<<"两点间的距离为"<<dist(A,B)<<endl;
system("pause");
return 0;
}