注册 登录
编程论坛 新人交流区

求2个学生平均分的类~哪出问题了??

weiweizkk 发布于 2007-10-11 15:40, 642 次点击
#include<iostream.h>
class Student
{
public:
Student(char x,int y,float z)
{
x=n;
y=no;
z=s;

t++;
g+=z;
}
friend void print()
{
cout<<g/t<<endl;
}
private:
static int t;
char n;
int no;
float s;
static float g;
};
float Student::g=0;
int Student::t=0;
void main()
{
char w,q;
Student s1(w,1,90.5),s2(q,2,80.5);
print();
}

想求这2个学生的平均分...貌似有点问题...~~~小弟刚学C++...请大家指点指点~~谢谢!
9 回复
#2
xhtang2007-10-11 16:47
,,检查一下,

[QUOTE]

#include<iostream.h>
class Student
{
public:
Student(char x,int y,float z)
{
x=n;
y=no;
z=s;

t++;
g+=z;
}
friend void print()
{
cout<<g/t<<endl;
}
private:
static int t;
char n;
int no;
float s;
static float g;
};
float Student::g=0;
int Student::t=0;
void main()
{
char w,q;
Student s1(w,1,90.5),s2(q,2,80.5);//这里它要传什么过去?
print();
}

[/QUOTE]
#3
Standford2007-10-11 18:42
调用函数时,是形参给实参单向传递值
Student(char x,int y,float z)
{
x=n;
y=no;
z=s;
此处位置写反了。改为
Student(char x,int y,float z)
{
n=x;
no=y;
s=z;
即可
#4
weiweizkk2007-10-11 19:07
回复:(Standford)调用函数时,是形参给实参单向传递...

这样可以出来结果,但为什么有2个warning(s)啊?
: warning C4700: local variable 'w' used without having been initialized
: warning C4700: local variable 'q' used without having been initialized

d.obj - 0 error(s), 2 warning(s)
这是什么意思啊?

#5
Standford2007-10-11 19:34
我在机子上运行没有 2 warning(s)啊。我感觉定义
void main()
{
char w,q;
Student s1(w,1,90.5),s2(q,2,80.5);
print();
}
有些不妥,定义w,q变量是作为两同学代码吧,这里没有初始化,s1,s2中的w,q是传入的形参常量,这里的w,q意义不一样,最好定义char w,q;时初始化,不然就没啥意义了。
#6
weiweizkk2007-10-16 19:13
结果出来了  谢谢指点!
#7
ascend_07072007-10-16 19:56
怎么出来的?给w和q初始化了是吗?分别赋一个植?
#8
weiweizkk2007-10-27 15:36

没,就是照3楼朋友说的做的~~~呵呵

#9
dengerlong2007-10-27 16:03
```````````````
#10
dengerlong2007-10-27 16:04
`````````````````
1