相关内容:
1.类的三种成员类型
如图所示: public
, protected
, private
三种成员类型,当没表明类型的时候,(class
)默认是private
的。
类外部的意思是:main
函数,或者定义全局变量的外部空间,或者其他类空间。
2.三种继承方式
三种继承方式:也是 public
,protected
,private
。从上图可以看出:
当public继承的时候,父类中public
类型的成员会被继承到子类的public
中去,而父类的protected
成员也会被继承到子类的protected
中去;
当protected
继承的时候,父类中无论是public
的还是protected
的成员都会被继承到子类的protected
中去;
当private
继承的时候,父类中 public
和 protected
的成员都会被继承到子类的private
中去;
注意:父类的private
成员是无论如何都不能被继承的。
3.改变访问权限
使用 using
关键字可以改变基类成员在派生类中的访问权限,例如将 public
改为 private
、将 protected
改为 public
。
注意:using
只能改变基类中 public
和 protected
成员的访问权限,不能改变 private
成员的访问权限,因为基类中 private
成员在派生类中是不可见的,根本不能使用,所以基类中的 private
成员在派生类中无论如何都不能访问。
代码相关内容:
- 公有,私有,受保护继承:
class B : public A;
class B : private A;
class B : protected A;
; - 公有继承:父类中的
public
成员继承到子类中仍是public
成员,父类中的protected
成员继承到子类中仍是protected
成员; - 受保护继承:父类中的
public
成员和protected
成员继承到子类中变为protected
成员; - 私有继承:父类中的
public
成员和protected
成员继承到子类中变为private
成员; - 接口继承(即公有继承)和实现继承(即私有继承和受保护继承,极少用到);
- 修改继承访问(去除个别成员);
- 默认继承访问级别;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
using namespace std;
class A
{
public:
A()
{
a = 10; b = 20; c = 30; a2 = 100; a3 = 200;
}
int a;
int a2;
int a3;
protected:
int b;
private:
int c;
};
class B1 : public A // 公有继承
{
public:
void test()
{
cout << a << endl;
cout << b << endl; // 可以;protected成员进行了继承;
//cout << c << endl; //错误;因为private成员没有继承;
}
};
class B2 : private A
{
public:
using A::a2; // 通过using A::a2将a2保持为原有的public;称之为修改继承访问(去除个别成员);
void test()
{
// private继承之后将原来的protected成员和public成员都变成了private的;
cout << a << endl;
cout << b << endl;
//cout << c << endl; // 错误,因为c是类A中private的,此处没有继承,不能使用;
}
};
class B3 : protected A
{
public:
void test()
{
// protected继承之后将原来的protected成员和public成员都变成了portected的;
cout << a << endl;
cout << b << endl;
//cout << c << endl; //错误,因为c是类A中private的,此处没有继承,不能使用;
}
};
class B4 : A //默认为private继承;等效于:class B4 : private A
{
int b4; //默认是private成员;
};
struct B5 : A // 默认是public继承;等效于 struct B5 : public A
{
int b5; //默认是public成员;
};
class C : public B1 // 私有继承
{
public:
void test()
{
cout << a << endl;
cout << b << endl;
// cout << c << endl;
}
};
int main()
{
//public继承
B1 b1;
cout << b1.a << endl;
//b1.b; // 错误;虽然类 B1 从类 A 中继承了b,但是protected b只能在类A中使用,不能再类外面使用;
//b1.c; // 错误;作为 private 类 B1 没有从类 A 中继承 pricate 成员;
b1.test();
//private继承,将原有类中的public和protected成员都变成了private成员;
B2 b2;
//cout << b2.a << endl; //错误
//cout << b2.b << endl; //错误
//cout << b2.c << endl; //错误
cout << b2.a2 << endl;
//cout << b2.a3 << endl; //错误;
b2.test();
//protected继承,将原有类中的public和protected成员都变成了protected成员;
B3 b3;
//cout << b3.a << endl;// 错误,protected成员可以在继承的子类中使用,但是不能再类外使用
//cout << b3.b << endl;// 错误,protected成员可以在继承的子类中使用,但是不能再类外使用
//cout << b3.c << endl;// 错误;作为 private 类 B3 没有从类 A 中继承 pricate 成员;
b3.test();
return 0;
}代码执行结果:
参考链接:
C++的继承(三种继承方式)