| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 777 人关注过本帖
标题:friend 函数问题
只看楼主 加入收藏
haoyasen
Rank: 2
等 级:论坛游民
帖 子:90
专家分:20
注 册:2013-3-30
结帖率:91.67%
收藏
已结贴  问题点数:20 回复次数:3 
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;
}
搜索更多相关主题的帖子: 学生 friend 设计 private include 
2013-05-30 22:45
haoyasen
Rank: 2
等 级:论坛游民
帖 子:90
专家分:20
注 册:2013-3-30
收藏
得分:0 
扩展练习那个友元函数 ,有问题 ,目前在调 不知道错哪了
2013-05-30 22:47
justlxy
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:28
专家分:158
注 册:2013-5-14
收藏
得分:20 
#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[]);          //更具要求,此处给书申明,接下来在类外定义。
   };  
 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 getRank(CStudent grade[])           //此处定义友元函数。
    {
         cout<<"chufa "<<endl;
         //int num=sizeof(grade)/sizeof(grade[0]);              //此处语句错误,sizeof(grade)的值是4,而sizeof(grade[0])的值是20!。
         //故此处作如下改动:
         int num=3;
         for(int i=0;i<num-1;i++)//触发不了
        {
            
             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(i=0;i<num;i++)             //注意此处的修改,原程序的i被多次定义了。
         {
             grade[i].displayInfor();
         }
     }
 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;
 }
2013-06-01 12:15
haoyasen
Rank: 2
等 级:论坛游民
帖 子:90
专家分:20
注 册:2013-3-30
收藏
得分:0 
回复 3楼 justlxy
en   我也该完啦
2013-06-02 11:10
快速回复:friend 函数问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017199 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved