C++的三种访问权限与三种继承方式
来源:互联网 发布:淘宝男模特 编辑:程序博客网 时间:2024/05/30 19:34
【转载】http://blog.csdn.net/luoweifu/article/details/46953343 访问权限 public protected private 对本类 可见 可见 可见 对子类 可见 可见 不可见 对外部(调用方) 可见 不可见 不可见
三种访问权限
我们知道C++中的类,有三种访问权限(也称作访问控制),它们分别是public、protected、private。要理解它们其实也很容易,看下面了一个例子。
父类:
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
子类:
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
- 15
- 16
- 17
- 18
- 19
- 20
调用方:
void test(){ Person person("张三", 22); person.ShowInfo(); //public属性,对外部可见 cout << person.m_name << endl; //protected属性,对外部不可见 cout << person.m_age << endl; //private属性,对外部不可见}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
总结
我们对C++类三种方式控制权限总结如下,这与Java中的三种对应的访问权限是一样的。
三种继承方式
C++中继承的方式还有多种,也分别都用public、protected、private表示。这与Java不一样,Java只有继承的概念,默认是public继承的。
1. 三种继承方式不影响子类对父类的访问权限,子类对父类只看父类的访问控制权。如下面三种继承方式都能访问父类中的public和protected成员。
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; //职称};
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
.
2. 继承方式是为了控制子类(也称派生类)的调用方(也叫用户)对父类(也称基类)的访问权限。
public继承
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; //职称};
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
void TestPublic(){ Teacher teacher("李四", 35, "副教授"); teacher.ShowInfo(); cout << endl; teacher.ShowTeacherInfo();}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
结果:
姓名:李四
年龄:35姓名:李四
年龄:35
职称:副教授
private继承:
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; //职称};
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
void TestPrivate(){ Teacher teacher("李四", 35, "副教授"); teacher.ShowInfo(); //错误,因为Teacher采用了private的继承方式,外部不可访问。 cout << endl; teacher.ShowTeacherInfo();}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
.
3. public、protected、private三种继承方式,相当于把父类的public访问权限在子类中变成了对应的权限。 如protected继承,把父类中的public成员在本类中变成了protected的访问控制权限;private继承,把父类的public成员和protected成员在本类中变成了private访问控制权。
protected继承:
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; //职称};
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
void TestProtected(){ Teacher teacher("李四", 35, "副教授"); teacher.ShowInfo(); //错误,基类Person的ShowInfo此时对Teacher相当于protected的,外部不可以被访问 cout << endl; teacher.ShowTeacherInfo();}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
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;};
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
阅读全文
0 0
- C++——三种继承方式与三种访问权限的相互组合
- C++——三种继承方式与三种访问权限的相互组合
- C++——三种继承方式与三种访问权限的相互组合
- 三种继承方式与三种访问权限的相互组合
- C++的三种访问权限与三种继承方式
- C++的三种访问权限与三种继承方式
- C++的三种访问权限与三种继承方式
- 三种继承方式与三种访问权限的相互组合
- C++的三种访问权限与三种继承方式
- 类的三种继承方式的访问权限.
- C++三种继承方式下的访问权限控制
- 三种类的访问权限与继承方式 public protected private derive
- C++访问控制-三种继承方式
- C++访问控制-三种继承方式
- 浅谈c++类成员的三种访问控制方式和类继承派生时的三种继承方式
- C++——三种继承方式
- C++的三种继承机制以及继承机制对三种访问权限(public, protected, private)的影响
- 三种继承方式
- HDU 4057 AC自动机+状压dp
- 如何根据configure.ac和Makefile.am为开源代码产生当前平台的Makefile
- leetcode462. Minimum Moves to Equal Array Elements II
- 不同buildType下的区别构建配置方式
- css实现梯形标签页
- C++的三种访问权限与三种继承方式
- 第二周项目一C/C++语言中函数参数传递的三种方式之传值
- UIlabel,文字复制
- 大型分布式网站优化术语分析
- php中的错误类写法
- BZOJ 1003 物流运输 (spfa+dp)
- php根据Http请求头部信息下载文件
- Linux系统编程——vfork() 函数详解
- Android 文件存储