首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

C++类对象指针作为函数参数应用的有关问题

2013-03-06 
C++类对象指针作为函数参数应用的问题书上有个题:建立一个对象数组,内放5个学生的数据(学号、成绩),设立一

C++类对象指针作为函数参数应用的问题

书上有个题:建立一个对象数组,内放5个学生的数据(学号、成绩),设立一个函数max,用指向对象的指针作函数参数,在max函数中找出5个学生中成绩最高者,并输出其学号。
对于这个问题,本小白调试了好久,也没想明白这个学号最后怎么输出……
也许是我思路有问题,请各位大牛给个思路,看我下面那代码,我真心不知道这max怎么写,又是怎么调用?

#include <iostream>

using namespace std;

class Student
{
public:
Student(int a,int b);
void  max(Student * x);

private:
int num;
int score;
};
Student::Student(int a,int b):num(a),score(b)
{}
void Student::max(Student * x)
{


for(int i=0;i<=4;i++)
    {
if(x->score>(x+1)->score)
{
(x+1)->score=x->score;

}
}


 cout<<(x+1)->num<<'\t'<<(x+1)->score<<endl;
 
}
int main()
{
Student st[5]={Student(10,89),Student(11,90),Student(12,91),Student(13,93),Student(14,94)};

//怎么调用那个max?
        return 0;
}


c++ class
[解决办法]
max这个方法不应是student中的方法,学生不具有和这个行为!
所以你的max需要独立写出来的
void max(Student * x)
{
     
     
        for(int i=0;i<=4;i++)
    {
        if(x->score>(x+1)->score)
        {
            (x+1)->score=x->score;
 
        }
    }
 //复制你的算法代码,没有测试对否!
 
     cout<<(x+1)->num<<'\t'<<(x+1)->score<<endl;
      
}

然后你再把你的5个student对象作为实参穿进去比较即可
[解决办法]
引用:
引用:max这个方法不应是student中的方法,学生不具有和这个行为!
所以你的max需要独立写出来的
C/C++ code?1234567891011121314151617void max(Student * x){                    for(int i=0;i<=4;i++)    {        ……

为了不批坏封装性,不建议采用友元,你建立一个int GetScore(){return **;}内连方法不就行了么!
[解决办法]
#include <iostream>
 
using namespace std;
 
class Student
{
public:
    Student(int a,int b);
    friend void  max(Student * x);
 
private:
    int num;
    int score;
};
Student::Student(int a,int b):num(a),score(b)
{}
/*void Student::max(Student * x)
{
     
     
        for(int i=0;i<=4;i++)
    {
        if(x->score>(x+1)->score)
        {
            (x+1)->score=x->score;


 
        }
    }
 */
void max(Student * x)
{
        for(int i=0;i<=4;i++)
    {
        if(x->score>(x+1)->score)
        {
            (x+1)->score=x->score;
  
        }
    }
 //复制你的算法代码,没有测试对否!
  
cout<<(x+1)->num<<endl;
cout<<(x+1)->score<<endl;
       
}
int main()
{
    Student st[5]={Student(10,89),Student(11,90),Student(12,91),Student(13,93),Student(14,94)};
 
//怎么调用那个max?
     max(st);
        return 0;
}
这样就对了。。。你看看行不

[解决办法]

引用:
引用:#include <iostream>
 
using namespace std;
 
class Student
{
public:
    Student(int a,int b);
    friend void  max(Student * x);
 
private:
    int num;
……

说了写个GetScore()方法就行了,利用上你的算法,都解决问题了。语法问题你自己再去研究
[解决办法]
仅供参考:
#include <iostream>

using namespace std;

class Student
{
public:
Student(int a,int b);
    static Student  max(Student stu[]);

    inline int getScore( void )
    {
        return score;
    }

    inline int getNumber( void )
    {
        return num;
    }

private:
int num;
int score;
};

Student::Student(int num,int score)
    :num(num),score(score)
{

}

Student Student::max(Student stu[])
{
    Student maxStu = stu[0];

    for ( int index = 1; index < 5; ++index )
    {
        if ( maxStu.score < stu[index].score )
        {
            maxStu = stu[index];
        }
    }

    return maxStu;
 
}
int main()
{
    Student stu[5]={Student(10,89),Student(11,90),Student(12,91),Student(13,93),Student(14,94)};

    Student maxStu = Student::max( stu );
    cout << " Max Score: " << maxStu.getScore() << " The Stu Number: " << maxStu.getNumber() << endl;
    return 0;


}


[解决办法]
引用:
引用:仅供参考:
C/C++ code?123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354#include <iostream> using namespace std; class S……



类成员函数是只能由类对象调用,只不过max是静态成员。

Student s1, s2;
s1.max();
s2.max();
====都等同于 Student::max();

至于没有体现“类对象指针”,
本来就应该尽可能的少用指针,最好用引用替换,或者用智能指针包装。
每次使用指针前,都要断言指针的有效性,是很麻烦的一件事。

热点排行