服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C/C++ - C++ 的三种访问权限与三种继承方式

C++ 的三种访问权限与三种继承方式

2021-04-19 14:59C++教程网 C/C++

我们知道C++中的类,有三种访问权限(也称作访问控制),它们分别是public、protected、private,C++中继承的方式还有多种。下面通过本文给大家详细介绍,对c++中的访问权限和继承方式感兴趣的朋友一起看看吧

三种访问权限

我们知道C++中的类,有三种访问权限(也称作访问控制),它们分别是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
class Person
{
public:
Person(const string& name, int age) : m_name(name), m_age(age)
{
}
void ShowInfo()
{
cout << "姓名:" << m_name << endl;
cout << "年龄:" << m_age << endl;
}
protected:
string m_name; //姓名
private:
int m_age; //年龄
};
class Person
{
public:
Person(const string& name, int age) : m_name(name), m_age(age)
{
}
void ShowInfo()
{
cout << "姓名:" << m_name << endl;
cout << "年龄:" << m_age << endl;
}
protected:
string m_name; //姓名
private:
int m_age; //年龄
};

子类:

?
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
class Teacher : public Person
{
public:
Teacher(const string& name, int age, const string& title)
: Person(name, age), m_title(title)
{
}
void ShowTeacherInfo()
{
ShowInfo(); //正确,public属性子类可见
cout << "姓名:" << m_name << endl; //正确,protected属性子类可见
cout << "年龄:" << m_age << endl; //错误,private属性子类不可见
cout << "职称:" << m_title << endl; //正确,本类中可见自己的所有成员
}
private:
string m_title; //职称
};
class Teacher : public Person
{
public:
Teacher(const string& name, int age, const string& title)
: Person(name, age), m_title(title)
{
}
void ShowTeacherInfo()
{
ShowInfo(); //正确,public属性子类可见
cout << "姓名:" << m_name << endl; //正确,protected属性子类可见
cout << "年龄:" << m_age << endl; //错误,private属性子类不可见
cout << "职称:" << m_title << endl; //正确,本类中可见自己的所有成员
}
private:
string m_title; //职称
};

调用方法:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
void test()
{
Person person("张三", 22);
person.ShowInfo(); //public属性,对外部可见
cout << person.m_name << endl; //protected属性,对外部不可见
cout << person.m_age << endl; //private属性,对外部不可见
}
void test()
{
Person person("张三", 22);
person.ShowInfo(); //public属性,对外部可见
cout << person.m_name << endl; //protected属性,对外部不可见
cout << person.m_age << endl; //private属性,对外部不可见
}

总结

我们对C++类三种方式控制权限总结如下,这与Java中的三种对应的访问权限是一样的。

qq%e6%88%aa%e5%9b%be20161104113813

三种继承方式

C++中继承的方式还有多种,也分别都用public、protected、private表示。这与Java不一样,Java只有继承的概念,默认是public继承的。

1. 三种继承方式不影响子类对父类的访问权限,子类对父类只看父类的访问控制权。

如下面三种继承方式都能访问父类中的public和protected成员。

?
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
class Teacher : /*public*/ /*protected*/ private Person
{
public:
Teacher(const string& name, int age, const string& title)
: Person(name, age), m_title(title)
{
}
void ShowTeacherInfo()
{
ShowInfo(); //正确,public属性子类可见
cout << "姓名:" << m_name << endl; //正确,protected属性子类可见
//cout << "年龄:" << m_age << endl; //错误,private属性子类不可见
cout << "职称:" << m_title << endl; //正确,本类中可见自己的所有成员
}
private:
string m_title; //职称
};
class Teacher : /*public*/ /*protected*/ private Person
{
public:
Teacher(const string& name, int age, const string& title)
: Person(name, age), m_title(title)
{
}
void ShowTeacherInfo()
{
ShowInfo(); //正确,public属性子类可见
cout << "姓名:" << m_name << endl; //正确,protected属性子类可见
//cout << "年龄:" << m_age << endl; //错误,private属性子类不可见
cout << "职称:" << m_title << endl; //正确,本类中可见自己的所有成员
}
private:
string m_title; //职称
};

2. 继承方式是为了控制子类(也称派生类)的调用方(也叫用户)对父类(也称基类)的访问权限。

public继承

?
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
class Teacher : public Person
{
public:
Teacher(const string& name, int age, const string& title)
: Person(name, age), m_title(title)
{
}
void ShowTeacherInfo()
{
ShowInfo(); //正确,public属性子类可见
cout << "职称:" << m_title << endl; //正确,本类中可见自己的所有成员
}
private:
string m_title; //职称
};
class Teacher : public Person
{
public:
Teacher(const string& name, int age, const string& title)
: Person(name, age), m_title(title)
{
}
void ShowTeacherInfo()
{
ShowInfo(); //正确,public属性子类可见
cout << "职称:" << m_title << endl; //正确,本类中可见自己的所有成员
}
private:
string m_title; //职称
};
void TestPublic()
{
Teacher teacher("李四", 35, "副教授");
teacher.ShowInfo();
cout << endl;
teacher.ShowTeacherInfo();
}
void TestPublic()
{
Teacher teacher("李四", 35, "副教授");
teacher.ShowInfo();
cout << endl;
teacher.ShowTeacherInfo();
}

结果:

姓名:李四
年龄:35

姓名:李四
年龄:35
职称:副教授

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
class Teacher : private Person
{
public:
Teacher(const string& name, int age, const string& title)
: Person(name, age), m_title(title)
{
}
void ShowTeacherInfo()
{
ShowInfo(); //正确,public属性子类可见
cout << "职称:" << m_title << endl; //正确,本类中可见自己的所有成员
}
private:
string m_title; //职称
};
class Teacher : private Person
{
public:
Teacher(const string& name, int age, const string& title)
: Person(name, age), m_title(title)
{
}
void ShowTeacherInfo()
{
ShowInfo(); //正确,public属性子类可见
cout << "职称:" << m_title << endl; //正确,本类中可见自己的所有成员
}
private:
string m_title; //职称
};
void TestPrivate()
{
Teacher teacher("李四", 35, "副教授");
teacher.ShowInfo(); //错误,因为Teacher采用了private的继承方式,外部不可访问。
cout << endl;
teacher.ShowTeacherInfo();
}
void TestPrivate()
{
Teacher teacher("李四", 35, "副教授");
teacher.ShowInfo(); //错误,因为Teacher采用了private的继承方式,外部不可访问。
cout << endl;
teacher.ShowTeacherInfo();
}

3. public、protected、private三种继承方式,相当于把父类的public访问权限在子类中变成了对应的权限。

如protected继承,把父类中的public成员在本类中变成了protected的访问控制权限;private继承,把父类的public成员和protected成员在本类中变成了private访问控制权。

protected继承:

?
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
class Teacher : protected Person
{
public:
Teacher(const string& name, int age, const string& title)
: Person(name, age), m_title(title)
{
}
void ShowTeacherInfo()
{
ShowInfo(); //正确,public属性子类可见
cout << "职称:" << m_title << endl; //正确,本类中可见自己的所有成员
}
private:
string m_title; //职称
};
class Teacher : protected Person
{
public:
Teacher(const string& name, int age, const string& title)
: Person(name, age), m_title(title)
{
}
void ShowTeacherInfo()
{
ShowInfo(); //正确,public属性子类可见
cout << "职称:" << m_title << endl; //正确,本类中可见自己的所有成员
}
private:
string m_title; //职称
};
void TestProtected()
{
Teacher teacher("李四", 35, "副教授");
teacher.ShowInfo(); //错误,基类Person的ShowInfo此时对Teacher相当于protected的,外部不可以被访问
cout << endl;
teacher.ShowTeacherInfo();
}
void TestProtected()
{
Teacher teacher("李四", 35, "副教授");
teacher.ShowInfo(); //错误,基类Person的ShowInfo此时对Teacher相当于protected的,外部不可以被访问
cout << endl;
teacher.ShowTeacherInfo();
}
class Leader : public Teacher
{
public:
Leader(const string& name, int age, const string& title, string position)
: Teacher(name, age, title), m_position(position)
{
}
void ShowLeaderInfo()
{
ShowInfo(); //基类Person的ShowInfo此时相当于protected的,但子类仍可以访问
ShowTeacherInfo(); //ShowTeacherInfo仍然是public的,可以访问
cout << m_position << endl;
}
private:
string m_position;
};
class Leader : public Teacher
{
public:
Leader(const string& name, int age, const string& title, string position)
: Teacher(name, age, title), m_position(position)
{
}
void ShowLeaderInfo()
{
ShowInfo(); //基类Person的ShowInfo此时相当于protected的,但子类仍可以访问
ShowTeacherInfo(); //ShowTeacherInfo仍然是public的,可以访问
cout << m_position << endl;
}
private:
string m_position;
};

以上所述是小编给大家介绍的C++ 的三种访问权限与三种继承方式,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对服务器之家网站的支持!

延伸 · 阅读

精彩推荐
  • C/C++OpenCV实现拼接图像的简单方法

    OpenCV实现拼接图像的简单方法

    这篇文章主要为大家详细介绍了OpenCV实现拼接图像的简单方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    iteye_183805102021-07-29
  • C/C++使用C++制作简单的web服务器(续)

    使用C++制作简单的web服务器(续)

    本文承接上文《使用C++制作简单的web服务器》,把web服务器做的功能稍微强大些,主要增加的功能是从文件中读取网页并返回给客户端,而不是把网页代码...

    C++教程网5492021-02-22
  • C/C++深入C++拷贝构造函数的总结详解

    深入C++拷贝构造函数的总结详解

    本篇文章是对C++中拷贝构造函数进行了总结与介绍。需要的朋友参考下...

    C++教程网5182020-11-30
  • C/C++c/c++内存分配大小实例讲解

    c/c++内存分配大小实例讲解

    在本篇文章里小编给大家整理了一篇关于c/c++内存分配大小实例讲解内容,有需要的朋友们可以跟着学习参考下。...

    jihite5172022-02-22
  • C/C++C语言实现双人五子棋游戏

    C语言实现双人五子棋游戏

    这篇文章主要为大家详细介绍了C语言实现双人五子棋游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    两片空白7312021-11-12
  • C/C++关于C语言中E-R图的详解

    关于C语言中E-R图的详解

    今天小编就为大家分享一篇关于关于C语言中E-R图的详解,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看...

    Struggler095962021-07-12
  • C/C++c/c++实现获取域名的IP地址

    c/c++实现获取域名的IP地址

    本文给大家汇总介绍了使用c/c++实现获取域名的IP地址的几种方法以及这些方法的核心函数gethostbyname的详细用法,非常的实用,有需要的小伙伴可以参考下...

    C++教程网10262021-03-16
  • C/C++C语言main函数的三种形式实例详解

    C语言main函数的三种形式实例详解

    这篇文章主要介绍了 C语言main函数的三种形式实例详解的相关资料,需要的朋友可以参考下...

    ieearth6912021-05-16