c++指向对象的指针作函数参数问题
题目是这样的:建立一个对象数组,内放五个学生的数据学号成绩,建立一个函数max,用指针向对象的指针作函数参数,在max函数中找出五个学生中成绩最高的并输出其学号.我的代码...
题目是这样的:建立一个对象数组,内放五个学生的数据学号成绩,建立一个函数max,用指针向对象的指针作函数参数,在max函数中找出五个学生中成绩最高的并输出其学号.我的代码如下:错在那里呢#include <iostream>
using namespace std;
class Date
{
public:
Date(int =1,int =1,int=2005 );void display();
private:
int month;
int day;
int year;
};
Date::Date(int m,int d,int y):month(m),day(d),year(y){}
void Date::display()
{
cout<<month<<"/"<<day<<"/"<<year<<endl;
}
int main()
{
Date d1(10,13,2005);
Date d2(12,30,2005);
Date d3(10,1,2005);
Date d4;
d1.display();
d2.display();
d3.display();
d4.display();
return 0; 展开
using namespace std;
class Date
{
public:
Date(int =1,int =1,int=2005 );void display();
private:
int month;
int day;
int year;
};
Date::Date(int m,int d,int y):month(m),day(d),year(y){}
void Date::display()
{
cout<<month<<"/"<<day<<"/"<<year<<endl;
}
int main()
{
Date d1(10,13,2005);
Date d2(12,30,2005);
Date d3(10,1,2005);
Date d4;
d1.display();
d2.display();
d3.display();
d4.display();
return 0; 展开
1个回答
2013-11-06
展开全部
#include <iostream>
#include <string>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:"<<Max_num;//是Max_num,not Max_number
}int main()
{
Studest *p;
p=stud;p->Max(p);return 0;
}
#include <string>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:"<<Max_num;//是Max_num,not Max_number
}int main()
{
Studest *p;
p=stud;p->Max(p);return 0;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询