定义一个学生类,其中有3个私有数据成员学号、姓名、成绩,以及若干成员函数实现对学生数据的赋值和输出。 20
在线等,谢谢要求(1)在主函数中定义一个对象数组,完成对数组中每个对象的赋值。(2)定义一个友元函数找出成绩最高的学生的学号和姓名,该函数的形式参数为对象引用。...
在线等,谢谢
要求 (1)在主函数中定义一个对象数组,完成对数组中每个对象的赋值。
(2)定义一个友元函数找出成绩最高的学生的学号和姓名,该函数的形式参数为对象引用。 展开
要求 (1)在主函数中定义一个对象数组,完成对数组中每个对象的赋值。
(2)定义一个友元函数找出成绩最高的学生的学号和姓名,该函数的形式参数为对象引用。 展开
3个回答
展开全部
包含友元函数的:
#include <iostream>
#include <string>
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="<<score<<endl;
cout<<"name="<<name<<endl;
cout<<"number="<<number<<endl;
}
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 <iostream>
#include <string>
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="<<score<<endl;
cout<<"name="<<name<<endl;
cout<<"number="<<number<<endl;
}
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;
}
#include <iostream>
#include <string>
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="<<score<<endl;
cout<<"name="<<name<<endl;
cout<<"number="<<number<<endl;
}
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 <iostream>
#include <string>
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="<<score<<endl;
cout<<"name="<<name<<endl;
cout<<"number="<<number<<endl;
}
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;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
#include <vector>
#include <iostream>
#include <string>
using namespace std;
class stu
{
int no;
string name;
double grade;
public:
stu(int id, string n, double g):no(id), name(n), grade(g) {}
int getid()
{
return no;
}
void setid(int i)
{
no = i;
}
string getname()
{
return name;
}
void setname(const string n)
{
name = n;
}
double getgrade()
{
return grade;
}
void setgrade(double g)
{
grade = g;
}
friend stu & max(vector<stu> & slist);
};
stu & max(vector<stu> & slist)
{
double f = 0;
int no;
for (size_t i = 0; i < slist.size(); ++i)
{
if (slit.grade > f)
{
f = slist.at(i).grade;
no = i;
}
}
return slist.at(i);
}
int main (int argc, char ** argv)
{
vector<stu> slist;
string name;
int id;
double g;
while (cin>>name)
{
cin>>id;
cin>>g;
stu(id, name, g);
slist.push_back(stu);
}
cout<<max(slist).name<<endl;
}
#include <iostream>
#include <string>
using namespace std;
class stu
{
int no;
string name;
double grade;
public:
stu(int id, string n, double g):no(id), name(n), grade(g) {}
int getid()
{
return no;
}
void setid(int i)
{
no = i;
}
string getname()
{
return name;
}
void setname(const string n)
{
name = n;
}
double getgrade()
{
return grade;
}
void setgrade(double g)
{
grade = g;
}
friend stu & max(vector<stu> & slist);
};
stu & max(vector<stu> & slist)
{
double f = 0;
int no;
for (size_t i = 0; i < slist.size(); ++i)
{
if (slit.grade > f)
{
f = slist.at(i).grade;
no = i;
}
}
return slist.at(i);
}
int main (int argc, char ** argv)
{
vector<stu> slist;
string name;
int id;
double g;
while (cin>>name)
{
cin>>id;
cin>>g;
stu(id, name, g);
slist.push_back(stu);
}
cout<<max(slist).name<<endl;
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
using System;
using System.Collections.Generic;
using System.Text;
namespace StudentTest
{
class Program
{
static Student[] students = new Student[2];
static void Main(string[] args)
{
SetArrayValues();
FindBestStudent();
Console.ReadLine();
}
static void SetArrayValues()
{
for (int i = 0; i < students.Length; i++)
{
students[i] = new Student();
}
}
static void FindBestStudent()
{
Student BestStudent= students[0];
for (int i = 0; i < students.Length; i++)
{
if (students[i].FreeScore() > BestStudent.FreeScore())
{
BestStudent = students[i];
}
} Console.WriteLine("成绩最好的学生信息是");
BestStudent.Print();
}
}
class Student
{
private string ID;
private string Name;
private int Score;
public Student()
{
SetValue();
}
public void SetValue()
{
Console.WriteLine("输入编号");
this.ID = Console.ReadLine();
Console.WriteLine("输入姓名");
this.Name = Console.ReadLine();
Console.WriteLine("输入成绩");
this.Score =Convert.ToInt32( Console.ReadLine());
}
public void Print()
{
Console.WriteLine(this.ID);
Console.WriteLine(this.Name);
Console.WriteLine(this.Score.ToString());
}
public int FreeScore()
{
return this.Score;
}
}
}
另外一种和这种差不多
using System;
using System.Collections.Generic;
using System.Text;
namespace StudentScore2
{
class Program
{
static Student[] students = new Student[2];
static void Main(string[] args)
{
SetArrayValues();
FindBestStudent();
Console.ReadKey();
}
static void SetArrayValues()
{
for (int i = 0; i < students.Length; i++)
{
Console.WriteLine("输入编号");
string ID = Console.ReadLine();
Console.WriteLine("输入姓名");
string Name = Console.ReadLine();
Console.WriteLine("输入成绩");
int Score = Convert.ToInt32(Console.ReadLine());
students[i] = new Student(ID,Name,Score);
}
}
static void FindBestStudent()
{
Student BestStudent = students[0];
for (int i = 0; i < students.Length; i++)
{
if (students[i].FreeScore() > BestStudent.FreeScore())
{
BestStudent = students[i];
}
} Console.WriteLine("成绩最好的学生信息是");
BestStudent.Print();
}
}
class Student
{
private string ID;
private string Name;
private int Score;
public Student(string id,string name,int score)
{
this.ID = id;
this.Name = name;
this.Score = score;
}
public void Print()
{
Console.WriteLine(this.ID);
Console.WriteLine(this.Name);
Console.WriteLine(this.Score.ToString());
}
public int FreeScore()
{
return this.Score;
}
}
另外一种和这种差不多
}
希望对你有帮助
using System.Collections.Generic;
using System.Text;
namespace StudentTest
{
class Program
{
static Student[] students = new Student[2];
static void Main(string[] args)
{
SetArrayValues();
FindBestStudent();
Console.ReadLine();
}
static void SetArrayValues()
{
for (int i = 0; i < students.Length; i++)
{
students[i] = new Student();
}
}
static void FindBestStudent()
{
Student BestStudent= students[0];
for (int i = 0; i < students.Length; i++)
{
if (students[i].FreeScore() > BestStudent.FreeScore())
{
BestStudent = students[i];
}
} Console.WriteLine("成绩最好的学生信息是");
BestStudent.Print();
}
}
class Student
{
private string ID;
private string Name;
private int Score;
public Student()
{
SetValue();
}
public void SetValue()
{
Console.WriteLine("输入编号");
this.ID = Console.ReadLine();
Console.WriteLine("输入姓名");
this.Name = Console.ReadLine();
Console.WriteLine("输入成绩");
this.Score =Convert.ToInt32( Console.ReadLine());
}
public void Print()
{
Console.WriteLine(this.ID);
Console.WriteLine(this.Name);
Console.WriteLine(this.Score.ToString());
}
public int FreeScore()
{
return this.Score;
}
}
}
另外一种和这种差不多
using System;
using System.Collections.Generic;
using System.Text;
namespace StudentScore2
{
class Program
{
static Student[] students = new Student[2];
static void Main(string[] args)
{
SetArrayValues();
FindBestStudent();
Console.ReadKey();
}
static void SetArrayValues()
{
for (int i = 0; i < students.Length; i++)
{
Console.WriteLine("输入编号");
string ID = Console.ReadLine();
Console.WriteLine("输入姓名");
string Name = Console.ReadLine();
Console.WriteLine("输入成绩");
int Score = Convert.ToInt32(Console.ReadLine());
students[i] = new Student(ID,Name,Score);
}
}
static void FindBestStudent()
{
Student BestStudent = students[0];
for (int i = 0; i < students.Length; i++)
{
if (students[i].FreeScore() > BestStudent.FreeScore())
{
BestStudent = students[i];
}
} Console.WriteLine("成绩最好的学生信息是");
BestStudent.Print();
}
}
class Student
{
private string ID;
private string Name;
private int Score;
public Student(string id,string name,int score)
{
this.ID = id;
this.Name = name;
this.Score = score;
}
public void Print()
{
Console.WriteLine(this.ID);
Console.WriteLine(this.Name);
Console.WriteLine(this.Score.ToString());
}
public int FreeScore()
{
return this.Score;
}
}
另外一种和这种差不多
}
希望对你有帮助
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询