C++继承⽅式
C++类成员访问属性
类成员的访问属性有3种,公有属性,保护属性,私有属性。
其中公有属性的成员在类内和类外均可以直接访问;
保护属性的成员在类内可以访问,类外不可直接访问;
私有属性的成员在类内可以访问,类外不可直接访问;
举例:
类内:
1class A
2 {
3private:
4int a;
5protected:
6int b;
7public:
8int c;
9void PrintName() {
10//3个数据成员在类内均可直接访问
11 cout << a << endl;
12 cout << b << endl;
13 cout << c << endl;
14 }
15 };
类外:
1int main()
2 {
3 A Object;
4 cout << Object.a << endl; //私有不可直接访问,member "A::a" (declared at line 72) is inaccessible
5 cout << Object.b << endl; //保护不可直接访问,member "A::b" (declared at line 72) is inaccessible
6 cout << Object.c << endl; //公有可以直接访问
7
8return EXIT_SUCCESS;
9 }
C++继承⽅式
C++类的继承⽅式有3种,公有继承(public)、保护继承(protected)、私有继承(private)。
供组词1.公有继承
采⽤公有继承⽅式创建的⼦类,基类中的私有成员对⼦类来说是不可见的,不能直接访问;基类的保护成员在⼦类中的访问属性还是protected,基类的公有成员在⼦类中的访问属性还是public。
在⼦类中可以直接访问基类的保护和公有成员,但是在类外不可直接访问基类的保护成员。
1class People
2 {
3private:
4int a; //私有成员
5protected:
6int b; //保护成员
7public:
8int c; //公有成员
9 };
10
11/**
12 * 公有继承,基类中的私有成员在⼦类中不可见,保护成员属性还是保护成员,共有成员属性还是公有成员
13*/
14class PublicDerive : public People
15 {
16void printName() {
17 cout << a << endl; //基类私有不可见,member "People::a" (declared at line 8) is inaccessible
白色怎么读英语18 cout << b << endl; //基类保护,类内可见,属性保护
19 cout << c << endl; //基类公有,类内可见,属性公有
20 }
21 };
22
23int main()
24 {
25 PublicDerive publicDerive;
26 cout << publicDerive.a << endl; //member "People::a" (declared at line 8) is inaccessible
27 cout << publicDerive.b << endl; //member "People::b" (declared at line 10) is inaccessible
28 cout << publicDerive.c << endl; //
29
30return EXIT_SUCCESS;
31 }
2.保护继承
采⽤保护继承⽅式创建的⼦类,基类中的私有成员对⼦类来说是不可见的,不可直接访问,基类的保护成员和公共成员在⼦类中的属性均是protected。
1class People
2 {
3private:
4int a; //私有成员
5protected:
6int b; //保护成员
7public:
理论科学8int c; //公有成员
9 };
10
11class ProtectedDerive : protected People
12 {
13void printName() {
培根英文
14 cout << a << endl; //基类私有不可见,'People::a': cannot access private member declared in class 'People'
15 cout << b << endl; //基类保护,类内可见,属性保护
16 cout << c << endl; //基类公有,类内可见,属性保护
17 }
18 };
19
20int main()
21 {
好心当成驴肝肺意思
22 ProtectedDerive protectedDerive;
23 cout << protectedDerive.a << endl; //'People::a': cannot access private member declared in class 'People'
24 cout << protectedDerive.b << endl; //'People::b': cannot access protected member declared in class 'People'
荆轲刺秦小品25 cout << protectedDerive.c << endl; //'People::c' not accessible becau 'ProtectedDerive' us 'protected' to inherit from 'People'
26
27return EXIT_SUCCESS;
28 }
如上代码中,ProtectedDerive中成员b和c的访问属性还不能直观说明,因为类内均可直接访问,类外均不能访问,说明bc属性是保护或者私有。可⽤如下⽅式进⾏验证:
1class ProtectedDerive2 : public ProtectedDerive
2 {
3void printName() {
4 cout << a << endl; //基类私有不可见,'People::a': cannot access private member declared in class 'People'
5 cout << b << endl; //基类保护,类内可见,属性保护
6 cout << c << endl; //基类保护,类内可见,属性保护
7 }
8 };
论文致谢格式ProtectedDerive2类从ProtectedDerive类继承⽽来,但是bc属性在ProtectedDerive2类内是可见的。因私有继承,基类属性在⼦类中不可见,所以保护继承⽅式中,基类的保护和公有属性在⼦类中的属性是保护。
3.私有继承
采⽤私有继承⽅式创建的⼦类,基类中的私有成员对⼦类来说不可见,不可直接访问,保护和公共成员在⼦类中的属性均为private。
1class People
2 {
3private:
4int a; //私有成员
5protected:
6int b; //保护成员
7public:
8int c; //公有成员
9 };
10
11class PrivateDerive : private People
12 {
13void printName()
14 {
15 cout << a << endl; //基类私有不可见
16 cout << b << endl; //基类保护,类内可见,属性私有
17 cout << c << endl; //基类公有,类内可见,属性私有
18 }
19 };
20
21int main()
22 {
23
24 PrivateDerive privateDerive;
25 cout << privateDerive.a << endl; //'People::a': cannot access private member declared in class 'People'
26 cout << privateDerive.b << endl; //'People::b': cannot access protected member declared in class 'People'
27 cout << privateDerive.c << endl; //'People::c' not accessible becau 'ProtectedDerive' us 'protected' to inherit from 'People'
28
29return EXIT_SUCCESS;
30 }
如上⽅式可以知道在⼦类PrivateDerive中,bc成员可在⼦类内直接访问,不可在⼦类外直接访问,其属性为保护或者私有。采⽤如下⽅式验证属性:
1class PrivateDerive2 : public PrivateDerive
2 {
茄子卤的做法3void printName() {
4 cout << a << endl; //基类私有,不可见,'People::a': cannot access private member declared in class 'People'
5 cout << b << endl; //基类私有,不可见,'People::b': cannot access inaccessible member declared in class 'People'
6 cout << c << endl; //基类私有,不可见,'People::c' not accessible becau 'ProtectedDerive' us 'private' to inherit from 'People'
7 }
8 };
PrivateDerive2类从PrivateDerive类继承⽽来,a/b/c三个属性均不可直接在类内访问,说明PrivateDerive的三个属性均是私有。