C++在类中对变量声明的时候就已经创建了该变量了么?
C++在类中对变量声明的时候就已经创建了该变量了么?或者说已经为该变量分配内存了,只是内存中的数是未知的。代码如下:
//student.h
class student
{public:
int score;
int num;
public:
student();
student(int s,int n);
void f1();
void f2();
};
//student.cpp
student::student(){}
student::student(int s,int m)
{
score=s;
num=m;
}
void student::f1()
{
score=score+1;
num=num+1;
cout<<"score="<<score<<", "<<"num="<<num<<endl;
}
void student::f2()
{
score=score+2;
num=num+2;
cout<<"score="<<score<<", "<<"num="<<num<<endl;
}
//main
#include "student.h"
void main()
{
student op;
op.f1();
op.f2();
}
输出为:-58993459,-58993459
-58993457,-58993457