定义一个学生类,其中有3个私有数据成员学号、姓名、成绩,以及若干成员函数实现对学生数据的赋值和输出。

2020-04-25 军事 417阅读
包含友元函数的:
#include
#include
using namespace std;
class Student
{ public:
Student(int x,string y,int z):number(x),name(y),score(z){}
void display();
friend void contrast(Student *a,Student *b,Student *c);
private:
int number;
string name;
int score;
};
void Student::display()
{
cout<<"score="< cout<<"name="< cout<<"number="<}
void Student::contrast(Student *a,Student *b,Student *c)
{
int max;
max=(a->score>b->score )? a->score : b->score;
max=(max > c->score )? max : c->score;
if (max==a->score )
(*a).display();
else if (max==b->score)
(*b).display();
else (*c).display();
}

int main()
{
Student student[3]={Student(1,"wangli",90),Student(2,"dengxin",88),Student(3,"chenchao",82)};
student[0].contrast(&student[0],&student[1],&student[2]);
return 0;
}
不好意思,上面的程序由于友元不支持vc6,我没有调试,但是下面的不包含的仿制品,估计差不多哈。。。
不包含友元函数的:
#include
#include
using namespace std;
class Student
{ public:
Student(int x,string y,int z):number(x),name(y),score(z){}
void display();
int getscore();
void contrast(Student *a,Student *b,Student *c);
private:
int number;
string name;
int score;
};
void Student::display()
{
cout<<"score="< cout<<"name="< cout<<"number="<}
int Student::getscore()
{
return score;
}
void Student::contrast(Student *a,Student *b,Student *c)
{
int max;
max=(a->getscore()>b->getscore() )? a->getscore() : b->getscore();
max=(max > c->getscore() )? max : c->getscore();
if (max==a->getscore() )
(*a).display();
else if (max==b->getscore() )
(*b).display();
else (*c).display();
}

int main()
{
Student student[3]={Student(1,"wangli",90),Student(2,"dengxin",88),Student(3,"chenchao",82)};
student[0].contrast(&student[0],&student[1],&student[2]);
return 0;
}
声明:你问我答网所有作品(图文、音视频)均由用户自行上传分享,仅供网友学习交流。若您的权利被侵害,请联系fangmu6661024@163.com