C++继承方式

2022/2/20 22:29:17

本文主要是介绍C++继承方式,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

C++类成员访问属性

类成员的访问属性有3种,公有属性,保护属性,私有属性。

其中公有属性的成员在类内和类外均可以直接访问;

保护属性的成员在类内可以访问,类外不可直接访问;

私有属性的成员在类内可以访问,类外不可直接访问;

举例:

类内:

 1 class A
 2 {
 3 private:
 4     int a;
 5 protected:
 6     int b;
 7 public:
 8     int c;
 9     void PrintName() {
10         //3个数据成员在类内均可直接访问
11         cout << a << endl;
12         cout << b << endl;
13         cout << c << endl;
14     }
15 };

类外:

1 int 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     
8     return EXIT_SUCCESS;
9 }

 

C++继承方式

C++类的继承方式有3种,公有继承(public)、保护继承(protected)、私有继承(private)。

1.公有继承

采用公有继承方式创建的子类,基类中的私有成员对子类来说是不可见的,不能直接访问;基类的保护成员在子类中的访问属性还是protected,基类的公有成员在子类中的访问属性还是public。

在子类中可以直接访问基类的保护和公有成员,但是在类外不可直接访问基类的保护成员。

 1 class People
 2 {
 3 private:
 4     int a; //私有成员
 5 protected:
 6     int b; //保护成员
 7 public:
 8     int c; //公有成员
 9 };
10 
11 /**
12  * 公有继承,基类中的私有成员在子类中不可见,保护成员属性还是保护成员,共有成员属性还是公有成员
13  */
14 class PublicDerive : public People
15 {
16     void 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 
23 int 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 
30     return EXIT_SUCCESS;
31 }

2.保护继承

采用保护继承方式创建的子类,基类中的私有成员对子类来说是不可见的,不可直接访问,基类的保护成员和公共成员在子类中的属性均是protected。

 1 class People
 2 {
 3 private:
 4     int a; //私有成员
 5 protected:
 6     int b; //保护成员
 7 public:
 8     int c; //公有成员
 9 };
10 
11 class ProtectedDerive : protected People
12 {
13     void 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 
20 int 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 because 'ProtectedDerive' uses 'protected' to inherit from 'People'
26 
27     return EXIT_SUCCESS;
28 }

如上代码中,ProtectedDerive中成员b和c的访问属性还不能直观说明,因为类内均可直接访问,类外均不能访问,说明bc属性是保护或者私有。可用如下方式进行验证:

1 class ProtectedDerive2 : public ProtectedDerive
2 {
3     void 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。

 1 class People
 2 {
 3 private:
 4     int a; //私有成员
 5 protected:
 6     int b; //保护成员
 7 public:
 8     int c; //公有成员
 9 };
10 
11 class PrivateDerive : private People
12 {
13     void printName()
14     {
15         cout << a << endl; //基类私有不可见
16         cout << b << endl; //基类保护,类内可见,属性私有
17         cout << c << endl; //基类公有,类内可见,属性私有
18     }
19 };
20 
21 int 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 because 'ProtectedDerive' uses 'protected' to inherit from 'People'
28 
29     return EXIT_SUCCESS;
30 }

如上方式可以知道在子类PrivateDerive中,bc成员可在子类内直接访问,不可在子类外直接访问,其属性为保护或者私有。采用如下方式验证属性:

1 class PrivateDerive2 : public PrivateDerive
2 {
3     void 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 because 'ProtectedDerive' uses 'private' to inherit from 'People'
7     }
8 };

PrivateDerive2类从PrivateDerive类继承而来,a/b/c三个属性均不可直接在类内访问,说明PrivateDerive的三个属性均是私有。

 



这篇关于C++继承方式的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程