重新改了#include <iostream>
#include <string>
class Department
{
public:
std::string DepName;
Department(std::string theName);
~Department();
void room();
};
class Members:public Department
{
public:
std::string MemName;
Members(std::string theName);
~Members();
void work(std::string theName);
};
Department::Department(std::string theName)
{
DepName=theName;
std::cout<<"部门名称:"<<DepName<<std::endl;
};
Department::~Department()
{
}
Members::Members(std::string theName):Department(theName)
{
};
Members::~Members()
{
}
void Department::room()
{
std::cout<<"房间号:405-2"<<std::endl;
}
void Members::work(std::string theName)
{
std::cout<<theName<<"正在努力工作"<<std::endl;
}
int main()
{
Members M1("张三");
Department D1("研发部");
D1.room();
M1.work(M1.MemName);
return 0;
};
改了,程序运行算是没错了。但是运行程序得出的结果和我预想的有出入。
1、张三赋给了部门。我的设想是赋给成员名。这个是因为子构造器和基构造器继承的关系吗?改子的,基的也变?
2、我想通过M1.work(M1.MemName)打印张三正在努力工作。好像也没成功。
3、Member M1("张三");这是对构造器赋值还是对Member类对象M1的MemName属性赋值?
学成浆糊了。
|