【4.17】建立一个对象数组,内放六个学生的数据(学号、成绩)、用指针指向数组首元素。输入2,4,6个学生的数据
#include<iostream>
using namespace std;
class student{
public:
void stud(double a,double b);
void disp();
private:
double number;
double score;
};
void student::stud(double a,double b)
{number=a;
score=b;}
void student::disp()
{
cout<<"nmuber:"<<number<<endl;
cout<<"score:"<<score<<endl;
}
int main()
{student stu[6];
student *pr;
stu[0].stud(10,10);
stu[1].stud(20,20);
stu[2].stud(30,30);
stu[3].stud(40,40);
stu[4].stud(50,50);
stu[5].stud(60,60);
pr=stu;
pr++;
pr-> disp();
pr++;pr++;
pr-> disp();
pr++;pr++;
pr-> disp();
system("pause");
return 0;
}
【4.18】
建立一个对象数组,内放6个学生的数据(学号、成绩),设立一个函数max,使用对象指针作为函数参数,在max函数中找出6个学生中成绩最高者,并输出学号
#include <iostream>
using namespace std;
class student{
public:
student (double n,double s)
{ number=n;
score=s;}
private:
double number;
double score;
friend void max(student*t);
};
void max(student*t)
{double m=t[0].score;int x;
for(int i=0;i<6;i++)
if(m<=t[i].score)
{m=t[i].score;x=i;}
cout<<"成绩最高者的学员为:"<<t[x].number<<endl;
cout<<"成绩最高者的成绩为:"<<m<<endl;
};
int main()
{student stu[6]={student(1,99),student(2,98),student(3,97),student(4,96),student(5,95),student(6,96)};
max(stu);
system("pause");
return 0;
}
【4.19】
构建一个类book,其中含有两个私有数据成员qu和price,建立一个有5个元素的数组对象,将qu初始化为1~5,将price初始化为qu的10倍。显示每个对象的qu*price
#include<iostream>
#include<string>
using namespace std;
class book{
public:
book(int a)
{ qu=a;
price=a*10;}
void show()
{double b;
b=qu*price;
cout<<"qu*price="<<b<<endl;}
private:
double qu;
double price;
};
int main()
{book boo[5]={book(1),book(2),book(3),book(4),book(5)};
for(int i=0;i<5;i++)
{boo[i].show();}
system("pause");
return 0;
}
【4.20】
修改题4.19,通过对象指针访问对象数组,使程序以相反的顺序显示对象数组的qu*price
#include<iostream>
using namespace std;
class book{
public:book(int a )
{ qu=a;
price=a*10;}
void show()
{int b;
b=qu*price;
cout<<"qu*price="<<b<<endl;}
private:
int qu;
int price;
};
int main()
{
book boo[5]={book(1),book(2),book(3),book(4),book(5)};
for(int i=0;i<5;i++)
{boo[4-i].show();}
system("pause");
return 0;
}
【4.21】
编写一个程序,已有若干学生的数据,包括学号、姓名、成绩、要求输出这些学生的数据并计算出学生人数和平均成绩(要求将学生人数和总成绩用静态数据成员表示)
#include<iostream>
#include<string>
using namespace std;
class student{
public:
student(string name1,float score1);
void show();
void show_sum_ave();
private:
string name;
float score;
static float sum;
static int count;
static float ave;
};
student::student(string name1, float score1)
{name=name1;
score=score1;
++count;
sum=sum+score;
ave=sum/count;
}
void student::show()
{cout<<"姓名"<<name<<endl;
cout<<"成绩"<<score<<endl;
}
void student::show_sum_ave()
{
cout<<"学生人数"<<count<<endl;
cout<<"累加成绩"<<sum<<endl;
cout<<"平均成绩"<<ave<<endl;
}
int student::count=0;
float student::sum=0.0;
float student::ave=0.0;
int main()
{student stu1("luxingshan",100);
stu1.show();
stu1.show_sum_ave();
student stu2("tansy",98);
stu2.show();
stu2.show_sum_ave();
system("pause");
return 0;
}
【4.22】设计一个点类,其中包括一对坐标点数据成员、一个求两点之间距离的友元函数dist和显示坐标点的成员函数,并用数据进行测试
#include<iostream>
#include<cmath>
using namespace std;
class point{
public:
point(double a,double b)
{x=a;
y=b; }
friend void distance(point&a,point&b);
void show()
{cout<<"坐标为::"<<"("<<x<<","<<y<<")"<<endl;}
private:
double x;
double y;
};
void distance(point&a,point&b)
{double c=sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
cout<<"两点间距离为"<<c<<endl;}
int main()
{point point1(2,2);
point point2(2,1);
point1.show();
point2.show();
distance(point1,point2);
system("pause");
return 0;
}