请教关于C++对象中函数调用的问题

来源:百度知道 编辑:UC知道 时间:2024/09/19 16:16:31
要求用fun()调用student类中的display()和change()
下面是我写的代码 不通过 请问问题何在?THX

代码如下

#include <iostream>
using namespace std;
class student
{
public:
student(int n,float s):num(n),score(s){}
void change(int n,float s){num=n,score=s;}
void display(){cout<<num<<" "<<score<<endl;}
void fun(student&);
private:
int num;
float score;
};

void student::fun(student&t)
{
t.display();
t.change();
t.display();
}

int main()
{
student stud(101,78.5);
stud.fun();
getchar();
return 0;
}
还要求需要使用对象的引用(student&)作为形参哦 再帮忙看看 谢谢

没传参数或者是参数列表忘了去掉了
void student::fun( --> student&t <-- )

stud.fun( --> <-- );

改了,没编译.你自己试试;

#include <iostream>
using namespace std;
class student
{
public:
student(int n,float s):num(n),score(s){}
void change(int n,float s){num=n,score=s;}
void display(){cout<<num<<" "<<score<<endl;}
void fun();
private:
int num;
float score;
};

void student::fun()
{
display();
change(101,80.5);
display();
}

int main()
{
student stud(101,78.5);
stud.fun();
getchar();
return 0;
}

或者
#include <iostream>
using namespace std;
class student
{
public:
student(int n,float s):num(n),score(s){}
void change(int n,float s){num=n,score=s;}
void display(){cout<<num<<" "<&