类的继承与派生
4、小作业:
按照继承的规则,既然父类中的private私有成员不能在子类中直接使用,那么有没有什么办法能解决这个问题呢?让子类可以直接或者间接的使用父类中的private私有成员呢?大家想想?
比如上面代码中的 CZhongStudent 类的 get_flag_1 函数,如何能获取到 flag_private 的值呢?大家课后想一想,并且自己实现一下试试!
#include <iostream> #include <string> using namespace std; class C_Student { private: string password; public: string user; string set_provate(string x) { this->password = x; return this->password; } }; class C_Pupil :public C_Student { public: string arithmetic_score; void get_provate(string x) { string pass = set_provate(x); cout << "密码 = " << pass << endl; } }; int main() { C_Pupil lisi; lisi.get_provate("12azs"); return 0; }
浙公网安备 33010602011771号