Three access rights and three ways of inheriting from C + +

Source: Internet
Author: User

Three kinds of access rights

We know that classes in C + + have three access rights (also known as access Control), which are public, protected, private, respectively. To understand them is also very easy, see below an example.
Parent class:

class Person{public:    Person(conststringintm_namem_age(age)    {    }    void ShowInfo()    {        "姓名:" << m_name << endl;        "年龄:" << m_age << endl;    }protected:    string  m_name;     //姓名private:    int     m_age;      //年龄};

Sub-class:

Class Teacher: Publicperson{ Public:Teacher(Const string& Name,intAgeConst string& title): Person(name, age),M_title(title) {    }voidShowteacherinfo () {showinfo ();//correct, public attribute subclass visiblecout <<"Name:"<< m_name << Endl;//correct, protected attribute subclass visiblecout <<"Age:"<< m_age << Endl;//error, private attribute subclass not visiblecout <<"title:"<< m_title << Endl;//correct, all of its members are visible in this class}Private:stringM_title;//Job title};

Calling party:

void test(){    Person person("张三"22);    person.ShowInfo();                  //public属性,对外部可见    cout << person.m_name << endl;      //protected属性,对外部不可见    cout << person.m_age << endl;       //private属性,对外部不可见}
Summarize

We summarize the C + + class Three control permissions as follows, which is the same as the three corresponding access rights in Java.

Public
access Rights protected Private
For this class Visible Visible Visible
Sub-class Visible Visible Not visible
to external (caller) Visible Not visible Not visible
Three ways of inheriting

There are many ways of inheriting in C + +, and they are all represented by public, protected and private. This is not the same as Java, Java has only the concept of inheritance, the default is public inheritance.
1. The three inheritance methods do not affect the subclass's access to the parent class, and the subclass only looks at the parent class for access control. The following three inheritance methods can access public and protected members in the parent class.

Class Teacher:/*public*/ /*protected*/ Privateperson{ Public:Teacher(Const string& Name,intAgeConst string& title): Person(name, age),M_title(title) {    }voidShowteacherinfo () {showinfo ();//correct, public attribute subclass visiblecout <<"Name:"<< m_name << Endl;//correct, protected attribute subclass visible        //cout << "Age:" << m_age << Endl; Error, private attribute subclass not visiblecout <<"title:"<< m_title << Endl;//correct, all of its members are visible in this class}Private:stringM_title;//Job title};

.
2. The inheritance method is to control the access rights of the caller (also called the user) of the subclass (also known as the derived class) to the parent class (also called the base class).

Public inheritance

class Teacher: public  person{ Public : teacher  (const  string  & name, int  age, const  string  & title): person  (name, age), Span class= "Hljs-title" >m_title (title) {} void  showteacherinfo () { Showinfo (); //correct, public attribute subclass visible  cout <<  "title:"  << m _title << Endl; //correct, all of its members are visible in this class } private : string  m_title; //title }; 
void TestPublic(){    Teacher teacher("李四"35"副教授");    teacher.ShowInfo();    cout << endl;    teacher.ShowTeacherInfo();}

Results:

Name: John Doe
Age: 35

Name: John Doe
Age: 35
Title: Associate Professor

Private inheritance:

class Teacher: private  person{ public : teacher  ( Const  string  & name, int  age, const  string  & title):  Person  (name, age), m_title  (title) {} void Showteacherinfo () {showinfo (); //correct, public attribute subclass visible  cout <<  "title:"  << m _title << Endl; //correct, all of its members are visible in this class } private : string  m_title; //title }; 
TestPrivate(){    teacher("李四", 35, "副教授");    teacher.ShowInfo();             //错误,因为Teacher采用了private的继承方式,外部不可访问。    cout << endl;    teacher.ShowTeacherInfo();}

.
3. Public , protected, private three inheritance, equivalent to the parent class public access rights in the subclass into the corresponding permissions. if protected inherits, the public member in the parent class becomes the protected access control permission in this class, and private inherits the public member of the parent class into the private access control in this class.

Protected inheritance:

class Teacher: protected  person{ public : teacher  ( Const  string  & name, int  age, const  string  & title):  Person  (name, age), m_title  (title) {} void Showteacherinfo () {showinfo (); //correct, public attribute subclass visible  cout <<  "title:"  << m _title << Endl; //correct, all of its members are visible in this class } private : string  m_title; //title }; 
TestProtected(){    teacher("李四", 35, "副教授");    teacher.ShowInfo();         //错误,基类Person的ShowInfo此时对Teacher相当于protected的,外部不可以被访问    cout << endl;    teacher.ShowTeacherInfo();}
Class Leader: Publicteacher{ Public:Leader(Const string& Name,intAgeConst string& Title,stringPosition):Teacher(name, age, title),m_position(position) {    }voidShowleaderinfo () {showinfo ();The showinfo of the //base class person is now equivalent to protected, but the subclass can still accessShowteacherinfo ();The//showteacherinfo is still public and can be accessedcout << m_position << Endl; }Private:stringM_position;};

Copyright NOTICE: This article for Bo Master original article, without Bo Master permitted not for any commercial use, reproduced please indicate the source.

Three access rights and three ways of inheriting from C + +

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.