c++指向对象的指针作函数参数问题

2024-11-28 19:50:35
推荐回答(1个)
回答1:

#include
#include using namespace std;class Studest
{
private:
string number;
float score;
public:
Studest(string s,float d):number(s),score(d){};
void Max(Studest *q);//q也必须为Studest类 };Studest stud[5]={
Studest("125",90),
Studest("126",89),
Studest("124",79),
Studest("122",89),
Studest("127",78)
};void Studest::Max(Studest *q)//q也必须为Studest类
{
Studest *p1;// 指针必须为Studest类
string Max_num;
int Max;
int i;
p1=q;Max=p1->score;
//Max=p1->number;//number为字符型,而Max为int
Max_num=p1->number;for(i=1;i<5;i++){
if((p1+i)->score>Max)
{Max=(p1+i)->score;
Max_num=(p1+i)->number;//是Max_num,not Max_number}
}cout<<"the Max_number:"<}int main()
{
Studest *p;
p=stud;p->Max(p);return 0;
}