代码参考于链接
十一. 封装的public与protected与private 、友元、继承的public与protected与private
1. public与protected与private
C++让您能够将类属性和方法声明为公有的,这意味着有了对象后就可获取它们;也可将其声明为私有的, 这意味着只能在类的内部(或其友元)中访问。 作为类的设计者, 您可使用 C++关键字 public和 private 来指定哪些部分可从外部(如 main( ))访问,哪些部分不能。
class Human
{
private:
// Private member data:
int age;
string name;
protected:
int wage;
public:
int GetAge()
{
return age;
}
void SetAge(int humansAge)
{
age = humansAge;
}
// ...Other members and declarations
};
// 假设有一个名为 eve 的 Human 实例,如果试图使用下述代码访问成员age或者wage,将会报错
Human eve;
cout << eve.age; // compile error
cout << eve.wage; // compile error
2. 友元-能够访问封装为private的成员
使用关键字 friend 让外部函数 DisplayAge( )能够访问私有数据成员
#include <iostream>
#include <string>
using namespace std;
class Human
{
private:
friend void DisplayAge(const Human& person);
string name;
int age;
public:
Human(string humansName, int humansAge)
{
name = humansName;
age = humansAge;
}
};
void DisplayAge(const Human& person)
{
cout << person.age << endl;
}
int main()
{
Human firstMan("Adam", 25);
cout << "Accessing private member age via friend function: ";
DisplayAge(firstMan);
return 0;
}
使用关键字 friend 让外部类 Utility 能够访问私有数据成员
#include <iostream>
#include <string>
using namespace std;
class Human
{
private:
friend class Utility;
string name;
int age;
public:
Human(string humansName, int humansAge)
{
name = humansName;
age = humansAge;
}
};
class Utility
{
public:
static void DisplayAge(const Human& person)
{
cout << person.age << endl;
}
};
int main()
{
Human firstMan("Adam", 25);
cout << "Accessing private member age via friend class: ";
Utility::DisplayAge(firstMan);
return 0;
}
3. 继承的public与protected与private(重新修改继承后的属性)
继承的public与protected与private,是指派生类继承基类后,成员和方法的封装属性变化。比如使用protected去继承基类,那么在派生类中继承的成员和方法最低也是protected级。
有public, protected, private三种继承方式,它们相应地改变了基类成员的访问属性。
- 1.public继承:基类public成员,protected成员,private成员的访问属性在派生类中分别变成:public, protected, private
- 2.protected继承:基类public成员,protected成员,private成员的访问属性在派生类中分别变成:protected, protected, private
- 3.private继承:基类public成员,protected成员,private成员的访问属性在派生类中分别变成:private, private, private
但无论哪种继承方式,上面两点都没有改变:
- 1.private成员只能被本类成员(类内)和友元访问,不能被派生类访问;
比如A继承于a,a中有个private age,那么在A中使用this.age是不允许的,类中只能通过this.a::age - 2.protected成员可以被派生类访问。
-比如A继承于a,a中有个protected age,那么在A中使用this.age是允许的,类外的实例.age是不允许的。
4. 示例1-public继承
#include<iostream>
#include<assert.h>
using namespace std;
class A {
public:
int a;
A() {
a1 = 1;
a2 = 2;
a3 = 3;
a = 4;
}
void fun() {
cout << a << endl; //正确
cout << a1 << endl; //正确
cout << a2 << endl; //正确
cout << a3 << endl; //正确
}
public:
int a1;
protected:
int a2;
private:
int a3;
};
class B : public A {
public:
int a;
B(int i) {
A();
a = i;
}
void fun() {
cout << a << endl; //正确,public成员
cout << a1 << endl; //正确,基类的public成员,在派生类中仍是public成员。
cout << a2 << endl; //正确,基类的protected成员,在派生类中仍是protected可以被派生类访问。
cout << a3 << endl; //错误,基类的private成员不能被派生类访问。
}
};
int main() {
B b(10);
cout << b.a << endl;
cout << b.a1 << endl; //正确
cout << b.a2 << endl; //错误,类外不能访问protected成员
cout << b.a3 << endl; //错误,类外不能访问private成员
system("pause");
return 0;
}
5. 示例2-protected继承
#include<iostream>
#include<assert.h>
using namespace std;
class A {
public:
int a;
A() {
a1 = 1;
a2 = 2;
a3 = 3;
a = 4;
}
void fun() {
cout << a << endl; //正确
cout << a1 << endl; //正确
cout << a2 << endl; //正确
cout << a3 << endl; //正确
}
public:
int a1;
protected:
int a2;
private:
int a3;
};
class B : protected A {
public:
int a;
B(int i) {
A();
a = i;
}
void fun() {
cout << a << endl; //正确,public成员。
cout << a1 << endl; //正确,基类的public成员,在派生类中变成了protected,可以被派生类访问。
cout << a2 << endl; //正确,基类的protected成员,在派生类中还是protected,可以被派生类访问。
cout << a3 << endl; //错误,基类的private成员不能被派生类访问。
}
};
int main() {
B b(10);
cout << b.a << endl; //正确。public成员
cout << b.a1 << endl; //错误,protected成员不能在类外访问。
cout << b.a2 << endl; //错误,protected成员不能在类外访问。
cout << b.a3 << endl; //错误,private成员不能在类外访问。
system("pause");
return 0;
}
6. 示例3-private继承
#include<iostream>
#include<assert.h>
using namespace std;
class A {
public:
int a;
A() {
a1 = 1;
a2 = 2;
a3 = 3;
a = 4;
}
void fun() {
cout << a << endl; //正确
cout << a1 << endl; //正确
cout << a2 << endl; //正确
cout << a3 << endl; //正确
}
public:
int a1;
protected:
int a2;
private:
int a3;
};
class B : private A {
public:
int a;
B(int i) {
A();
a = i;
}
void fun() {
cout << a << endl; //正确,public成员。
cout << a1 << endl; //正确,基类public成员,在派生类中变成了private,可以被派生类访问。
cout << a2 << endl; //正确,基类的protected成员,在派生类中变成了private,可以被派生类访问。
cout << a3 << endl; //错误,基类的private成员不能被派生类访问。
}
};
int main() {
B b(10);
cout << b.a << endl; //正确。public成员
cout << b.a1 << endl; //错误,private成员不能在类外访问。
cout << b.a2 << endl; //错误, private成员不能在类外访问。
cout << b.a3 << endl; //错误,private成员不能在类外访问。
system("pause");
return 0;
}
7.上面示例一个异常点
仔细看代码中派生类B中定义了和基类同名的成员a,此时基类的a仍然存在,可以验证。
int main(){
B b(10);
cout << b.a << endl;
cout << b.A::a << endl;
system("pause");
return 0;
}