搞清楚访问控制和以 xx 方式继承
#include <iostream>
#include <string>
using namespace std;
class A
{
private: // 只能自己访问
string : private_str;
protected: // 不能被类外访问, 也就是说父类子类都可以访问
string protected_str;
public: // 谁都可以访问
string public_str;
string get_str(string name)
{
if (name == "private_str")
return private_str;
if (name == "protected_str")
return protected_str;
if (name == "public_str")
return public_str;
}
};
class B : public A
{
public:
void print_str()
{
cout << private_str << endl;
cout << protected_str << endl;
cout << public_str << endl;
cout << get_str("private_str") << endl;
}
};
class C : protected A
{
void print_str()
{
cout << private_str << endl;
cout << protected_str << endl;
cout << public_str << endl;
}
}