如果把类中的初始化函数也注释就会正常打印数值了,这是怎么回事呢?
#include <iostream>
#include <stdlib.h>
#include <string>
using namespace std;
class Student
{
public:
void setName(string _name)
{
m_strName=_name;
}
string getName()
{
return m_strName;
}
void setGender(string _gender)
{
m_strGender = _gender;
}
string getGender() {
return m_strGender;
}
int getScore()
{
return m_iScore;
}
/*void initScore()
{
m_iScore = 0;
}
* */
void study (int _score)
{
m_iScore += _score;
}
private:
string m_strName;
string m_strGender;
int m_iScore;
};
int main()
{
Student stu;
//stu.initScore();
stu.setName("z");
stu.setGender("f");
stu.study(5);
stu.study(5);
cout << stu.getName() << " " << stu.getGender() << " " << stu.getScore() << endl;
return 0;
}
1.没有明白为什么没有初始化,score的值不正常,有study 函数来赋值呢。
stu.study(5);
void study (int _score)
{
m_iScore += _score;
}
2. 把类中的初始化函数也注释就会正常打印数值了,这是怎么回事呢?