创建了一个基类woker 派生了一个employee的类 然后执行下面的语句 生成的woker指针里面的成员没有初始化 但是函数可以正常调用
这是类代码
class woker
{
public:
virtual void showinfo() = 0;
virtual void getdepaname() = 0;
int num = 0 ;
string name;
int depanum = 0;
};
class employee : public woker
{
public:
employee(int temnum, string temname, int temdepanum)
{
this->num = temnum;
this->name = temname;
this->depanum = temdepanum;
}
void showinfo()
{
cout << "num :" << this->num << "\t" << "name :" << this->name << "\t" << "depanum :" << this->depanum << endl;
}
void getdepaname()
{
cout << "岗位名称 : 普通员工" << endl;
}
int num ;
string name;
int depanum;
};
给指针赋地址
你的
woker* a = new employee(1, "wxx",1);
已经通过构造给a赋值了
如果想给woker赋值,这样
class employee : public woker
{
public:
employee(int temnum, string temname, int temdepanum)
{
this->num = temnum;
this->name = temname;
this->depanum = temdepanum;
}
void showinfo()
{
cout << "num :" << this->num << "\t" << "name :" << this->name << "\t" << "depanum :" << this->depanum << endl;
}
void setwokerNum(int num){
woker::num = num;
}
void getdepaname()
{
cout << "岗位名称 : 普通员工" << endl;
}
int num ;
string name;
int depanum;
};
a->setwokerNum(100);
100就赋值给woker中的num
this去掉看看,直接num=temnum