friend 函数问题
/*【基本题】设计一个学生类(CStudent),它具有私有数据成员是:学号、姓名、数学、外语和计算机课程的成绩。
要求能实现求三门课总成绩和平均成绩,并能设置和显示学生信息 (类声明和成员函数定义分离)。
扩展练习4(学生类设计).设计一个友元函数,按照成绩从高到低的顺序输出姓名、学号和成绩信息。
*/
#include<iostream>
using namespace std;
class CStudent{
private:
int id_,math_,forlan_,computer_;//right type :double;
char *name_;
public:
CStudent(int id=0,char *name=" ",int math=0,int forlan=0,int computer=0):id_(id),name_(name),math_(math),forlan_(forlan),computer_ (computer )
{
}
int getAverage();
int getTotal();
void displayInfor();
void setName(char * name);
void setId(int id);
friend void getRank(CStudent grade[])
{
cout<<"chufa "<<endl;
int num=sizeof(grade)/sizeof(grade[0]);
cout<<"chufa "<<endl;
for(int i=0;i<num-1;i++)//触发不了
{
cout<<"chufa "<<endl;
for(int j=i+1;j<num;j++)
{
if(grade[i].getTotal() < grade[j].getTotal())
{
CStudent temp;
temp=grade[i];
grade[i]=grade[j];
grade[j]=temp;
cout<<i<<endl;
}
}
}
for(int i=0;i<num;i++)
{
grade[i].displayInfor();
}
}
};
int CStudent::getAverage()
{
return (math_+forlan_+computer_ )/3;
}
int CStudent::getTotal ()
{
return math_+forlan_+computer_;
}
void CStudent::displayInfor ()
{
cout<<"name: "<<name_ <<" ID: "<<id_<<" math: "<<math_<<
" foreign langue: "<<forlan_<<" computer: "<<computer_<<endl;
}
void CStudent::setName(char * name)
{
name_=name;
}
void CStudent::setId(int id)
{
id_=id;
}
void main()
{
CStudent *grade=new CStudent[3];
//grade={ CStudent(1,"hys",3,3,3), CStudent(2,"szh",1,1,1), CStudent(3,"dyl",2,2,2)};
grade[0]=CStudent(1,"hys",1,3,3);
grade[1]=CStudent(2,"hys",2,3,3);
grade[2]=CStudent(3,"hys",3,3,3);
getRank(grade);
delete []grade;
}