C + + Three access rights and three kinds of inheritance _c language

Source: Internet
Author: User
Tags inheritance

Three kinds of access rights

We know that classes in C + + have three kinds of access rights (also known as access Control), which are public, protected, private, respectively. To understand them is also easy, look at an example below.

Parent class:

Class person
{public
: Person
(const string& name, int age): M_name (name), M_age (age)
{
}
void Showinfo ()
{
cout << "Name:" << m_name << Endl;
cout << "Age:" << m_age << Endl;
}
Protected:
string m_name;//Name
private:
int m_age;//Age
};
Class person
{public
: Person
(const string& name, int age): M_name (name), M_age (age)
{
}
void Showinfo ()
{
cout << "Name:" << m_name << Endl;
cout << "Age:" << m_age << Endl;
}
Protected:
string m_name;//Name
private:
int m_age;//Age
};

Sub class:

Class Teacher:public 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 << "Name:" << m_name << Endl;//correct, protected attribute subclass visible
cout << "Age:" << M_age < < Endl; Error, private attribute subclass not visible
cout << "title:" << m_title << Endl;//correct, all members in this class are
private:
string m_title;//job title
};
Class Teacher:public person
{public
:
Teacher (const string& name, int age, const string& title) c21/>: Person (name, age), M_title (title)
{
}
void Showteacherinfo ()
{
showinfo ();//correct, The public attribute subclass
is visible cout << "name:" << m_name << Endl;//correct, protected attribute subclass visible
cout << "Age:" <& Lt M_age << Endl; Error, private attribute subclass not visible
cout << "title:" << m_title << Endl;//correct, all members in this class are
private:
string m_title;//job title
};
   

Call Method:

void Test ()
{person
of person ("John");
Person. Showinfo (); Public properties, externally visible
cout << person.m_name << Endl//protected properties, externally invisible
cout << person.m_age << Endl; Private property, on external invisible
}
void Test () {person person
("John");
Person. Showinfo (); Public properties, externally visible
cout << person.m_name << Endl//protected properties, externally invisible
cout << person.m_age << Endl; Private property, not visible to external

Summarize

We summarize the following three ways of controlling permissions for C + + classes, which is the same as the three corresponding access rights in Java.

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

Three ways of inheriting

C + + inheritance in a variety of ways, but also by public, protected, private expressed. This is not the same as Java, Java only inherited the concept, the default is public inheritance.

1. Three inheritance methods do not affect the access rights of subclasses to the parent class, which only look at the parent class's access control.

You can access both public and protected members of the parent class, such as the following three ways of inheriting.

 class Teacher:/*public*//*protected*/private Person {public:teacher (const STRING&AM P Name, int age, const string& title: Person (name, age), M_title (title) {} void Showteacherinfo () {showinfo ();//correct, The public attribute subclass is visible cout << "name:" << m_name << Endl; Correct, protected attribute subclasses are visible//cout << "Age:" << m_age << Endl; Error, private attribute subclass not visible cout << "title:" << m_title << Endl; Correct, all of its members are visible in this class} private:string m_title;
Title}; Class Teacher:/*public*//*protected*/private Person {public:teacher (const string& name, int age, const STRING&AM P title): Person (name, age), M_title (title) {} void Showteacherinfo () {showinfo ();//correct, public attribute subclass visible cout << "Name:" << m_name << Endl; Correct, protected attribute subclasses are visible//cout << "Age:" << m_age << Endl; Error, private attribute subclass not visible cout << "title:" << m_title << Endl; Correct, all of its members are visible in this class} private:string m_title; 
Title}; 

2. Inheritance is to control the access of the caller (also known as the user) of the subclass (also called 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), 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
};
Class Teacher:public person
{public
:
Teacher (const string& name, int age, const string& title) c19/>: Person (name, age), M_title (title)
{
}
void Showteacherinfo ()
{
showinfo ();//correct, The public attribute subclass
is 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 ("Dick", 35, "Associate Professor");
Teacher. Showinfo ();
cout << Endl;
Teacher. Showteacherinfo ();
}
void Testpublic ()
{
Teacher Teacher ("Dick", 35, "Associate Professor");
Teacher. Showinfo ();
cout << Endl;
Teacher. Showteacherinfo ();
}
   

Results:

Name: Dick
Age: 35

Name: Dick
Age: 35
Title: Associate Professor

Private inheritance:

 class teacher:private person {public:teacher (const string& name, int age, const s tring& 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}; 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 <& Lt Endl Correct, all of its members are visible in this class} private:string m_title;
Title}; void Testprivate () {Teacher Teacher ("Dick", 35, "Associate Professor"); Teacher. Showinfo ();
Error, because teacher adopts private inheritance and is not accessible externally.
cout << Endl; Teacher.
Showteacherinfo (); } void Testprivate () {Teacher Teacher ("Dick", 35, "Associate Professor"); Teacher. Showinfo ();
Error, because teacher adopts private inheritance and is not accessible externally.
cout << Endl; Teacher.
Showteacherinfo (); }

3. Public, protected, private three kinds of inheritance, which is equivalent to the public access to the parent class in the subclass into the corresponding permissions.

such as protected inheritance, the public members in the parent class are transformed into protected access control rights in this class, private inheritance, and the public and protected members of the parent class into private access control in this class.

Protected inheritance:

Class teacher:protected Person {public:teacher (const string& name, int age, const string& title): Person (nam E, age), M_title (title) {} void Showteacherinfo () {showinfo ();//correct, public attribute subclass visible cout << "title:" << m_title &L t;< Endl; Correct, all of its members are visible in this class} private:string m_title;
Title}; 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}; void testprotected () {Teacher Teacher ("Dick", 35, "Associate Professor"); Teacher. Showinfo ();
Error, the base class person's showinfo at this time to teacher equivalent protected, the outside can not be visited cout << Endl; Teacher.
Showteacherinfo (); } void Testprotected () {Teacher Teacher ("Dick", 35, "Associate Professor"); Teacher. Showinfo ();
Error, the base class person's showinfo at this time to teacher equivalent protected, the outside can not be visited 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 ();//base class person's showinfo is equivalent to protected at this time, but subclasses can still access Showteacherinfo ();
Showteacherinfo is still public and can access 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 ();//base class person's showinfo at this point is equivalent to protect Ed's, but subclasses can still access showteacherinfo ();
Showteacherinfo is still public and can access cout << m_position << Endl;
} private:string m_position; };

The above is a small set to introduce the C + + three access rights and three kinds of inheritance, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

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.